用户与组的创建

概念 和 基本信息

centos Linux用户中分为三类用户:即超级用户(root用户、根用户,用户UID=0)、普通用户(用户UID>=1000)、伪用户(用户UID=1~999)

centos Linux组分类也和用户类似,普通组GID>=1000。

超级用户拥有系统的最高权限,甚至可以删除系统;普通用户相对于超级用户少了很多权限,比如删除用户,禁止用户,添加用户组,删除用户组,修改系统配置等等;伪用户是无法使用密码登陆的,即仅仅用于系统自动的用户,人是无法用于登陆使用的

linux系统支持多用户,除了管理员,其他用户一般不应该使用root,而是应该向管理员申请一个账号。组类似于角色,系统可以通过组对有共性的用户进行统一管理。每个用户应该至少属于一个组,不能游离于组外;且在创建用户时,系统会在/home目录下自动生成一个目录作为该用户的“家目录”,该用户在登录后会首先进入家目录。家目录可以在添加用户时指定,如果不指定,默认会以用户名来命名。例如,用户bob的家目录默认是/home/bob,他在登录linux系统后,会首先进入/home/bob。

用户与组基本信息
/etc/passwd是用来储存用户信息的文件,/etc/shadow是用来储存用户加密后密码及密码相关属性的文件, /etc/group是用来存放用户组信息的文件,/etc/gshadow加密后的组密码,一般不会使用。

每个用户应该至少属于一个初始组,但是可以属于多个附加组。一般情况下,用户的初始组就是在建立用户的同时建立的和用户名相同的组。用户的权限也只属于当前的初始组。

/etc/group
/etc/gshadow

组的创建
[root@usertest ~]# groupadd -h
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
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       directory prefix
-f, --force          			如果组存在则成功退出,不创建新的组,如果组ID已经存在则取消-g 参数设置
  -g, --gid GID              	设置组ID,未指明则就会按照用户组ID中上一个gid自增1
  -o, --non-unique              允许组ID(GID)不唯一,可重复。
  -p, --password PASSWORD       设置组密码,一般不会使用

创建三个组 group1,group2,group3

groupadd group1
groupadd -g 2000 group2
groupadd group3

查看组信息可以看到三个新创建的组。

cat /etc/group

centos 创建多个用户 方法 centos创建用户和组_用户名

信息使用:分隔。分为4部分
组名:密码:组ID(GID):组中用户列表

组名修改及组删除

修改组

[root@usertest ~]# groupmod -h
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
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
-g, --gid GID                 修改组ID(GID)
  -n, --new-name NEW_GROUP      修改组名
  -o, --non-unique              允许设置的组ID重复
  -p, --password PASSWORD       修改组密码
groupmod -g 1002 group2
groupmod -g 1111 -n group1111 group3

centos 创建多个用户 方法 centos创建用户和组_centos 创建多个用户 方法_02


删除组

[root@usertest ~]# groupdel -h
Usage: groupdel [options] GROUP

Options:
  -h, --help                    display this help message and exit
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -f, --force                   delete group even if it is the primary group of a user

centos 创建多个用户 方法 centos创建用户和组_用户名_03

用户

/etc/passwd
/etc/shadow

用户的创建
[root@usertest ~]# useradd -h
Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]

Options:
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

常用

Options:
  -b, --base-dir BASE_DIR       设置用户家目录的父目录
  -d, --home-dir HOME_DIR       设置用户的家目录
  -g, --gid GROUP               设置用户的组id或组名
  -G, --groups GROUPS           设置用户的附加ID
  -m, --create-home             创建用户时创建用户家目录
  -M, --no-create-home          创建用户时不创建用户家目录
  -o, --non-unique              允许用户的uid重复
  -p, --password PASSWORD       设置用户的登录密码,一般不会在创建时指定。
  -s, --shell SHELL             设置用户所用的shell
  -u, --uid UID                 用户的uid
  -U, --user-group              创建用户时创建组,组名和用户名相同

默认情况,用户家目录的父目录是 /home 用户的家目录和用户名一致,组名和用户名一致,创建用户时创建家目录,不允许用户uid重复,无密码。

用户创建

useradd user1
groupadd group2
useradd -g group2 user2
useradd -d /user3 -p 123456 user3

为用户设置密码

passwd user1

查看用户信息

cat /etc/passwd

centos 创建多个用户 方法 centos创建用户和组_用户名_04


主要看最后三行,用户信息用 : 分隔。分别是

用户名 : 用户密码(使用X代替,具体存储在/etc/shadow) : 用户ID : 组ID : 用户注释信息 : 用户的家目录 : 用户登录所使用的shell

user3 的 家目录 是 /user3,而未设置家目录的user1 user2 家目录是/home 下的同名文件夹。

查看用户密码

cat /etc/shadow

centos 创建多个用户 方法 centos创建用户和组_centos 创建多个用户 方法_05


