From bd1784ed63880415abef30eea51f4d7aed5715e8 Mon Sep 17 00:00:00 2001 From: lin <2312289931@qq.com> Date: Fri, 14 Jun 2024 19:01:13 +0800 Subject: [PATCH] 20240614 --- ...67\344\270\216\346\235\203\351\231\220.md" | 312 ++++++++++++++++++ 1 file changed, 312 insertions(+) create mode 100644 "08\346\236\227\350\212\263\345\251\225/20240614\347\254\224\350\256\260-\347\224\250\346\210\267\344\270\216\346\235\203\351\231\220.md" diff --git "a/08\346\236\227\350\212\263\345\251\225/20240614\347\254\224\350\256\260-\347\224\250\346\210\267\344\270\216\346\235\203\351\231\220.md" "b/08\346\236\227\350\212\263\345\251\225/20240614\347\254\224\350\256\260-\347\224\250\346\210\267\344\270\216\346\235\203\351\231\220.md" new file mode 100644 index 0000000..eb0382d --- /dev/null +++ "b/08\346\236\227\350\212\263\345\251\225/20240614\347\254\224\350\256\260-\347\224\250\346\210\267\344\270\216\346\235\203\351\231\220.md" @@ -0,0 +1,312 @@ +# 用户与权限 +一、用户与用户组 +```html + +1.用户管理 + - 添加用户 + (1).useradd 原生底层命令 + (2).adduser 加强版命令 + + - 删除用户 + (1).userdel -r 删除用户和家目录和邮件目录 + + - 修改用户 + (1).usermod + (2).usermod -G xxx 将xxx用户修改为.... + (3).usermod -aG xxx 给xxx用户追加一个附加组 + + - 查看用户信息 + (1).id 用户名 + (2).passwd + + - passwd+用户名:给这个用户名设置密码 + + +2.用户组管理 + - 添加用户组 + (1).groupadd + + - 删除用户组 + (1).groupdel + + - 修改用户组 + (1).groupmod +``` + +二、文件与目录和权限 +```html +1.基本权限 + (1).读:read,简写r:对于文件,是可以读取文件内容 + 对于目录,是可以浏览目录信息 + + (2).写:write,简写w:对于文件,是可以修改文件的内容 + 对于目录,可以在里面创建,修改,删除文件或目录 + + (3).执行:execute,简写x.对于文件,执行该文件,对于 + 目录,是可以进入该目录 + +2.权限的表达方式 + (1).字符:r w x + + (2).数字:4 读 r + 2 写 w + 1 执行 x + +3.权限管理的命令 + (1).修改权限 + - chmod + 数字权限表示 + 文件名 + - 例:chmod 777 1.txt + + - chmod + 字符权限表示 + 文件名 + - 例:chmod u=rx,g=wx 1.txt + + (2). + - u:拥有者 + - g:拥有者所在的组 + - o:除以上两者外的其他人 + - a:所有人 + - +:不改变原有权限基础上添加新权限 + - —:不改变原有权限基础上删除某权限 + - =:覆盖所有旧权限 + +4.修改拥有者 + (1).chown + +5.修改拥有者的组 + (1).chgrp + +6.通用的一个常用的选项 -R 递归设置权限 + +``` + + + + + +### 练习 + +### 权限管理练习 + +1. 创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) + +```html + hhs@hecs-328451:~$ sudo mkdir -p /guanli/{zonghe,jishu} + ``` + + + +2. 添加组帐号zonghe、caiwu、jishu,GID号分别设置为2001、2002、2003 + + ```html + hhs@hecs-328451:~$ sudo groupadd -g 2001 zonghe + hhs@hecs-328451:~$ sudo groupadd -g 2002 caiwu + hhs@hecs-328451:~$ sudo groupadd -g 2003 jishu + hhs@hecs-328451:~$ cat /etc/group + ``` + + + +3. 创建jerry、kylin、tsengia、obama用户,其中的kylin用户帐号在2020年12月30日后失效 + + ```html + hhs@hecs-328451:~$ sudo useradd jerry + hhs@hecs-328451:~$ sudo useradd -e 2020-12-30 kylin + hhs@hecs-328451:~$ sudo useradd tsengia + hhs@hecs-328451:~$ sudo useradd obama + hhs@hecs-328451:~$ cat /etc/passwd +``` + + + +4. 将jerry、kylin、tsengia、obama等用户添加到zonghe组内 + +```html +hhs@hecs-328451:~$ sudo usermod -aG zonghe jerry +hhs@hecs-328451:~$ sudo usermod -aG zonghe kylin +hhs@hecs-328451:~$ sudo usermod -aG zonghe tsengia +hhs@hecs-328451:~$ sudo usermod -aG zonghe obama +hhs@hecs-328451:~$ cat /etc/group +``` + + + +5. 创建handy、cucci用户,其中cucci帐号的登录Shell设置为“/sbin/nologin” + +```html +hhs@hecs-328451:~$ sudo useradd handy +hhs@hecs-328451:~$ sudo useradd -s /sbin/nologin cucci +hhs@hecs-328451:~$ cat /etc/passwd +``` + + + +6. 将handy、cucci等用户添加到jishu组内 + +```html +hhs@hecs-328451:~$ sudo usermod -aG jishu handy +hhs@hecs-328451:~$ sudo usermod -aG jishu cucci +hhs@hecs-328451:~$ cat /etc/group +``` + + + +7. 将上述的所有用户均要求加入到guanli组内 + +```html +hhs@hecs-328451:~$ sudo groupadd guanli +hhs@hecs-328451:~$ sudo gpasswd -M jerry,kylin,tsengia,obama,handy,cucci guanli +``` + + + +8. 将zonghe组内的obama用户删除 + + ```html +hhs@hecs-328451:~$ sudo gpasswd -d obama zonghe +Removing user obama from group zonghe +``` + + + +9. 为jerry用户设置密码为“123456”(使用普通方法)为cucci用户设置密码为“redhat” + +```html +hhs@hecs-328451:~$ sudo passwd jerry +New password: +Retype new password: +passwd: password updated successfully + ``` + ```html +hhs@hecs-328451:~$ sudo passwd cucci +New password: +Retype new password: +passwd: password updated successfully + + ``` + + + +10. 将jerry用户锁定,并查看锁定状态 + +```html +hhs@hecs-328451:~$ sudo passwd -l jerry +passwd: password expiry information changed. +hhs@hecs-328451:~$ sudo passwd -S jerry +jerry L 06/14/2024 0 99999 7 -1 + + ``` + + + +11. 打开两个xshell窗口,通过(who 或者 w)命令查看连接状态,并通过fuser杀掉其中一个 + +```html +hhs@hecs-328451:~$ sudo apt-get update +hhs@hecs-328451:~$ sudo apt-get install psmisc +hhs@hecs-328451:~$ who +hhs@hecs-328451:~$ fuser -k /dev/pts/1 +/dev/pts/1: 256429 256473 + +``` + + + +12. 查看cucci用户,属于那些组,并查看其详细信息 + + ```html +hhs@hecs-328451:~$ groups cucci + +hhs@hecs-328451:~$ sudo apt-get update +hhs@hecs-328451:~$ sudo apt-get install finger +hhs@hecs-328451:~$ finger cucci + ``` + + + +13. 手工创建账号student(预留) + +```html +hhs@hecs-328451:~$ sudo useradd student +hhs@hecs-328451:~$ sudo passwd student + + ``` + + + +14. 设置权限及归属:/guanli目录属组设为guanli, /guanli/zonghe目录的属组设为zonghe /guanli/jishu目录的属组设为jishu,设置3个目录都是禁止其他用户访问的权限 + ```html +hhs@hecs-328451:~$ sudo chown .guanli /guanli +hhs@hecs-328451:~$ sudo chown .zonghe /guanli/zonghe/ +hhs@hecs-328451:~$ sudo chown .jishu /guanli/jishu/ +hhs@hecs-328451:~$ sudo chmod -R a-r /guanli +hhs@hecs-328451:~$ ls -l /guanli/ + ``` + + + +15. 建立公共目录/ceshi允许技术组内的所有用户读取、写入、执行文件, 禁止其他用户读、写、执行 + +```html +hhs@hecs-328451:~$ sudo mkdir /ceshi +hhs@hecs-328451:~$ sudo chown .jishu /ceshi/ +hhs@hecs-328451:~$ sudo chmod 770 /ceshi/ +hhs@hecs-328451:~$ ls -ld /ceshi +drwxrwx--- 2 root jishu 4096 Jun 14 18:36 /ceshi + + ``` + + + +16. 清除jerry用户密码 + + ```html +hhs@hecs-328451:~$ sudo passwd -d jerry +passwd: password expiry information changed. + ``` + + + +17. 锁定cucci用户密码并查看状态 + + ```html +hhs@hecs-328451:~$ sudo passwd -l cucci +passwd: password expiry information changed. +hhs@hecs-328451:~$ sudo passwd -S cucci +cucci L 06/14/2024 0 99999 7 -1 + ``` + + + +18. 修改obama用户的UID为8888 + ```html +hhs@hecs-328451:~$ id obama +uid=1020(obama) gid=1020(obama) groups=1020(obama),2004(guanli) +hhs@hecs-328451:~$ sudo usermod -u 8888 obama +hhs@hecs-328451:~$ id obama +uid=8888(obama) gid=1020(obama) groups=1020(obama),2004(guanli) + ``` + + + +19. 通过passwd命令修改kylin用户的最长密码使用期限为60天 + +```html +hhs@hecs-328451:~$ sudo passwd -x 60 kylin +hhs@hecs-328451:~$ sudo cat /etc/shadow |grep "kylin" +kylin:!:19888:0:60:7::18626: +``` + + + + + +20. 通过id groups等命令查看用户handy信息 + +```html +hhs@hecs-328451:~$ sudo id handy +uid=1021(handy) gid=1021(handy) groups=1021(handy),2003(jishu),2004(guanli) +hhs@hecs-328451:~$ sudo groups handy +handy : handy jishu guanli +``` + + \ No newline at end of file -- Gitee