diff --git "a/48 \347\206\212\346\231\257\345\263\260/\347\224\250\346\210\267\345\222\214\346\226\207\344\273\266\346\235\203\351\231\220.md" "b/48 \347\206\212\346\231\257\345\263\260/\347\224\250\346\210\267\345\222\214\346\226\207\344\273\266\346\235\203\351\231\220.md" new file mode 100644 index 0000000000000000000000000000000000000000..51e29400e56e7d84865d1d681d6742f218644ce1 --- /dev/null +++ "b/48 \347\206\212\346\231\257\345\263\260/\347\224\250\346\210\267\345\222\214\346\226\207\344\273\266\346\235\203\351\231\220.md" @@ -0,0 +1,290 @@ +# 用户与文件权限 + +## 用户 + +### 用户管理 + +超级用户root:0 0 0 不受权限的限制 + +普通用户从1000开始 + +#### 添加用户: + +低级useradd 用户名 + +-m 生成同名家目录 + +-d 指定目录 + +-s /bin/shell 指定生成shell + +-g 用户组原生组 -G附加组 + +高级 adduser + +#### 删除用户: + +userdel 用户名 + +-r 删除用户及相关的目录 + +#### 查询: + +找普通用户: + +cat /etc/passwd + +查找某个用户: + +id 用户名 + +#### 修改用户: + +usermod -aG 组名 用户名 追加组 + +#### 设置密码: + +passwd 用户名 + +passwd -l 用户名 锁定用户 + +passwd -S 用户名 查看用户状态 + +### 用户组管理 + +#### 添加用户到组: + +groupadd 组名 + +-g指定组号 -o可以与原有组同名 + +#### 查询组: + +cat /etc/group + +#### 删除组: + +groupdel 组名 + +#### 修改: + +groupmod 可以修改组id + +## 文件权限 + +### 文件类型 + +10个字符中 第一位表示: + +目录 d + +普通文件 - + +链接 l + +其他 b c s p + +### 基本权限 + +r 读 4 文件只打开内容,目录只能浏览极少信息 + +w 写 2 修改 删除 移动 复制 + +x 执行 1 执行文件 进入目录 + +在原有的权限上增加新权限 + + +在原有的权限上减少权限 - + +覆盖权限 = + +### 权限修改 + +2-4 文件所有者 u + +5-7 所属用户组 g + +8-10 其他人 o + +所有人 a + +chmod o+x a.txt + +其他人增加a.txt的执行权限 + +chmod 421 a.txt + +a.txt中用户所有者u具有读权限,所属组g具有写权限,其他人o具有执行权限。 + +chown 用户名 目录/文件 修改目录/文件的所有者 + +chagp 组名 文件/目录 修改文件/目录的所属组 + +# 作业 + +1. 创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) + + ``` + sx@hecs-157832:~/0614$ mkdir -p guanli/zonghe guanli/jishu + ``` + +2. 添加组帐号zonghe、caiwu、jishu,GID号分别设置为2001、2002、2003 + + ``` + root@hecs-157832:~# groupadd zonghe -g 2001 + root@hecs-157832:~# groupadd caiwu -g 2002 + root@hecs-157832:~# groupadd jishu -g 2003 + ``` + +3. 创建jerry、kylin、tsengia、obama用户,其中的kylin用户帐号在2020年12月30日后失效 + + ``` + root@hecs-157832:~# useradd jerry + root@hecs-157832:~# useradd kylin + root@hecs-157832:~# useradd tsengia + root@hecs-157832:~# useradd obama + root@hecs-157832:~# usermod -e '2020-12-30' kylin + ``` + +4. 将jerry、kylin、tsengia、obama等用户添加到zonghe组内 + + ``` + root@hecs-157832:~# usermod -aG zonghe jerry + root@hecs-157832:~# usermod -aG zonghe tsengia + root@hecs-157832:~# usermod -aG zonghe obama + ``` + +5. 创建handy、cucci用户,其中cucci帐号的登录Shell设置为“/sbin/nologin” + + ``` + root@hecs-157832:~# useradd handy + root@hecs-157832:~# useradd cucci -s /sbin/nologin + ``` + +6. 将handy、cucci等用户添加到jishu 组内 + + ``` + root@hecs-157832:~# usermod -aG jishu handy + root@hecs-157832:~# usermod -aG jishu cucci + ``` + +7. 将上述的所有用户均要求加入到guanli组内 + + ``` + root@hecs-157832:~# groupadd guanli + root@hecs-157832:~# usermod -aG guanli jerry + root@hecs-157832:~# usermod -aG guanli kylin + root@hecs-157832:~# usermod -aG guanli tsengia + root@hecs-157832:~# usermod -aG guanli obama + root@hecs-157832:~# usermod -aG guanli handy + root@hecs-157832:~# usermod -aG guanli cucci + ``` + +8. 将zonghe组内的obama用户删除 + + ``` + root@hecs-157832:~# gpasswd -d obama zonghe + Removing user obama from group zonghe + ``` + +9. 为jerry用户设置密码为“123456”(使用普通方法)为cucci用户设置密码为“redhat” + + ``` + root@hecs-157832:~# passwd jerry + New password: + Retype new password: + passwd: password updated successfully + root@hecs-157832:~# passwd cucci + New password: + Retype new password: + passwd: password updated successfully + ``` + +10. 将jerry用户锁定,并查看锁定状态 + + ``` + root@hecs-157832:~# passwd -l jerry + passwd: password expiry information changed. + root@hecs-157832:~# passwd -S jerry + jerry L 06/14/2024 0 99999 7 -1 + ``` + +11. 打开两个xshell窗口,通过(who 或者 w)命令查看连接状态,并通过fuser杀掉其中一个 + + ``` + root@hecs-157832:~# who + root pts/4 2024-06-14 16:56 (112.5.195.104) + root pts/5 2024-06-14 16:56 (112.5.195.104) + sx pts/6 2024-06-14 16:56 (112.5.195.104) + sx pts/7 2024-06-14 16:56 (112.5.195.104) + ``` + +12. 查看cucci用户,属于那些组,并查看其详细信息 + + ``` + root@hecs-157832:~# id cucci + uid=1007(cucci) gid=1007(cucci) groups=1007(cucci),2003(jishu),2004(guanli) + ``` + +13. 手工创建账号student(预留) + + ``` + root@hecs-157832:~# useradd student + ``` + +14. 设置权限及归属:/guanli目录属组设为guanli, /guanli/zonghe目录的属组设为zonghe /guanli/jishu目录的属组设为jishu,设置3个目录都是禁止其他用户访问的权限 + + ``` + sx@hecs-157832:~/0614$ sudo chgrp guanli guanli + sx@hecs-157832:~/0614$ chmod o-r guanli + sx@hecs-157832:~/0614/guanli$ sudo chgrp zonghe zonghe + sx@hecs-157832:~/0614/guanli$ chmod o-r zonghe + sx@hecs-157832:~/0614/guanli$ sudo chgrp jishu jishu + sx@hecs-157832:~/0614/guanli$ chmod o-r jishu + ``` + +15. 建立公共目录/ceshi允许技术组内的所有用户读取、写入、执行文件, 禁止其他用户读、写、执行 + + ``` + sx@hecs-157832:~/0614/guanli$ mkdir ceshi + sx@hecs-157832:~/0614/guanli$ sudo chgrp jishu ceshi + sx@hecs-157832:~/0614/guanli$ chmod g+wrx,o-wrx ceshi + ``` + +16. 清除jerry用户密码 + + ``` + sx@hecs-157832:~/0614/guanli$ sudo passwd -d jerry + passwd: password expiry information changed. + ``` + +17. 锁定cucci用户密码并查看状态 + + ``` + root@hecs-157832:~# passwd -l cucci + passwd: password expiry information changed. + root@hecs-157832:~# passwd -S cucci + cucci L 06/14/2024 0 99999 7 -1 + ``` + +18. 修改obama用户的UID为8888 + + ``` + root@hecs-157832:~# usermod -u 8888 obama + ``` + +19. 通过passwd命令修改kylin用户的最长密码使用期限为60天 + + ``` + root@hecs-157832:~# chage -M 60 kylin + root@hecs-157832:~# passwd -S kylin + ``` + +20. 通过id groups等命令查看用户handy信息 + +``` +root@hecs-157832:~# id handy +uid=1006(handy) gid=1006(handy) groups=1006(handy),2003(jishu),2004(guanli) +root@hecs-157832:~# groups handy +handy : handy jishu guanli +``` \ No newline at end of file diff --git "a/48 \347\206\212\346\231\257\345\263\260/\351\207\215\345\256\232\345\220\221\345\222\214\347\256\241\351\201\223.md" "b/48 \347\206\212\346\231\257\345\263\260/\351\207\215\345\256\232\345\220\221\345\222\214\347\256\241\351\201\223.md" new file mode 100644 index 0000000000000000000000000000000000000000..557e4a3b35f9c00d0248233c94223e4003193e2f --- /dev/null +++ "b/48 \347\206\212\346\231\257\345\263\260/\351\207\215\345\256\232\345\220\221\345\222\214\347\256\241\351\201\223.md" @@ -0,0 +1,137 @@ +# 重定向 + +0 标准输入 + +1 标准输出 + +2 标准错误 + +- 标准输出覆盖重定向 + + ``` + sx@hecs-157832:~/0613$ ls ../ >1.txt + sx@hecs-157832:~/0613$ cat 1.txt + 0603 + 0611 + 0613 + China + #将ls ../ 标准输出的内容覆盖到1.txt + ``` + +- 标准输出追加重定向 + + ``` + sx@hecs-157832:~/0613$ ls >> 1.txt + sx@hecs-157832:~/0613$ cat 1.txt + 0603 + 0611 + 0613 + China + 1.txt + ``` + +- 输入重定向 + + ``` + sx@hecs-157832:~/0613$ sort<1.txt + 3 + 4 + 5 + 6 + 8 + #将1.txt的内容作为输入的内容并通过sort排序 + ``` + +- 标准错误覆盖重定向 + + ``` + sx@hecs-157832:~/0613$ cd 99 2>2.txt + sx@hecs-157832:~/0613$ cat 2.txt + -bash: cd: 99: No such file or directory + #因为没有99目录 所以报错的信息覆盖到2.txt + ``` + +- 同时重定向标准输出和标准错误 + + ``` + sx@hecs-157832:~/0613$ (cd 99 || cat 1.txt) &>3.txt + sx@hecs-157832:~/0613$ cat 3.txt + -bash: cd: 99: No such file or directory + 5 + 3 + 4 + 8 + 6 + ``` + +- 标准错误丢弃 + + ``` + sx@hecs-157832:~/0613$ find /etc -name '*.conf' 2>/dev/null + ``` + +# 管道 + +将一个命令的输出传给另一个命令 + +``` +sx@hecs-157832:~/0613$ find /etc -name '*.conf' | grep 'apt' +find: ‘/etc/chatscripts’: Permission denied +find: ‘/etc/ssl/private’: Permission denied +find: ‘/etc/polkit-1/localauthority’: Permission denied +find: ‘/etc/ppp/peers’: Permission denied +/etc/apt/listchanges.conf +/etc/apt/apt.conf +#找/etc下*.conf里面 关键字有apt的 +``` + +# tee + +``` +sx@hecs-157832:~/0613$ echo wwjsb | tee wwj.txt +wwjsb +``` + +与>的区别是tee显示了一遍内容 + +# 问题 + +<< + +Here Document (<<) 是一种在命令行或脚本中将多行文本作为输入传递给命令的方法。 + +``` +sx@hecs-157832:~/0613$ cat << EOF +> 4 +> 3 +> 2 +> 1 +> EOF +4 +3 +2 +1 +#多行文本传递给 cat 命令并打印到标准输出 +``` + +() + +1.子shell + +子 Shell 是一个新的 Shell 进程,用圆括号 () 来创建。子 Shell 内的操作不会影响外部的 Shell。 + +``` +sx@hecs-157832:~/0613$ (cd ../0603 && ls) +student_scores.csv +sx@hecs-157832:~/0613$ pwd +/home/sx/0613 +#进入0603目录并ls 但并没有改变当前路径 +``` + +2.命令组 + +3.变量 + +4.函数 + +5.定义数组 \ No newline at end of file