看最后三行,user1 user2 user3

user2未设置密码,user1和user3密码都为123456 但是user1是创建后设置的密码,因此是加密的,user3是创建时设置的密码,不是加密的。(奇怪的是user3密码好像无法使用。。。) 因此用户密码设置最好是创建用户之后使用passwd 命令设置而不是直接使用-p 参数设置

查看用户信息

id 用户名
id user1
id user2
id user3

centos 创建多个用户 方法 centos创建用户和组_centos_06


三个信息分别是 用户ID 组ID(初始组ID) 所属组列表。

初始组可以看做当前用户所使用的组?使用newgrp 组名可以更改初始组。

用户修改删除

用户修改

[root@usertest ~]# usermod -h
Usage: usermod [options] LOGIN

Options:
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                the user from other groups
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name
  -L, --lock                    lock the user account
  -m, --move-home               move contents of the home directory to the
                                new location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -v, --add-subuids FIRST-LAST  add range of subordinate uids
  -V, --del-subuids FIRST-LAST  remove range of subordinate uids
  -w, --add-subgids FIRST-LAST  add range of subordinate gids
  -W, --del-subgids FIRST-LAST  remove range of subordinate gids
  -Z, --selinux-user SEUSER     new SELinux user mapping for the user account

常用

Options:
  -d, --home HOME_DIR           修改新的用户家目录
  -g, --gid GROUP               设置新的组ID
  -G, --groups GROUPS           设置新的附加组列表
  -l, --login NEW_LOGIN         设置新的登录名 可以看做更改新的用户名,但是家目录未改变(如果未设置 -d 参数)
  -m, --move-home               将原家目录内容转移到新家目录,只有-d参数设置时有效
  -s, --shell SHELL             新的登录shell
  -u, --uid UID                 设置用户新的uid

示例,将用户 user1 的用户组更改为为 group1

groupadd group1
usermod -g group1 user1
id user1

centos 创建多个用户 方法 centos创建用户和组_创建用户_07

用户删除

[root@usertest ~]# userdel -h
Usage: userdel [options] LOGIN

Options:
  -f, --force                   force some actions that would fail otherwise
                                e.g. removal of user still logged in
                                or files, even if not owned by the user
  -h, --help                    display this help message and exit
  -r, --remove                  remove home directory and mail spool
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -Z, --selinux-user            remove any SELinux user mapping for the user

常用

Options:
  -f, --force                  强制删除,即使有进程使用该用户启动
  -r, --remove                  删除用户同时删除该目录。

示例,删除 三个用户

userdel user1
userdel -r user2
userdel -rf user3

查看 /etc/passwd 用户信息 发现三个用户都被删除。查看 /home 目录

centos 创建多个用户 方法 centos创建用户和组_centos 创建多个用户 方法_08


另,一般情况下删除用户时,当用户初始组时创建用户时创建的,会将该用户的初始组一并删除。

rm -rf /home/user1/

12.文件与用户、组的关系

一个用户创建了一个文件,则这个用户就自动成为这个文件的"所有者",这个用户所属的初始组就成为文件的"所在组",除了所有者和所在组的用户,系统中的其他用户对于文件都是"其他组"的用户。可以通过ls -l命令查看文件的所有者和所在组。但是文件的所有者和所在组并不是固定不变的,可以修改。

先将先前创建的用户和组一并删除。再创建新的用户组进行测试。

创建三个组

groupadd group1
groupadd group2
groupadd group3

创建一个用户 usetest,设置初始组为group1,附加组为group2,group3并设置密码。

useradd -g group1 -G group2,group3 usertest
passwd usertest

centos 创建多个用户 方法 centos创建用户和组_创建用户_09


切换到用户usertest 并进入该用户家目录 查看用户组

从root用户切换到普通用户不用输入密码

su usertest
cd
groups

centos 创建多个用户 方法 centos创建用户和组_centos_10

该用户属于一个初始组,两个附加组。用户的有效组是初始组。

切换用户初始组命令 newgrp 附加组组名
使用 newgrp 命令一边切换 user1 的初始组,一边创建文件。

touch test1_file
mkdir test1_dir
newgrp group2
touch test2_file
mkdir test2_dir
newgrp group3
touch test3_file
mkdir test3_dir

可以看到,三个文件所属的 用户一致,但是所属的用户组不一致。

centos 创建多个用户 方法 centos创建用户和组_linux_11

文件所属用户组及权限

ls -l命令可以以列表形式展示当前目录下的子目录和文件

ls -l
[usertest@usertest ~]$ ls -l
total 0
drwxr-xr-x. 2 usertest group1 6 Jul 13 03:39 test1_dir
-rw-r--r--. 1 usertest group1 0 Jul 13 03:39 test1_file
drwxr-xr-x. 2 usertest group2 6 Jul 13 03:39 test2_dir
-rw-r--r--. 1 usertest group2 0 Jul 13 03:39 test2_file
drwxr-xr-x. 2 usertest group3 6 Jul 13 03:39 test3_dir
-rw-r--r--. 1 usertest group3 0 Jul 13 03:39 test3_file


