服务器平台:
VMware® Workstation 15 Pro(15.5.2 build-15785246)
CentOS7(3.10.0-957.el7.x86_64)
Part1. 用户和用户组
1.查看系统用户和用户组
查看系统用户
命令
cat /etc/passwd
结果
[root@localhost jack]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
jack:x:1000:1000:CentOS:/home/jack:/bin/bash
stack:x:1001:1000::/home/stack:/bin/bash
...
说明,共7个字段
字段id | 说明 |
---|---|
1 | 用户名 |
2 | 密码 x表示 真正的密码在 /etc/shadow加密存储 |
3 | 用户id |
4 | 用户组id |
5 | 注释,可通过useradd -c 添加用户注释 |
6 | 用户目录 |
7 | shell路径 |
查看用户密码
命令,只有 root 才有权限
cat /etc/shadow
结果
[root@localhost jack]# cat /etc/shadow
root:$1$Lunhh7P6$sNoV7VANcjiI2VL0BO7rD/::0:99999:7:::
bin:*:17834:0:99999:7:::
daemon:*:17834:0:99999:7:::
jack:$1$Lunhh7P6$sNoV7VANcjiI2VL0BO7rD/:18474:0:99999:7:::
stack:$1$L2mGGMRO$H6sEiHTAz2qYrD8AG0pHx.:18529:0:99999:7:::
说明,共9个字段
jack
:$1$Lunhh7P6$sNoV7VANcjiI2VL0BO7rD/
:18474
:0
:99999
:7
:
:
:
字段id | 说明 |
---|---|
1 | 帐号 |
2 | 密码 |
3 | 自1970-01-01 密码被修改的天数 |
4 | 多少天后允许修改密码 0 表示随时可修改 |
5 | 多少天后 系统强制用户修改成新密码 1 表示永远不能修改 |
6 | 密码过期前多少天 系统提示用户密码将过期 -1 不会有警告 |
7 | 密码过期后多少天 系统禁用这个帐号 -1 表示永不禁用 |
8 | 这个帐号被禁用的天数 |
9 | 留用 |
查看用户组
命令
cat /etc/group
结果
[root@localhost ~]# cat /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
jack:x:1000:
docker:x:981:
说明
四个字段分别为:用户名、密码、组ID、包含的用户
2.用户管理
查看用户信息
[root@localhost ~]# id stack
uid=1001(stack) gid=1000(jack) groups=1000(jack)
查看用户所在分组:groups user
[root@localhost ~]# groups stack
stack : dev root jack
添加用户
useradd --help
-----------------------------------
# 默认情况会添加:用户,用户组,用户目录
useradd user_name
修改用户
#
usermod -d /home/new -s /bin/bash -g dev
查看帮助
[root@localhost ~]# usermod --help
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
him/her 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
-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
-Z, --selinux-user SEUSER new SELinux user mapping for the user account
删除用户
# 删除 /etc/passwd、/etc/shadow、/etc/group 这三个目录中的信息,保留 /home/user 目录信息
userdel user
查看帮助
[root@localhost ~]# userdel --help
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
-Z, --selinux-user remove any SELinux user mapping for the user
设置密码
passwd user
如果不指定 user ,默认为当前用户设定新密码。雷同,su 不指定 user,默认切换到 root。
为用户设置分组
- 设置默认分组,这里只能指定一个默认分组
usermod -g group
- 添加到其他分组,退出之前所在的其他分组(默认分组除外)
usermod -G group1,group2,group3 user
- 添加到其他分组,而不退出之前加入的其他分组
usermod -a -G group1,group2 user
将用户移除分组
gpasswd -d user group
查看帮助
[root@localhost ~]# gpasswd --help
Usage: gpasswd [option] GROUP
Options:
-a, --add USER add USER to GROUP
-d, --delete USER remove USER from GROUP
-h, --help display this help message and exit
-Q, --root CHROOT_DIR directory to chroot into
-r, --delete-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.
哟西,还可以添加用户、移除组密码
3.组管理
新建用户组
groupadd group_name
查看帮助
[root@localhost ~]# 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
-R, --root CHROOT_DIR directory to chroot into
删除用户组
groupdel group
Part2. 文件权限
Part3. Linux系统特点
理解Linux的多用户多任务
Linux 是真正意义上的多用户操作系统,多用户系统从事实来说对系统管理更为方便。从安全角度来说,多用户管理的系统更为安全,因为文件/路径都是设有权限的。
用户角色区分
用户在系统中是分角色的,在Linux 系统中,由于角色不同,权限和所完成的任务也不同;值得注意的是用户的角色是通过UID和识别的,特别是UID;在系统管理中,系统管理员一定要坚守UID 唯一的特性;
- root 用户:系统唯一,是真实的,可以登录系统,可以操作系统任何文件和命令,拥有最高权限;
- 虚拟用户:这类用户也被称之为伪用户或假用户,与真实用户区分开来,这类用户不具有登录系统的能力,但却是系统运行不可缺少的用户,比如bin、daemon、adm、ftp、mail等;这类用户都系统自身拥有的,而非后来添加的,当然我们也可以添加虚拟用户;每个虚拟用户都有自己的功能,所以才有
cat /etc/passwd
里面那么多用户。 - 普通真实用户:这类用户能登录系统,但只能操作自己家目录的内容;权限有限;这类用户都是系统管理员自行添加的;
查看可用 shell 命令
[root@localhost ~]# chsh -l
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh