Linux - 用户管理 - 用户组

【基本介绍】

Linux系统的一大块就是用户管理,管理用户的信息,这里介绍用户组。

常用的命令有groupadd,groupdel,gpasswd,groups,finger

用户组分为两类:

私有组->在创建一个新用户时,若没有指定所属于的组,linux将建立一个和用户同名的私有组

标准组->可以容纳多个用户,若使用标准组创建一个新用户是应该指定该用户的所在组

【基本操作】

添加用户组:可以用groupadd,也可以useradd

[root@bogon srv]# groupadd --help
Usage: groupadd [options] GROUP

Options:
  -f, --force                   exit successfully if the group already exists,
                                and cancel -g if the GID is already used
  -g, --gid GID                 use GID for the new group
  -h, --help                    display this help message and exit
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -o, --non-unique              allow to create groups with duplicate
                                (non-unique) GID
  -p, --password PASSWORD       use this encrypted password for the new group
  -r, --system                  create a system account
[root@bogon srv]# grep test /etc/group
[root@bogon srv]# groupadd test 
[root@bogon srv]# grep test /etc/group
test:x:508:

useradd-g组名用户名----创建用户并将该用户加入到组中

修改用户组

[root@bogon srv]# groupmod --help
Usage: groupmod [options] GROUP
Options:
  -g, --gid GID                 change the group ID to GID
  -h, --help                    display this help message and exit
  -n, --new-name NEW_GROUP      change the name to NEW_GROUP
  -o, --non-unique              allow to use a duplicate (non-unique) GID
  -p, --password PASSWORD       change the password to this (encrypted)
                                PASSWORD
[root@bogon srv]# groupmod -n ttest test
[root@bogon srv]# grep test /etc/group
ttest:x:508:

添加删除用户组:

[root@bogon srv]#  gpasswd --help
gpasswd: unrecognized option '--help'
Usage: gpasswd [option] GROUP
Options:
  -a, --add USER                add USER to GROUP
  -d, --delete USER             remove USER from GROUP
  -r, --remove-password         remove the GROUP's password
  -R, --restrict                restrict access to GROUP to its members
  -M, --members USER,...        set the list of members of GROUP
  -A, --administrators ADMIN,...
                                set the list of administrators for GROUP
Except for the -A and -M options, the options cannot be combined.
[root@bogon srv]# gpasswd -a spark ttest
Adding user spark to group ttest
[root@bogon srv]# grep ttest /etc/group
ttest:x:508:spark
[root@bogon srv]# grep ttest /etc/group
ttest:x:508:spark
[root@bogon srv]# gpasswd -d spark ttest
Removing user spark from group ttest
[root@bogon srv]# grep ttest /etc/group
ttest:x:508:

groupdel用户组名----删除用户组,该帐号必须存在且不能作为某个用户的私有组引用(不能删除私有组和不存在的组),如果该组中还有其他用户在使用,必须先删除组中的用户才能删除该组

查看用户组

[root@bogon srv]# man groups
GROUPS(1)                        User Commands                       GROUPS(1)
NAME
       groups - print the groups a user is in
SYNOPSIS
       groups [OPTION]... [USERNAME]...

DESCRIPTION
       Print  group  memberships  for  each USERNAME or, if no USERNAME is specified, for the current process (which may differ if the groups
       database has changed).

groups用户名----查看用户属于哪个组

[root@bogon srv]# groups spark
spark : spark
[root@bogon srv]# gpasswd -a spark ttest
Adding user spark to group ttest
[root@bogon srv]# groups spark
spark : spark ttest

【参考】

http://bbs.chinaunix.net/thread-3751354-1-1.html

相关推荐