分为8部分

(1)d(2)rwxr-xr-x. (3)2 (4)usertest (5)group1 (6)6 (7)Jul 13 03:39 (8)test1_dir
(1)-(2)rw-r--r--. (3)1 (4)usertest (5)group1 (6)0 (7)Jul 13 03:39 (8)test1_file

(1) 第一组只有1个字符,表示条目的类型:

-:普通文件

d:目录

l:软链接

c:字符设备,例如键盘、鼠标

b:块文件,例如硬盘;

(2) 第二组有9个字符,每3个表达一个意思:

第1-3个表示文件所有者的权限,

第4-6个表示文件所在组的用户的权限,

第7-9个表示文件其他组的权限。

总的来说,r表示可读,w表示可写,x表示可执行,但是对于文件和目录,表达的意思又不尽相同,下面分别描述。

文件

目录

-

无权限

无权限

r

可以用cat、more、less等命令查看文件内容

可以用ls命令查看目录下的内容

w

可以用vi、vim编辑器修改文件,但不代表可删除

可以在目录内增加、删除、重命名文件或子目录

x

可执行

可以用cd命令进入该目录

(3) 第三组是一个数字,对于文件来说是硬链接数,通常是1,对于目录是其下的子目录数量(包含隐藏子目录)。

(4) 第四组表示文件的所有者。

(5) 第五组表示文件的所在组。

(6) 第六组是一个数字,表示其大小(KB)。

(7) 第七组是日期时间,表示文件或目录的最后修改时间。

(8) 第八组是文件名或目录的名字。

文件或目录所属用户与用户组更改

drwxr-xr-x. 2 usertest group2 6 Jul 13 03:39 test2_dir
-rw-r--r--. 1 usertest group2 0 Jul 13 03:39 test2_file

所属用户更改 命令 chown

chown [-R] username source

其中,username为修改后的用户名,source为文件或目录的路径名称,-R表示级联修改目录下面的子目录和文件的所有者。

所属用户组更改命令 chgrp

chgrp [-R] groupname source

其中,groupname为修改后的组名,source为文件或目录的路径名称,-R表示级联修改目录下面的子目录和文件的所在组。注意修改文件的所有者和所在组是相互独立的事件,互不影响。

所属用户与用户组同时修改命令 chown

chown [-R] username:groupname source

其中,username为修改后的用户名,groupname为修改后的组名,source为文件或目录的路径名称,-R表示级联修改目录下面的子目录和文件的所在组。这里的用户和组没有必然联系,即username不一定要属于groupname的那个组。

测试修改用户与组
创建用户 feng 默认初始组 feng

useradd feng
passwd feng
chown feng test1_file
chown -R feng test1_dir/
chgrp feng test2_file
chgrp -R feng test2_dir/
chown feng:feng test3_file
chown -R feng:feng test3_dir

centos 创建多个用户 方法 centos创建用户和组_用户名_12

文件或目录权限设置

文件或目录的权限可看做分为三部分,所属用户权限,所属组权限,其它组的权限。注,超级用户权限不在此列,超级用户如root 对几乎所有文件目录有所有权限。

修改文件权限命令 chmod
(1) 使用权限代码赋值

chmod role1=auth source

其中,role为角色代码,可以设置其中的一个或多个,多个角色之间用逗号(,)分隔且不能有空格,其取值范围如下:

u-文件或目录的所有者;

g-文件或目录所在组的用户;

o-其他用户;

a-所有用户,包含u、g、o,只能单独使用。

auth为权限码组合,例如读写执行权限为rwx,读和执行权限为rx,写权限为w,根据需要选择。

source为文件或目录的路径名称。

(2) 使用加减号添加或减少权限

chmod role+auth source

chmod role-auth source

role、auth、source的含义同上,同样可以设置一个或多个角色。

(3) 使用数字赋值

chmod xyz source

其中,x、y、z是三个数字,分别表示文件或目录的所有者、所在组的用户、其他组的用户的"权限值"。

权限值是这样计算的:

r=4

w=2

x=1

将这个用户的权限代码对应的数字加起来就是"权限值",所以其取值范围可能是0、1、2、3、4、5、6、7。

设置权限 实例
一 。修改test1_file 所有用户只有读权限

chmod u=r,g=r,o=r test1_file

二。修改test2_file 所属用户增加执行权限,所属组增加可写权限,其他组去除可读权限。

chmod u+x,g+w,o-r test2_file

三。修改test3_file所有用户设置所有权限。

chmod 777 test3_file

centos 创建多个用户 方法 centos创建用户和组_创建用户_13