Linux安全模型
资源分派:AAA,认证、授权、审计
用户
UID来唯一标识
管理员:root,0普通用户:1-60000自动分配- 系统用户:1-499(centos 6),1-999(centos 7以后)
- 对守护进程获取资源进行权限分配
- 登录用户:500+,1000+,给用户使用
[root@centos7 ~]# id root
uid=0(root) gid=0(root) 组=0(root)
用户组
GID唯一标识
管理员组:root,0普通组:- 系统组:1-499,1-999
- 普通组:500+,1000+,给用户使用
用户和组的关系
primary group:主要组supplementary group:附加组[root@centos7 ~]# id postfix
uid=89(postfix) gid=89(postfix) 组=89(postfix),12(mail)
安全上下文
Context
以root和普通用户身份运行,得到结果不同,取决于运行者身份用户和组的配置文件
/etc/passwd
root:x:0:0:root:/root:/bin/bash
用户名:密码:UID:GID:描述:家目录:shell类型
[root@centos7 ~]# getent passwd root
root:x:0:0:root:/root:/bin/bash
/etc/shadow
[root@centos7 ~]# getent shadow root
root:$6$i/NVboM0BpMFmWlk$uzLpz1xbY8C3IUfcVQRW9i20YNwPnz4IZh8IXYelyq49jIC.Sa2N3RuTgb6AcjOJO/0peipWqvh6m1pC1AfU7.::0:99999:7:::
/etc/group
/etc/gshadow命令
创建用户useradd
* -u UID 指定用户UID
* -o 配合-u,不检查UID的唯一性
* -g GID 指明用户所属主要组,可为组的名称,也可以是GID
* -c COMMENT 用户注释信息
* -d HOME_DIR 以指定路径为家目录
* -s SHELL 指定默认shell,可用列表在/etc/shells中
* -G 为用户指明附加组
* -N 不创建和用户同名的私用组做主组
* -r 创建系统用户
* -m 创建家目录,一般用于系统用户
* -M 不创建家目录,一般用于非系统用户
useradd -r 创建系统用户
/etc/default/useradd 创建用户默认参数[root@centos7 ~]# useradd test
[root@centos7 ~]# id test
uid=1000(test) gid=1000(test) 组=1000(test)
[root@centos7 ~]# useradd -r mysql
[root@centos7 ~]# id mysql
uid=998(mysql) gid=996(mysql) 组=996(mysql)
创建组 groupadd
* -f 当组已经存在时,返回成功
* -g 指定创建组的GID
* -h 显示帮助
* -K 使用指定值,覆盖/etc/login.defs的默认值
* -o 允许使用已存在的GID创建新的组
* -p 使用加密密码
* -r 创建系统组
修改用户信息 usermod
* -c 修改用户描述信息 /etc/passwd 第五个字段
* -d 修改用户家目录
* -e 设置账号有效期 /etc/shadow 第八个字段
* -f 指定天数 /etc/shadow 第七个字段
* -g 修改为指定主组
* -G 修改附加组
* -a 配合-G,增加附加组
* -h 帮助
* -l 修改用户名
* -L 锁定用户
* -m 配合-d,移动家目录数据到新的家目录
* -o 允许使用重复的UID
* -p 使用加密的密码
* -s 修改使用新的SHELL类型
* -u 修改UID
* -U 解锁用户
chsh 修改用户的shell类型
chfn 修改用户的描述信息passwd 修改密码chage 修改和查看用户密码信息修改组信息 groupmod
* -g 修改组的GID
* -n 修改组名
* -o 允许组的GID不唯一
* -p 设置组的加密密码
groupmems
- -g 指定要操作修改那个组
- -a 指定添加一个组的成员
- -d 指定删除一个组的成员
- -l 列出所有组成员
- -p 清空所有的用户
gpasswd 修改组密码
删除用户和用户组
userdel
groupdel练习
1)创建组distro,GID为2019
[root@centos7 etc]# groupadd distro -g 2019
[root@centos7 etc]# getent group distro
distro:x:2019:
2) 创建用户mandriva,ID为1005,基本组为distro
useradd mandriva -u 1005 -g distro
[root@centos7 etc]# getent passwd mandriva
mandriva:x:1005:2019::/home/mandriva:/bin/bash
3) 创建用户mageia,id为1100,家目录为/home/Linux
[root@centos7 etc]# useradd mageia -u 1100 -d /home/Linux
[root@centos7 etc]# getent passwd mageia
mageia:x:1100:1100::/home/Linux:/bin/bash
4) 给用户mageia添加密码,密码为mageedu123,并设置用户密码七天后过期
[root@centos7 etc]# passwd mageia
[root@centos7 etc]# passwd mageia -x 7
[root@centos7 etc]# getent shadow mageia
mageia:$6$Liy5d2Sx$KhAsaEOC3Ad/n3tThhGdpkTQxzn1BNtXdJ3RHr58oD0K03gJMONeCk0ivV86vi0277ac/yHumsMXPmT935kCA.:18560:0:7:7:::
5) 删除mandriva,保留其家目录
[root@centos7 etc]# userdel mandriva
[root@centos7 etc]# ls /home/mandriva/ -dl
drwx------. 2 1005 distro 62 10月 25 09:52 /home/mandriva/
6) 创建用户slackware,id为2002,基本组为distro,附加组为peguin
[root@centos7 etc]# getent group peguin
[root@centos7 etc]# groupadd peguin
[root@centos7 etc]# useradd slackware -u 2002 -g distro -G peguin
[root@centos7 etc]# getent passwd slackware
slackware:x:2002:2019::/home/slackware:/bin/bash
[root@centos7 etc]# getent group peguin
peguin:x:2020:slackware
7) 修改slackware的默认shell 为/bin/tcsh
[root@centos7 etc]# usermod slackware -s /bin/tcsh
[root@centos7 etc]# getent passwd slackware
slackware:x:2002:2019::/home/slackware:/bin/tcsh
8) 为用户slackware新增附加组admins
[root@centos7 etc]# usermod -a -G admins slackware
[root@centos7 etc]# id slackware
uid=2002(slackware) gid=2019(distro) 组=2019(distro),2020(peguin),2021(admins)
显示/etc 目录下,以非字母开头,后面跟了一个字母以及其他任意长度任意字符的文件或目录
[root@centos7 etc]# touch 1
[root@centos7 etc]# ls /etc/[^[:alpha:]]*
/etc/1
复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中
[root@centos7 etc]# mkdir /tmp/mytest1
[root@centos7 etc]# cp -r /etc/p*[^[:digit:]] /tmp/mytest1
[root@centos7 etc]# ll /tmp/mytest1
总用量 28
drwxr-xr-x. 2 root root 4096 10月 25 09:20 pam.d
-rw-r--r--. 1 root root 876 10月 25 09:20 passwd
-rw-r--r--. 1 root root 837 10月 25 09:20 passwd-
drwxr-xr-x. 10 root root 116 10月 25 09:20 pki
drwxr-xr-x. 2 root root 28 10月 25 09:20 plymouth
drwxr-xr-x. 5 root root 52 10月 25 09:20 pm
drwxr-xr-x. 2 root root 6 10月 25 09:20 popt.d
drwxr-xr-x. 2 root root 154 10月 25 09:20 postfix
drwxr-xr-x. 3 root root 123 10月 25 09:20 ppp
drwxr-xr-x. 2 root root 78 10月 25 09:20 prelink.conf.d
-rw-r--r--. 1 root root 233 10月 25 09:20 printcap
-rw-r--r--. 1 root root 1819 10月 25 09:20 profile
drwxr-xr-x. 2 root root 251 10月 25 09:20 profile.d
-rw-r--r--. 1 root root 6545 10月 25 09:20 protocols
drwxr-xr-x. 2 root root 35 10月 25 09:20 python
将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
[root@centos7 etc]# cat /etc/issue
\S
Kernel \r on an \m
[root@centos7 etc]# cat /etc/issue | tr [:lower:] [:upper:] > /tmp/issue.out
[root@centos7 etc]# cat /tmp/issue.out
\S
KERNEL \R ON AN \M
显示/etc 目录下,以非字母开头,后面跟了一个字母以及其他任意长度任意字符的文件或目录
[root@centos7 etc]# touch 1
[root@centos7 etc]# ls /etc/[^[:alpha:]]*
/etc/1
复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中
[root@centos7 etc]# mkdir /tmp/mytest1
[root@centos7 etc]# cp -r /etc/p*[^[:digit:]] /tmp/mytest1
[root@centos7 etc]# ll /tmp/mytest1
总用量 28
drwxr-xr-x. 2 root root 4096 10月 25 09:20 pam.d
-rw-r--r--. 1 root root 876 10月 25 09:20 passwd
-rw-r--r--. 1 root root 837 10月 25 09:20 passwd-
drwxr-xr-x. 10 root root 116 10月 25 09:20 pki
drwxr-xr-x. 2 root root 28 10月 25 09:20 plymouth
drwxr-xr-x. 5 root root 52 10月 25 09:20 pm
drwxr-xr-x. 2 root root 6 10月 25 09:20 popt.d
drwxr-xr-x. 2 root root 154 10月 25 09:20 postfix
drwxr-xr-x. 3 root root 123 10月 25 09:20 ppp
drwxr-xr-x. 2 root root 78 10月 25 09:20 prelink.conf.d
-rw-r--r--. 1 root root 233 10月 25 09:20 printcap
-rw-r--r--. 1 root root 1819 10月 25 09:20 profile
drwxr-xr-x. 2 root root 251 10月 25 09:20 profile.d
-rw-r--r--. 1 root root 6545 10月 25 09:20 protocols
drwxr-xr-x. 2 root root 35 10月 25 09:20 python
将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
[root@centos7 etc]# cat /etc/issue
\S
Kernel \r on an \m
[root@centos7 etc]# cat /etc/issue | tr [:lower:] [:upper:] > /tmp/issue.out
[root@centos7 etc]# cat /tmp/issue.out
\S
KERNEL \R ON AN \M
切换用户或以其他用户身份执行命令
su:switch user
su username 非登录切换,不完全切换su - username 登录切换,完全切换。会读取目标用户的配置信息,并且换切换家目录su - c root COMMAND
sbin/nologin
bin/false
精彩评论