diff --git "a/04\351\231\210\345\277\203\350\214\271/2024.6.14\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232-\347\224\250\346\210\267\344\270\216\346\235\203\351\231\220.md" "b/04\351\231\210\345\277\203\350\214\271/2024.6.14\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232-\347\224\250\346\210\267\344\270\216\346\235\203\351\231\220.md" new file mode 100644 index 0000000000000000000000000000000000000000..74b63ff9bdadb9031fe97a40d8ab596934b5c13b --- /dev/null +++ "b/04\351\231\210\345\277\203\350\214\271/2024.6.14\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232-\347\224\250\346\210\267\344\270\216\346\235\203\351\231\220.md" @@ -0,0 +1,482 @@ +## 2024.6.14笔记-用户与权限 + +#### 用户和用户组管理 + +- 用户管理 + + - 添加账户 useradd -m -s /bin/bash 用户名 + + -m 是添加以用户名为主的家目录,-s指明用户的默认shell程序,可用列表在/etc/shells文件中 + + + + - 删除账户 userdel -r 用户名 + + 删除用户,家,邮件目录,-R是递归的删除 + + + + - 修改账户 usermod [选项同添加账户] 用户名 + + + + - 查看帐户 getent passwd 用户名 + + + +- 用户组管理 + + - 添加用户组 groupadd 用户组 + + - 删除用户组 groupdel 用户组 + + - 添加用户到用户组 usermod -aG 用户组 用户名 + + 1.usermod -aG 组名 用户名 给用户追加一个附加组 + + 2.usermod -G 组名 用户名 将用户的组名修改,清空 + +- 密码管理 + + - 设置密码 passwd + +- sudo权限管理 + + - usermod -aG sudo xxx + - 修改/etc/sudoers文件 + +- 登录权限管理 + +- 用户环境管理 + + - 用户配置文件 : .bashrc + - 用户环境变量: $PATH、$HOME、$SHELL + +#### 文件与目录的权限管理 + +- https://www.runoob.com/linux/linux-file-attr-permission.html + +![alt text](截图/2024.6.14截图1.png) + +- 文件类型 + + - -普通文件 + - d目录 + - l连接文件(软连接) + - b c s p 等其它文件 + +- 基本权限 + + - r 读 + - w 写 + - x 执行 + +- 权限表示 + + - 字符表示 + + 1.+ + + 2.— + + 3.= + + 加和减不改变原有权限,=直接覆盖原有权限 + + - 数字表示 + + - 4 代表 r + - 2 代表 w + - 1 代表 x + + - 权限修改 + + - chmod 修改权限 + + - chown 修改拥有者 + + - chgrp 修改所属组 + + 通常有一个常用选项 -R递归设置权限 + + - 权限的继承 + + - 目录权限下文件和目录的影响 + - umask + + - 特权权限 + + - SUID + - SGID + - SBIT + + - 隐藏权限 + + - i + - 用在文件上,无法对该文件进行任何修改 + - 用在目录上,无法在里面创建新文件,但可以修改已有文件 + - a + - 除了可追加内容外,无法进行其它修改 + - 隐藏权限的操作 + - 修改隐藏权限 chattr + - chattr +i XXX + - chattr -i XXX + - 查看隐藏权限 + - lsattr + + + +### 权限管理练习 + + + +1. 创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) + + ``` + rue@hecs-342311:~$ mkdir -p guanli && cd guanli && mkdir zonghe jishu + rue@hecs-342311:~/guanli$ ls + jishu zhonghe + ``` + +2. 添加组帐号zonghe、caiwu、jishu,GID号分别设置为2001、2002、2003 + + ``` + rue@hecs-342311:~/guanli$ sudo groupadd -g 2001 zonghe + rue@hecs-342311:~/guanli$ sudo groupadd -g 2002 caiwu + rue@hecs-342311:~/guanli$ sudo groupadd -g 2003 jishu + + 查看: + rue@hecs-342311:~$ cat /etc/group + zonghe:x:2001: + caiwu:x:2002: + jishu:x:2003: + ``` + +3. 创建jerry、kylin、tsengia、obama用户,其中的kylin用户帐号在2020年12月30日后失效 + + ``` + root@hecs-342311:~# useradd jerry && useradd -e 2020-12-30 kylin && useradd tsengia && useradd obama + useradd在普通模式下没有权限需要到超级管理员下新增用户 + + 查看新建的用户 + root@hecs-342311:~# cat /etc/passwd + jerry:x:1001:1001::/home/jerry:/bin/sh + kylin:x:1002:1002::/home/kylin:/bin/sh + tsengia:x:1003:1003::/home/tsengia:/bin/sh + obama:x:1004:1004::/home/obama:/bin/sh + ``` + +4. 将jerry、kylin、tsengia、obama等用户添加到zonghe组内 + + ``` + rue@hecs-342311:~$ sudo usermod -aG zonghe jerry + rue@hecs-342311:~$ sudo usermod -aG zonghe kylin + rue@hecs-342311:~$ sudo usermod -aG zonghe tsengia + rue@hecs-342311:~$ sudo usermod -aG zonghe obama + + rue@hecs-342311:~$ getent group zonghe + zonghe:x:2001:jerry,kylin,tsengia,obama + + rue@hecs-342311:~$ id jerry kylin tsengia obama + uid=1001(jerry) gid=1001(jerry) groups=1001(jerry),2001(zonghe) + uid=1002(kylin) gid=1002(kylin) groups=1002(kylin),2001(zonghe) + uid=1003(tsengia) gid=1003(tsengia) groups=1003(tsengia),2001(zonghe) + uid=1004(obama) gid=1004(obama) groups=1004(obama),2001(zonghe) + + ``` + +5. 创建handy、cucci用户,其中cucci帐号的登录Shell设置为“/sbin/nologin” + + ``` + root@hecs-342311:~# useradd handy && useradd cucci && usermod -s /bin/nologin cucci + + 查看handy和cucci用户的信息 + root@hecs-342311:~# id handy + uid=1005(handy) gid=1005(handy) groups=1005(handy) + root@hecs-342311:~# id cucci + uid=1006(cucci) gid=1006(cucci) groups=1006(cucci) + + + root@hecs-342311:~# cat /etc/passwd |awk '$1~/cucci/{print $0}' + cucci:x:1006:1006::/home/cucci:/bin/nologin + + ``` + +6. 将handy、cucci等用户添加到jishu组内 + + ``` + rue@hecs-342311:~$ sudo usermod -aG jishu handy + rue@hecs-342311:~$ sudo usermod -aG jishu cucci + + rue@hecs-342311:~$ getent group jishu + jishu:x:2003:handy,cucci + ``` + +7. 将上述的所有用户均要求加入到guanli组内 + + ``` + rue@hecs-342311:~$ sudo groupadd guanli + rue@hecs-342311:~$ sudo gpasswd -M jerry,kylin,tsengia,obama,handy,cucci guanli + + rue@hecs-342311:~$ getent group guanli + guanli:x:2004:jerry,kylin,tsengia,obama,handy,cucci + ``` + +8. 将zonghe组内的obama用户删除 + + ``` + rue@hecs-342311:~$ sudo gpasswd -d obama zonghe + Removing user obama from group zonghe + + rue@hecs-342311:~$ getent group zonghe + zonghe:x:2001:jerry,kylin,tsengia + rue@hecs-342311:~$ id obama + uid=1004(obama) gid=1004(obama) groups=1004(obama),2004(guanli) + ``` + +9. 为jerry用户设置密码为“123456”(使用普通方法)为cucci用户设置密码为“redhat” + + ``` + rue@hecs-342311:~$ sudo passwd jerry + New password: + Retype new password: + passwd: password updated successfully + + rue@hecs-342311:~$ sudo passwd cucci + New password: + Retype new password: + passwd: password updated successfully + ``` + +10. 将jerry用户锁定,并查看锁定状态 + + ``` + rue@hecs-342311:~$ sudo passwd -l jerry + passwd: password expiry information changed. + + rue@hecs-342311:~$ sudo passwd -S jerry + jerry L 06/14/2024 0 99999 7 -1 + ``` + +11. 打开两个xshell窗口,通过(who 或者 w)命令查看连接状态,并通过fuser杀掉其中一个 + + ``` + rue@hecs-342311:~$ w + 21:07:07 up 3 days, 9:47, 4 users, load average: 0.07, 0.02, 0.00 + USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT + rue pts/0 183.253.241.171 21:06 0.00s 0.01s 0.00s w + rue pts/1 183.253.241.171 21:06 30.00s 0.03s 0.02s top + root pts/2 183.253.241.171 21:06 19.00s 0.00s 0.00s -bash + root pts/3 183.253.241.171 21:06 19.00s 0.01s 0.01s top + rue@hecs-342311:~$ (通过fuser杀死断开连接) + 连接断开 + 连接主机... + 连接主机成功 + + + root@hecs-342311:~# man fuser + No manual entry for fuser + root@hecs-342311:~# fuser -k /dev/pts/0 + -bash: fuser: command not found + (man和fuser找不到命令是因为没有安装furser,下面的命令是安装命令) + root@hecs-342311:~# apt install psmisc + + root@hecs-342311:~# fuser -k /dev/pts/0(-k:kill杀死) + /dev/pts/0: 598249 + + ``` + +12. 查看cucci用户,属于那些组,并查看其详细信息 + + ``` + root@hecs-342311:~# groups cucci(groups查看用户属于那些组) + cucci : cucci jishu guanli + + root@hecs-342311:~# finger cucci(查看详细信息) + Login: cucci Name: + Directory: /home/cucci Shell: /bin/nologin + Never logged in. + No mail. + No Plan. + + root@hecs-342311:~# id cucci + uid=1006(cucci) gid=1006(cucci) groups=1006(cucci),2003(jishu),2004(guanli) + + root@hecs-342311:~# cat /etc/passwd |awk '$1~/cucci/{print $0}' + cucci:x:1006:1006::/home/cucci:/bin/nologin + + ``` + +13. 手工创建账号student(预留) + + [linux手工新建用户(通过文件操作)_shell vim创建用户-CSDN博客](https://blog.csdn.net/qq_44936336/article/details/108236182) + + ``` + 1,修改/etc/passwd + root@hecs-342311:~# vim /etc/passwd + student:x:1007:1007::/home/student:/bin/sh + + 2,修改/etc/shadow + root@hecs-342311:~# vim /etc/shadow + student:!:19888:0:99999:7::: + + 3,修改/etc/group + root@hecs-342311:~# vim /etc/group + student:x:1007 + + 4,新建家目录 + root@hecs-342311:~# mkdir -p /home/student + root@hecs-342311:~# ll -d /home/student + root@hecs-342311:~# id student + uid=1007(student) gid=1007(student) groups=1007(student) + root@hecs-342311:~# cat /etc/passwd |grep 'student' + student:x:1007:1007::/home/student:/bin/sh + root@hecs-342311:~# ls -ld ../home/student + drwxr-xr-x 2 student root 4096 Jun 14 21:52 ../home/student + + 5,复制/etc/skel/.bash* 到家目录 + root@hecs-342311:~# cp /etc/skel/.bash* /home/student(复制环境变量) + root@hecs-342311:~# cd /home/student + root@hecs-342311:/home/student# ls -a + . .. .bash_logout .bashrc + + 6.新建邮箱 + root@hecs-342311:~# cd /var/spool/mail/ + root@hecs-342311:/var/spool/mail# touch student + root@hecs-342311:/var/spool/mail# chown student:mail ./student + root@hecs-342311:/var/spool/mail# ls -ld student + -rw-r--r-- 1 student mail 0 Jun 14 22:26 student + + 7,产生加密密码 + root@hecs-342311:/var/spool/mail# python3 + Python 3.9.2 (default, Feb 28 2021, 17:03:44) + [GCC 10.2.1 20210110] on linux + Type "help", "copyright", "credits" or "license" for more information. + >>> import crypt + >>> crypt.crypt("123","$6$L10ZWFymOSreQvtq") + '$6$L10ZWFymOSreQvtq$XhNDRLvUc2WENr/f8zQ8d/Lp4kw6jCztIr4gwCGwjOKdNOQet3n88ho2iE8DmsM9qXq5RPw3sjsfqCBQ4rogi/' + >>> quit() + + 将得到的密码密文填入/etc/shadow中 + root@hecs-342311:/var/spool/mail# vim /etc/shadow + root@hecs-342311:/var/spool/mail# vim /etc/shadow + root@hecs-342311:/var/spool/mail# vim /etc/shadow + root@hecs-342311:/var/spool/mail# su - student + $ pwd + /home/student + + 远程连接: + 连接主机... + 连接主机成功 + Linux hecs-342311 5.10.0-23-amd64 #1 SMP Debian 5.10.179-1 (2023-05-12) x86_64 + + Welcome to Huawei Cloud Service + + You have no mail. + Last login: Fri Jun 14 22:54:42 2024 from 183.253.241.171 + $ + (虽然能登录但是显示没有mail) + ``` + +14. 设置权限及归属:/guanli目录属组设为guanli, /guanli/zonghe目录的属组设为zonghe /guanli/jishu目录的属组设为jishu,设置3个目录都是禁止其他用户访问的权限 + + ``` + 1./guanli目录属组设为guanli + rue@hecs-342311:~$ sudo chown :guanli ./guanli + rue@hecs-342311:~$ ls -ld guanli + drwxr-xr-x 4 rue guanli 4096 Jun 14 16:36 guanli + + 2. /guanli/zonghe目录的属组设为zonghe + rue@hecs-342311:~$ sudo chown :zonghe ./guanli/zonghe + + 3./guanli/jishu目录的属组设为jishu + rue@hecs-342311:~$ sudo chown :jishu ./guanli/jishu + + 4.设置3个目录都是禁止其他用户访问的权限 + rue@hecs-342311:~$ chmod 750 ./guanli + rue@hecs-342311:~$ chmod 750 guanli/zonghe/ + rue@hecs-342311:~$ chmod 750 guanli/jishu/ + + rue@hecs-342311:~$ ls -ld guanli guanli/jishu/ guanli/zonghe/ + drwxr-x--- 4 rue guanli 4096 Jun 14 16:36 guanli + drwxr-x--- 2 rue rue 4096 Jun 14 11:28 guanli/jishu/ + drwxr-x--- 2 rue zonghe 4096 Jun 14 11:28 guanli/zonghe/ + ``` + +15. 建立公共目录/ceshi允许技术组内的所有用户读取、写入、执行文件, 禁止其他用户读、写、执行 + + ``` + rue@hecs-342311:~$ chmod -R 770 ceshi + rue@hecs-342311:~$ ls -ld ceshi + drwxrwx--- 2 rue rue 4096 Jun 14 23:26 ceshi + ``` + +16. 清除jerry用户密码 + + ``` + rue@hecs-342311:~$ sudo passwd -d jerry + passwd: password expiry information changed. + + 查看密码状态: + rue@hecs-342311:~$ sudo passwd -S jerry + jerry NP 06/14/2024 0 99999 7 -1 + ``` + +17. 锁定cucci用户密码并查看状态 + + ``` + rue@hecs-342311:~$ sudo passwd -l cucci + passwd: password expiry information changed. + + rue@hecs-342311:~$ sudo passwd -S cucci + cucci L 06/14/2024 0 99999 7 -1 + ``` + +18. 修改obama用户的UID为8888 + + ``` + rue@hecs-342311:~$ sudo usermod -u 8888 obama + rue@hecs-342311:~$ id obama + uid=8888(obama) gid=1004(obama) groups=1004(obama),2004(guanli) + ``` + +19. 通过passwd命令修改kylin用户的最长密码使用期限为60天 + + ``` + rue@hecs-342311:~$ sudo passwd -x 60 kylin + passwd: password expiry information changed. + + 查看密码使用期限: + rue@hecs-342311:~$ sudo chage -l kylin + Last password change : Jun 14, 2024 + Password expires : Aug 13, 2024 + Password inactive : never + Account expires : Dec 30, 2020 + Minimum number of days between password change : 0 + Maximum number of days between password change : 60 + Number of days of warning before password expires : 7 + ``` + +20. 通过id groups等命令查看用户handy信息 + +``` +rue@hecs-342311:~$ id handy +uid=1005(handy) gid=1005(handy) groups=1005(handy),2003(jishu),2004(guanli) +rue@hecs-342311:~$ groups handy +handy : handy jishu guanli + +rue@hecs-342311:~$ getent group handy +handy:x:1005: + +rue@hecs-342311:~$ getent passwd handy +handy:x:1005:1005::/home/handy:/bin/sh + +rue@hecs-342311:~$ finger handy +Login: handy Name: +Directory: /home/handy Shell: /bin/sh +Never logged in. +No mail. +No Plan. + +``` + diff --git "a/04\351\231\210\345\277\203\350\214\271/\346\210\252\345\233\276/2024.6.14\346\210\252\345\233\2761.png" "b/04\351\231\210\345\277\203\350\214\271/\346\210\252\345\233\276/2024.6.14\346\210\252\345\233\2761.png" new file mode 100644 index 0000000000000000000000000000000000000000..01bee7f696e7af59d62f091d32a95186c31b30c2 Binary files /dev/null and "b/04\351\231\210\345\277\203\350\214\271/\346\210\252\345\233\276/2024.6.14\346\210\252\345\233\2761.png" differ