文章目录
1、用户及用户组存在的意义
1)用户存在的意义
系统资源是有限的,如何合理分配系统资源?
在这个问题解决时必须要有连个资源配合
1.身份 account
2.授权 author
3.认证 auth
3A机制,3A机制组成系统中最底层的安全架构
2)用户组存在意义
用户组是一个逻辑容器
对用户进行归类和统一授权
2、用户及用户组在系统中的存在方式
电脑对数字敏感 ------------ id
人类对字符串敏感 ---------名称
- id <-------> 名称 必须要记录到文件当中用户才能存在
- 用户就是/etc/passwd文件中的一行字符
- 用户组存在的方式就是/etc/group 文件中的一行字符
3、用户切换
1)用户查看
*whoami ##查看当前用户
*id ##查看用户id信息
-u ##查看用户的用户id
-g ##查看用户主组id
-G ##查看用户所有的组的id
-n ##显示名称
[westos@localhost Desktop]$ whoami
westos
[westos@localhost Desktop]$ id -u westos
1000
[westos@localhost Desktop]$ id -g westos
1000
[westos@localhost Desktop]$ id -G westos
1000
[westos@localhost Desktop]$ id westos
uid=1000(westos) gid=1000(westos) groups=1000(westos)
[westos@localhost Desktop]$ id -G -n westos
westos
2)用户切换
su - username
- #切换用户环境
username ##如果root ----> commonuser 不需要后者密码
##commonuser ----> root 需要密码
##commonuser ----> commonuser 需要密码
#
#注意:在做用户切换时当使用完毕用户身份及时退出
#不要在一个shell中反复执行su命令
#在一个shell中反复执行su命令会导致环境错乱
[westos@localhost Desktop]$ su -
Password:
[root@localhost ~]# su - westos
[westos@localhost ~]$ su - westos
Password:
[westos@localhost ~]$ pwd
/home/westos
[westos@localhost ~]$ su -
Password:
[root@localhost ~]# pwd
/root
[root@localhost ~]# su westos
[westos@localhost root]$ pwd
/root
4、用户涉及到的系统配置文件
/etc/passwd ##用户身份信息文件
##用户名称:用户密码:用户id:用户主组id:用户说明:用户家目录:用户默认shell
/etc/group ##组身份信息文件
#组名称:组密码:组id:组的附加成员
/etc/skel/.* ##用户环境配置文件模板
/etc/shadow ##用户认证信息文件
/home/username ##用户家目录
用户 密码 uid gid 用户说明: 家目录 用户默认shell
root :x :0 :0 :super user : :/root: /bin/bash
组名称:组密码:组id:
root: x :0:组的附加成员
5、用户和用户组建立及删除
监控用户建立的命令
watch -n 1 “tail -n 4 /etc/passwd /etc/group;echo =======;ls -l /home”
useradd username ##用户建立
-u id username ##uid 2**16=0-65535
##0 表示超级用户
##1-200 系统预留id
##201-999 系统用户
##1000-60000 用户级用户
##/etc/login.defs 记录用户建立的默认规则
-g id username ##主组id
-G id username ##附加组id
-d dir username ##指定用户家目录
-c word username ##指定用户说明
-s shell username ##指定用shell
userdel -r username ##用户删除 -r 删除用户的系统配置文件
groupadd groupname ##组建立
-g id groupname ##指定组名称
groupdel groupname ##组删除
[root@localhost ~]# useradd linux
[root@localhost ~]# userdel -r linux
[root@localhost ~]# groupadd linux
[root@localhost ~]# groupdel linux
[root@localhost ~]# groupadd -g 666 linux
[root@localhost ~]# groupdel linux
[root@localhost ~]# useradd -u 666 linux
[root@localhost ~]# userdel -r linux
[root@localhost ~]# groupadd -g 666 linux
[root@localhost ~]# useradd -g 666 linux
[root@localhost ~]# userdel -r linux
[root@localhost ~]# useradd -G 89 linux
[root@localhost ~]# id linux
uid=1001(linux) gid=1001(linux) groups=1001(linux),89(postfix)
[root@localhost ~]# userdel -r linux
[root@localhost ~]# useradd -G 89,90 linux
[root@localhost ~]# id linux
uid=1001(linux) gid=1001(linux) groups=1001(linux),90(postdrop),89(postfix)
[root@localhost ~]# userdel -r linux
[root@localhost ~]# useradd -c "linux user" linux
[root@localhost ~]# userdel -r linux
[root@localhost ~]# useradd -M linux
[root@localhost ~]# userdel -r linux
[root@localhost ~]# useradd -d /home/haha linux
[root@localhost ~]# userdel -r linux
[root@localhost ~]# useradd -s /sbin/nologin linux
[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
6、用户和用户组的信息管理
usermod
-l #更改用户名称
-u #更改用户id
-g #更改主组id
-G #更改用户附加组身份
-aG #添加用户附加组身份
-c #更改用户说明
-d #更改家目录指向
-md #更改家目录指向同时更改家目录名称
-s #更改默认shell
-L #冻结账号
-U #解锁
-M #不指定家目录
groupmod -g #更改用户组id
[root@localhost ~]# groupmod -g 1001 linux
[root@localhost ~]# usermod -l redhat linux
[root@localhost ~]# usermod -l linux redhat
[root@localhost ~]# usermod -u 666 linux
[root@localhost ~]# usermod -u 1001 linux
[root@localhost ~]# usermod -g 89 linux
[root@localhost ~]# usermod -g 1001 linux
[root@localhost ~]# usermod -G 1001 linux
[root@localhost ~]# usermod -G 89 linux
[root@localhost ~]# id linux
uid=1001(linux) gid=1001(linux) groups=1001(linux),89(postfix)
[root@localhost ~]# usermod -aG 90 linux
[root@localhost ~]# id linux
uid=1001(linux) gid=1001(linux) groups=1001(linux),90(postdrop),89(postfix)
[root@localhost ~]# usermod -G "" linux
[root@localhost ~]# id linux
uid=1001(linux) gid=1001(linux) groups=1001(linux)
[root@localhost ~]# usermod -c "linux user" linux
[root@localhost ~]# usermod -c "" linux
[root@localhost ~]# usermod -d /home/lee linux
[root@localhost ~]# usermod -d /home/linux linux
[root@localhost ~]# usermod -md /home/lee linux
[root@localhost ~]# usermod -md /home/linux linux
[root@localhost ~]# usermod -s /sbin/nologin linux
[root@localhost ~]# usermod -s /bin/bash linux
7、用户认证信息管理
1) 用户名称
passwd -S lee ##查看密码状态
[root@localhost Desktop]# watch -n 1 "tail -n 3 /etc/passwd /etc/shadow;passwd -S westoslinux"
[root@localhost westoslinux]# passwd -S westoslinux
westoslinux PS 2020-12-16 0 99999 7 -1 (Password set, SHA512 crypt.)
[root@localhost westoslinux]# passwd --status westoslinux
westoslinux PS 2020-12-16 0 99999 7 -1 (Password set, SHA512 crypt.)
2) 用户加密字符
- 更改密码
passwd lee ##只有root可以执行 “echo 123 | passwd --stdin lee”
passwd ##普通用户改密码
Changing password for user lee.
Current password: #输入原始密码
New password: ##输入新密码(8位以上无序数字+无序字母组合)
Retype new password: ##重复输入
passwd: all authentication tokens updated successfully.
[root@localhost Desktop]# useradd westoslinux
[root@localhost Desktop]# passwd westoslinux
Changing password for user westoslinux.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
[root@localhost westoslinux]# echo 123 | passwd --stdin westoslinux
Changing password for user westoslinux.
passwd: all authentication tokens updated successfully.
[root@localhost westoslinux]# passwd << EOF
> lee
> lee
> EOF
Changing password for user root.
New password: BAD PASSWORD: The password is shorter than 8 characters
Retype new password: passwd: all authentication tokens updated successfully.
[root@localhost Desktop]# su - westoslinux
[westoslinux@localhost ~]$ passwd
Changing password for user westoslinux.
Current password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[westoslinux@localhost ~]$
- 冻结认证
passwd -l lee ##冻结账号认证
passwd -u lee ##解锁账号认证
usermod -L #冻结账号
usermod -U #解锁
[root@localhost westoslinux]# usermod -L westoslinux
[root@localhost westoslinux]# usermod -u westoslinux
[root@localhost westoslinux]# passwd -l westoslinux
Locking password for user westoslinux.
passwd: Success
[root@localhost westoslinux]# passwd -u westoslinux
Unlocking password for user westoslinux.
passwd: Success
- 密码删除
passwd -d lee
#密码使用天数
*从1970-1-1算其到今天的时间
passwd -e lee ##修改默认使用时间为0
chage -d 0 lee ##账号必须改密码才能登陆系统
#密码最短有效期
passwd -n 1 lee ##lee在1天内不能改密码
chage -m 1 lee
#密码最长有效期
passwd -x 40 lee ##40天内lee用户必须更新密码否则会被冻结
chage -M 30 lee
#密码过期警告
passwd -w 2 lee ##账号过期前警告时间
chage -W 1 lee
#认证非活跃天数
passwd -i 2 lee ##账号认证最大时间超过后还能用多久
chage -I 1 lee
#账号认证到期时间
chage -E “2020-05-11” ##到2020-5-11这天账号会被冻结
#未启用功能
[root@localhost westoslinux]# passwd -d westoslinux
Removing password for user westoslinux.
passwd: Success
[root@localhost westoslinux]# passwd -e westoslinux
Expiring password for user westoslinux.
passwd: Success
[root@localhost westoslinux]# chage -d 11 westoslinux
[root@localhost westoslinux]# passwd -n 1 westoslinux
Adjusting aging data for user westoslinux.
passwd: Success
[root@localhost westoslinux]# chage -m 0 westoslinux
[root@localhost westoslinux]# passwd -x 30 westoslinux
Adjusting aging data for user westoslinux.
passwd: Success
[root@localhost westoslinux]# chage -M 40 westoslinux
[root@localhost westoslinux]# chage -M 9999 westoslinux
[root@localhost westoslinux]# passwd -w 2 westoslinux
Adjusting aging data for user westoslinux.
passwd: Success
[root@localhost westoslinux]# chage -W 7 westoslinux
[root@localhost westoslinux]# passwd -i 1 westoslinux
Adjusting aging data for user westoslinux.
passwd: Success
[root@localhost westoslinux]# chage -I 2 westoslinux
[root@localhost westoslinux]# chage -E "2020-12-30" westoslinux
在系统中完成以下用户操作
1.建立用户组shengchan,caiwu,jishu并满足以下要
- shengchan组id为8000
- caiwu组id为8001
- jishu组id为8002
2.建立westos,linux,lee,westosadmin等用户完成以下要求
- westos用户的附加组为shengchan和jishu
- lee的主组为caiwu附加组为技术,lee的uid和gid必须一致
- linux为系统账号不能直接被操作者使用
- westosamdin用户不属于以上三个部门,但是可以在系统中自由的管理用户
3.以上用户密码均为westos,并要求用户首次登陆时强制修改密码
- 设定以上用户密码必须在30天内进行休改,并在过期前2天发出警告求
4.建立目录完成以下要求
- 新建目录/sc /cw /js /pub
- /sc目录是生产部门的数据存储目录,只能被生产部门的人员读写,并且在sc中建立的文件都属于生产部门
- /cw目录是财务部门的数据存储目录,只能被财务部门的人员读写,并且在cw中建立的文件都属于财务部门
- /js目录是技术部门的数据存储目录,只能被技术部门的人员读写,并且在js中建立的文件都属于技术部门
- /pub为公司人员公共目录,可以被公司任何员工读写.但是只能删除自己的文件
- westosadmin用户可以对/sc /cw /js /pub做任何操作
[root@localhost Desktop]# groupadd -g 8000 shengchan
[root@localhost Desktop]# groupadd -g 8001 caiwu
[root@localhost Desktop]# groupadd -g 8002 jishu
[root@localhost Desktop]# useradd -G shengchan,jishu westosuser
[root@localhost Desktop]# useradd -u 8001 -g caiwu -G jishu lee
[root@localhost Desktop]# useradd -s /sbin/nologin linux
[root@localhost Desktop]# echo "westosadmin:x:0:0:/home/westosadmin:/bin/bash " >> /etc/passwd
[root@localhost Desktop]# id westosadmin
uid=0(root) gid=0(root) groups=0(root)
[root@localhost Desktop]# echo westos | passwd --stdin westosuser
[root@localhost Desktop]# echo westos | passwd --stdin lee
[root@localhost Desktop]# echo westos | passwd --stdin linux
[root@localhost Desktop]# echo westos | passwd --stdin westosadmin
[root@localhost Desktop]# passwd -e westosuser
[root@localhost Desktop]# passwd -e lee
[root@localhost Desktop]# passwd -e linux
[root@localhost Desktop]# passwd -e westosadmin
[root@localhost Desktop]# passwd -x 30 -w 2 westosuser
[root@localhost Desktop]# passwd -x 30 -w 2 lee
[root@localhost Desktop]# passwd -x 30 -w 2 linux
[root@localhost Desktop]# passwd -x 30 -w 2 westosadmin
[root@localhost Desktop]# mkdir /sc /cw /js /pub
[root@localhost Desktop]# chgrp shengchan /sc
[root@localhost Desktop]# chgrp caiwu /cw
[root@localhost Desktop]# chgrp jishu /js
[root@localhost Desktop]# chmod 770 /sc /cw /js
[root@localhost Desktop]# chmod 777 /pub/
[root@localhost Desktop]# chmod o+t /sc /cw /js /pub
[root@localhost Desktop]# chmod g+s /sc /cw /js
[root@localhost Desktop]# setfacl -m u:westosadmin:rwx /sc /cw /js
8、用户权力下放
#在系统中普通用户时无法执行系统管理命令的
#如果需要普通用户执行系统管理动作那么需要
#root用户来进行授权
普通用户授权方式 “sudo”
作用:
可以使普通用户使用指定的用户身份运行命令
授权方法:
visudo ##此命令作用是编辑/etc/sudoers并提供语法检测
在文件的100行左右 ##代码规范性
username hostname=(newusername) [NOPASSWD:] /command, /command1
#lee用户 在linux.wesots.com主机=(用超级用户身份) 执行useradd命令
lee linux.westos.com=(root) /usr/sbin/useradd
#westos用户 在linux.wesots.com 使用超户 免密 执行useradd 和 userdel
westos linux.wesots.com = (root) NOPASSWD: /usr/sbin/useradd, /usr/sbin/userdel
[root@localhost mnt]# hostname
localhost.localdomain
[root@localhost Desktop]# visudo
>>> /etc/sudoers: syntax error near line 101 <<<
What now? e
101 westosadmin localhost.localdomain=(root) NOPASSWD: /usr/sbin/useradd, /usr/sbin/userdel
[root@localhost Desktop]# su - westosadmin
[westosadmin@localhost ~]$ sudo useradd hello
[westosadmin@localhost ~]$ id hello
uid=1004(hello) gid=1004(hello) groups=1004(hello)