From 2f5f1b4fc2e0d1b5703e2133df745d0936ed5402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=9B=A8=E6=99=B4?= <2137161545@qq.com> Date: Fri, 14 Jun 2024 19:49:22 +0800 Subject: [PATCH] =?UTF-8?q?Linux=E5=86=85=E6=A0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...67\347\273\204\347\256\241\347\220\206.md" | 334 ++++++++++++++++++ 1 file changed, 334 insertions(+) create mode 100644 "49 \345\276\220\351\233\250\346\231\264/240614 \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" diff --git "a/49 \345\276\220\351\233\250\346\231\264/240614 \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/49 \345\276\220\351\233\250\346\231\264/240614 \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 0000000..8e9daa3 --- /dev/null +++ "b/49 \345\276\220\351\233\250\346\231\264/240614 \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,334 @@ +## 用户和用户组管理 + +- 用户管理 + + 超级用户:root 0 0 0 + + 其他用户:1-999 系统预留用户 + + 普通用户:*** 从1000开始递增 + + - 添加账户 useradd -m -s /bin/bash 用户名 + + - -d 目录路径 //指定家目录 + - -g 原生组(与用户名同名) + - -G 附加组 + + - 删除账户 userdel [-r] 用户名 + + - 修改账户 usermod [选项同添加账户] 用户名 + + - 查看帐户 getent passwd 用户名 + + ​ id 用户名 + + !!!查看所有账户 cat /etc/passwd + +- 用户组管理 + + - 添加用户组 groupadd 用户组名 + + - 删除用户组 groupdel 用户组名 + + - 添加用户到用户组 usermod -aG 用户组 用户名 + + !!!查看所有用户组 cat /etc/group + +- 密码管理 + + - 设置密码 passwd + - -l 锁定用户 + - -S 查看状态 + - -d 删除密码 + +- sudo权限管理 + + - 修改为未存在的用户组 usermod -aG sudo xxx + - 修改为已存在的用户组 usermod -og 用户组名 用户名 + - 修改/etc/sudoers文件 + +- 登录权限管理 + + - 查看连接情况 who/w + + - 中断连接 fuser -k /dev/pts/数字 + + !!!需先安装psmisc软件包,才会有fuser + +- 用户环境管理 + + - 用户配置文件 : .bashrc + - 用户环境变量: $PATH、$HOME、$SHELL + +## 文件与目录的权限管理 + +- 文件类型 + - `-` 普通文件 + - d 目录 + - l 连接文件 + - b c s p 等其它文件 +- 基本权限 + - r 读 + - w 写 + - x 执行 +- 权限表示 + - 字符表示 + - u 拥有者 + - g 用户组 + - o 其他人 + - a 所有人 + - 数字表示 + - 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 + lala@hecs-393504:~$ sudo mkdir -p guanli/{zonghe,jishu} + lala@hecs-393504:~$ tree guanli/ + guanli/ + ├── jishu + └── zonghe + ``` + +2. 添加组帐号zonghe、caiwu、jishu,GID号分别设置为2001、2002、2003 + + ```bash + lala@hecs-393504:~$ sudo groupadd -g 2001 zonghe + lala@hecs-393504:~$ sudo groupadd -g 2002 caiwu + lala@hecs-393504:~$ sudo groupadd -g 2003 jishu + lala@hecs-393504:~$ cat /etc/group + ... + zonghe:x:2001: + caiwu:x:2002: + jishu:x:2003: + ``` + +3. 创建jerry、kylin、tsengia、obama用户,其中的kylin用户帐号在2020年12月30日后失效 + + ```bash + lala@hecs-393504:~$ sudo useradd -m -s /bin/bash jerry + lala@hecs-393504:~$ sudo useradd -m -s /bin/bash kylin + lala@hecs-393504:~$ sudo useradd -m -s /bin/bash tsengia + lala@hecs-393504:~$ sudo useradd -m -s /bin/bash obama + lala@hecs-393504:~$ sudo chage -E 2020-12-30 kylin + lala@hecs-393504:/root$ sudo cat /etc/passwd + ... + jerry:x:1004:1004::/home/jerry:/bin/bash + kylin:x:1005:1005::/home/kylin:/bin/bash + tsengia:x:1006:1006::/home/tsengia:/bin/bash + obama:x:1007:1007::/home/obama:/bin/bash + lala@hecs-393504:~$ sudo chage -l kylin + Last password change : Jun 14, 2024 + Password expires : never + Password inactive : never + Account expires : Dec 30, 2020 + Minimum number of days between password change : 0 + Maximum number of days between password change : 99999 + Number of days of warning before password expires : 7 + ``` + +4. 将jerry、kylin、tsengia、obama等用户添加到zonghe组内 + + ```bash + lala@hecs-393504:/root$ sudo gpasswd -M jerry,kylin,tsengia,obama zonghe + lala@hecs-393504:/root$ sudo cat /etc/group + ... + zonghe:x:2001:jerry,kylin,tsengia,obama + ``` + +5. 创建handy、cucci用户,其中cucci帐号的登录Shell设置为“/sbin/nologin” + + ```ba + lala@hecs-393504:/root$ sudo useradd -m handy + lala@hecs-393504:/root$ sudo useradd -m -s /sbin/nologin cucci + lala@hecs-393504:/root$ sudo cat /etc/passwd + ... + handy:x:1008:1008::/home/handy:/bin/sh + cucci:x:1009:1009::/home/cucci:/sbin/nologin + ``` + +6. 将handy、cucci等用户添加到jishu组内 + + ```bash + lala@hecs-393504:/root$ sudo gpasswd -M handy,cucci jishu + lala@hecs-393504:/root$ sudo cat /etc/group + ... + jishu:x:2003:handy,cucci + ``` + +7. 将上述的所有用户均要求加入到guanli组内 + + ```bash + lala@hecs-393504:~$ sudo groupadd -g 2010 guanli + lala@hecs-393504:~$ sudo gpasswd -M jerry,kylin,tsengia,obama,handy,cucci guanli + lala@hecs-393504:~$ sudo cat /etc/group + ... + guanli:x:2010:jerry,kylin,tsengia,obama,handy,cucci + ``` + +8. 将zonghe组内的obama用户删除 + + ```ba + lala@hecs-393504:~$ sudo gpasswd -d obama zonghe + Removing user obama from group zonghe + ``` + +9. 为jerry用户设置密码为“123456”(使用普通方法)为cucci用户设置密码为“redhat” + + ```bash + lala@hecs-393504:~$ sudo passwd jerry + New password: + Retype new password: + passwd: password updated successfully + ``` + +10. 将jerry用户锁定,并查看锁定状态 + + ```bash + lala@hecs-393504:/root$ sudo passwd -l jerry + [sudo] password for lala: + passwd: password expiry information changed. + lala@hecs-393504:/root$ sudo passwd -S jerry + jerry L 06/14/2024 0 99999 7 -1 + ``` + +11. 打开两个xshell窗口,通过(who 或者 w)命令查看连接状态,并通过fuser杀掉其中一个 + + ```bash + lala@hecs-393504:/root$ w + 18:06:34 up 91 days, 18:12, 3 users, load average: 0.07, 0.02, 0.00 + USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT + root pts/0 112.5.195.104 16:27 1:13m 0.01s 0.01s -bash + root pts/1 112.5.195.104 18:05 0.00s 0.01s 0.00s w + lala pts/2 112.5.195.104 18:06 8.00s 0.01s 0.01s -bash + lala@hecs-393504:/root$ fuser -k /dev/pts/2 + /dev/pts/2: 255058 + lala@hecs-393504:/root$ w + 18:06:49 up 91 days, 18:13, 2 users, load average: 0.06, 0.01, 0.00 + USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT + root pts/0 112.5.195.104 16:27 1:13m 0.01s 0.01s -bash + root pts/1 112.5.195.104 18:05 1.00s 0.01s 0.00s w + ``` + +12. 查看cucci用户,属于那些组,并查看其详细信息 + + ```bash + lala@hecs-393504:/root$ groups cucci + cucci : cucci jishu guanli + lala@hecs-393504:/root$ id cucci + uid=1009(cucci) gid=1009(cucci) groups=1009(cucci),2003(jishu),2010(guanli) + ``` + +13. 手工创建账号student(预留) + + ```bash + lala@hecs-393504:~$ sudo useradd -m -s /bin/bash student + ``` + +14. 设置权限及归属:/guanli目录属组设为guanli, /guanli/zonghe目录的属组设为zonghe /guanli/jishu目录的属组设为jishu,设置3个目录都是禁止其他用户访问的权限 + + ```bash + lala@hecs-393504:~$ sudo chgrp -R guanli guanli + lala@hecs-393504:~$ ls -l + ... + drwxr-xr-x 4 root guanli 4096 Jun 14 16:33 guanli + lala@hecs-393504:~/guanli$ sudo chgrp -R zonghe zonghe + lala@hecs-393504:~/guanli$ sudo chgrp -R guanli/jishu jishu + lala@hecs-393504:~/guanli$ ls -l + total 8 + drwxr-xr-x 2 root jishu 4096 Jun 14 16:33 jishu + drwxr-xr-x 2 root zonghe 4096 Jun 14 16:33 zonghe + #禁止其他用户访问 + lala@hecs-393504:~$ sudo chmod 750 guanli + lala@hecs-393504:~$ sudo chmod 750 guanli/zonghe + lala@hecs-393504:~$ sudo chmod 750 guanli/jishu + lala@hecs-393504:~$ ls -l + ... + drwxr-x--- 4 root guanli 4096 Jun 14 16:33 guanli + lala@hecs-393504:~$ sudo ls -l guanli + total 8 + drwxr-x--- 2 root jishu 4096 Jun 14 16:33 jishu + drwxr-x--- 2 root zonghe 4096 Jun 14 16:33 zonghe + lala@hecs-393504:~$ + ``` + +15. 建立公共目录/ceshi允许技术组内的所有用户读取、写入、执行文件, 禁止其他用户读、写、执行 + + ```bash + lala@hecs-393504:~$ sudo mkdir ceshi + lala@hecs-393504:~$ sudo chmod 770 ceshi + lala@hecs-393504:~$ ls -l + ... + drwxrwx--- 2 root root 4096 Jun 14 18:35 ceshi + ``` + +16. 清除jerry用户密码 + + ```bash + lala@hecs-393504:~$ sudo passwd -d jerry + passwd: password expiry information changed. + ``` + +17. 锁定cucci用户密码并查看状态 + + ```bash + lala@hecs-393504:~$ sudo passwd -l cucci + passwd: password expiry information changed. + lala@hecs-393504:~$ sudo passwd -S jerry + jerry NP 06/14/2024 0 99999 7 -1 + ``` + +18. 修改obama用户的UID为8888 + + ```ba + lala@hecs-393504:~$ sudo usermod -u 8888 obama + lala@hecs-393504:~$ cat /etc/passwd + ... + obama:x:8888:1007::/home/obama:/bin/bash + ``` + +19. 通过passwd命令修改kylin用户的最长密码使用期限为60天 + + ```bash + lala@hecs-393504:~$ sudo passwd -x 60 kylin + passwd: password expiry information changed. + lala@hecs-393504:~$ sudo passwd -S kylin + kylin L 06/14/2024 0 60 7 -1 + ``` + +20. 通过id groups等命令查看用户handy信息 + + ```bash + lala@hecs-393504:~$ id handy + uid=1008(handy) gid=1008(handy) groups=1008(handy),2003(jishu),2010(guanli) + ``` + + -- Gitee