diff --git "a/14 \346\262\210\346\231\223\347\220\263/20240614 \347\224\250\346\210\267\345\222\214\347\224\250\346\210\267\347\273\204\347\256\241\347\220\206.md" "b/14 \346\262\210\346\231\223\347\220\263/20240614 \347\224\250\346\210\267\345\222\214\347\224\250\346\210\267\347\273\204\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..aee8d8210044e79e64c1fe07f552d05619a42ec9 --- /dev/null +++ "b/14 \346\262\210\346\231\223\347\220\263/20240614 \347\224\250\346\210\267\345\222\214\347\224\250\346\210\267\347\273\204\347\256\241\347\220\206.md" @@ -0,0 +1,260 @@ +## 用户和用户组管理 + +- 用户管理 + - 添加账户 useradd -m -s /bin/bash db2 + - 删除账户 userdel -r db2 + - 修改账户 usermod [选项同添加账户] db2 + - 查看帐户 getent passwd db2 +- 用户组管理 + - 添加用户组 groupadd stu1 + - 删除用户组 groupdel stu1 + - 添加用户到用户组 usermod -aG stu1 db2 +- 密码管理 + - 设置密码 passwd +- sudo权限管理 + - usermod -aG sudo xxx + - 修改/etc/sudoers文件 +- 登录权限管理 +- 用户环境管理 + - 用户配置文件 : .bashrc + - 用户环境变量: $PATH、$HOME、$SHELL + +## 文件与目录的权限管理 + +- https://www.runoob.com/linux/linux-file-attr-permission.html + + ![20240605092950](./assets/20240605092950.png) + +- 文件类型 + - -普通文件 + - d目录 + - l连接文件 + - b c s p 等其它文件 + +- 基本权限 + - r 读 + - w 写 + - x 执行 + +- 权限表示 + - 字符表示 + - 数字表示 + - 4 代表 r + - 2 代表 w + - 1 代表 x + - 权限修改 + - chmod 修改权限 + - chown 修改拥有者 + - chgrp 修改所属组 + - 权限的继承 + - 目录权限下文件和目录的影响 + - umask + - 特权权限 + - SUID + - SGID + - SBIT + - 隐藏权限 + - i + - 用在文件上,无法对该文件进行任何修改 + - 用在目录上,无法在里面创建新文件,但可以修改已有文件 + - a + - 除了可追加内容外,无法进行其它修改 + - 隐藏权限的操作 + - 修改隐藏权限 chattr + - chattr +i XXX + - chattr -i XXX + - 查看隐藏权限 + - lsattr + + + +### 权限管理练习 + +1. 创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) + +```bash +root@hecs-90883:~# mkdir guanli/zonghe guanli/jishu -p + +``` + + + +1. 添加组帐号zonghe、caiwu、jishu,GID号分别设置为2001、2002、2003 + +```bash +root@hecs-90883:~/guanli# groupadd -g 2001 zonghe +root@hecs-90883:~/guanli# groupadd -g 2002 caiwu +root@hecs-90883:~/guanli# groupadd -g 2003 jishu + +``` + + + +1. 创建jerry、kylin、tsengia、obama用户,其中的kylin用户帐号在2020年12月30日后失效 + +```bash +root@hecs-90883:~# useradd jerry | useradd -e 2020-12-30 kylin | useradd tsengia | useradd obama + +``` + + + +1. 将jerry、kylin、tsengia、obama等用户添加到zonghe组内 + +```bash +root@hecs-90883:~# gpasswd -M jerry,kylin,tsengia,obama zonghe + +``` + + + +1. 创建handy、cucci用户,其中cucci帐号的登录Shell设置为“/sbin/nologin” + +```bash +root@hecs-90883:~# useradd handy |useradd -s /sbin/nologin cucci + +``` + + + +1. 将handy、cucci等用户添加到jishu组内 + +```bash +root@hecs-90883:~# gpasswd -M handy,cucci jishu + +``` + + + +1. 将上述的所有用户均要求加入到guanli组内 + +```bash +root@hecs-90883:~# groupadd guanli +root@hecs-90883:~# gpasswd -M handy,cucci,jerry,kylin,tsengia,obama guanli + +``` + + + +1. 将zonghe组内的obama用户删除 + +```bash + +root@hecs-90883:~# gpasswd -d obama zonghe + +``` + +1. 为jerry用户设置密码为“123456”(使用普通方法)为cucci用户设置密码为“redhat” + +```bash +root@hecs-90883:~# passwd jerry +root@hecs-90883:~# passwd cucci + +``` + + + +1. 将jerry用户锁定,并查看锁定状态 + +```bash +root@hecs-90883:~# passwd -l jerry + +root@hecs-90883:~# passwd -S jerry + +``` + + + +1. 打开两个xshell窗口,通过(who 或者 w)命令查看连接状态,并通过fuser杀掉其中一个 + +```bash +root@hecs-90883:~# fuser -k /dev/pts/2 + +``` + + + +1. 查看cucci用户,属于那些组,并查看其详细信息 + +```bash +root@hecs-90883:~# id cucci + +``` + + + +1. 手工创建账号student(预留) + +```bash +useradd student? +``` + +1. 设置权限及归属:/guanli目录属组设为guanli, /guanli/zonghe目录的属组设为zonghe /guanli/jishu目录的属组设为jishu,设置3个目录都是禁止其他用户访问的权限 + +```bash +root@hecs-90883:~# chown .guanli guanli +root@hecs-90883:~# chown .zonghe guanli/zonghe +root@hecs-90883:~# chown .jishu guanli/jishu + +root@hecs-90883:~# chmod -R a-r guanli +root@hecs-90883:~# ls -l guanli + +``` + + + +1. 建立公共目录/ceshi允许技术组内的所有用户读取、写入、执行文件, 禁止其他用户读、写、执行 + +```bash +root@hecs-90883:~# mkdir ceshi +root@hecs-90883:~# chmod 770 ceshi + +``` + + + +1. 清除jerry用户密码 + +```bash +root@hecs-90883:~# passwd -d jerry + +``` + + + +1. 锁定cucci用户密码并查看状态 + +```bash +root@hecs-90883:~# passwd -l cucci +root@hecs-90883:~# passwd -S cucci + +``` + + + +1. 修改obama用户的UID为8888 + +```bash +root@hecs-90883:~# usermod -u 8888 obama + +``` + + + +1. 通过passwd命令修改kylin用户的最长密码使用期限为60天 + +```bash +root@hecs-90883:~# passwd -x 60 kylin + +``` + + + +1. 通过id groups等命令查看用户handy信息 + +```bash +root@hecs-90883:~# id handy +root@hecs-90883:~# groups handy + +``` +