- 今日内容
- sed
2.1 简介
2.2 取行
2.3 替换
2.4 后向引用(反向引用)- awk
3.1 简介
3.2 取列
3.3 取行 + 过滤- 总结三剑客
- 软硬链接
- 权限体系
6.1 rwx
6.2 测试文件的rwx权限
6.3 测试目录的rwx权限
6.4 通过权限控制让网站安全- 1台服务器
- 网站集群安全(网站架构)
6.5 权限补充- 用户管理体系
7.1 用户有关的文件
7.2 用户相关目录 /etc/skel/
7.3 故障案例 命令行-bash-4.2
7.4 用户相关命令
7.5 sudo
2. sed
2.1 简介
- 流编辑器 认为所有文件像源源不断的水流 通过sed处理
- sed -n '条件动作' 文件
- sed -n '5p' filename
- -n 参数
- 条件(找谁)
1
1,5
/oldboy/ - 动作
p(显示) 要与-n一起使用
d 删除
s 替换
参数 | 含义 |
---|---|
-n | 取消默认输出 sed命令在处理每一行的时候会有默认输出 |
-r | 支持扩展正则 |
-i | 修改文件内容 |
-i.bak | 先备份文件 然后修改文件内容(eg: oldboy.txt 先备份为oldboy.txt.bak 然后修改oldboy.txt内容) |
2.2 取行
- 取某一行 根据行号
[root@oldboy64-yl ~]# sed -n '3p' pass.txt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@oldboy64-yl ~]# sed '3p' pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
- 根据范围取行
[root@oldboy64-yl ~]# sed -n '1,5p' pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
- 过滤 类似于grep 'root' pass.txt
[root@oldboy64-yl ~]# grep 'root' pass.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@oldboy64-yl ~]# sed -n '/root/p' pass.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@oldboy64-yl ~]# sed -n '/^root/p' pass.txt
root:x:0:0:root:/root:/bin/bash
[root@oldboy64-yl ~]#
- 练习题
- 取出 pass.txt (/etc/passwd 前10行) 第1到9行
[root@oldboy64-yl ~]# sed -n '1,9p' pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@oldboy64-yl ~]# tail -1 pass.txt
394916732 stu06
[root@oldboy64-yl ~]# sed -n '$p' pass.txt
394916732 stu06
[root@oldboy64-yl ~]#
- 取出 文件中以 nologin 结尾的行
[root@oldboy64-yl ~]# grep 'nologin$' pass.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@oldboy64-yl ~]# sed -n '/nologin$/p' pass.txt
-
取出 用户名 只是由字母(大小写) 组成的行
image.png
[root@oldboy64-yl ~]# sed -nr '/^[a-Z]+/p' pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
- 取出从包含root的行到 包含 nobody的行
[root@oldboy64-yl ~]# sed -n '/root/,/nobody/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
[root@oldboy64-yl ~]#
2.3 替换
- sed 's###g'
- 's@@@g'
- sed's#nologin#oldboy#g' pass.txt
- g 全局替换 不加就替换每行出现的第1个内容
[root@oldboy64-yl ~]# sed 's#nologin#oldboy#g' pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/oldboy
daemon:x:2:2:daemon:/sbin:/sbin/oldboy
adm:x:3:4:adm:/var/adm:/sbin/oldboy
lp:x:4:7:lp:/var/spool/lpd:/sbin/oldboy
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/oldboy
operator:x:11:0:operator:/root:/sbin/oldboy
- sed修改文件内容
[root@oldboy64-yl ~]# sed -i 's#nologin#oldboy#g' pass.txt
[root@oldboy64-yl ~]# cat pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/oldboy
daemon:x:2:2:daemon:/sbin:/sbin/oldboy
adm:x:3:4:adm:/var/adm:/sbin/oldboy
lp:x:4:7:lp:/var/spool/lpd:/sbin/oldboy
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/oldboy
operator:x:11:0:operator:/root:/sbin/oldboy
- sed修改文件之前 备份 然后修改文件内容
[root@oldboy64-yl ~]# sed -i.bak 's#/sbin/oldboy#/bin/bash#g' pass.txt
[root@oldboy64-yl ~]# ll pass.*
-rw-r--r-- 1 root root 361 Aug 8 21:37 pass.txt
-rw-r--r-- 1 root root 361 Aug 8 21:36 pass.txt.bak
[root@oldboy64-yl ~]# cat pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/bash
daemon:x:2:2:daemon:/sbin:/bin/bash
adm:x:3:4:adm:/var/adm:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/bash
operator:x:11:0:operator:/root:/bin/bash
[root@oldboy64-yl ~]# cat pass.txt.bak
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/bash
daemon:x:2:2:daemon:/sbin:/bin/bash
adm:x:3:4:adm:/var/adm:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/bash
operator:x:11:0:operator:/root:/bin/bash
[root@oldboy64-yl ~]#
2.4 后向引用(反向引用)
题目:
[root@oldboy64-lnb ~]# echo 123456
123456
通过sed处理 想要下面的结果
<123456>
- 后向引用 核心:先保护() 然后在后面使用 \1 \2 \3
[root@oldboy64-yl ~]# echo 123456
123456
[root@oldboy64-yl ~]# echo 123456|sed 's#123456#<123456>#g'
<123456>
[root@oldboy64-yl ~]# echo 123456|sed -r 's#(.*)#<\1>#g'
<123456>
[root@oldboy64-yl ~]#
完成第2题
[root@oldboy64-lnb ~]# echo '12<34>56'
12<34>56
完成第3题
[root@oldboy64-lnb ~]# echo '<1><2><3><4><5><6>'
<1><2><3><4><5><6>
[root@oldboy64-yl ~]# echo 123456|sed -r 's#(..)(..)(..)#\1<\2>\3#g'
12<34>56
[root@oldboy64-yl ~]# echo 123456 | sed -r 's#([0-9])#<\1>#g'
<1><2><3><4><5><6>
[root@oldboy64-yl ~]# echo 123456 | sed -r 's#(.)#<\1>#g'
<1><2><3><4><5><6>
- pass.txt 第1列和最后一列位置调换
[root@oldboy64-yl ~]# sed -r 's#(.*)(:.*:)(.*)#\3\2\1#g' pass.txt
/bin/bash:/root:root:x:0:0:root
/bin/bash:/bin:bin:x:1:1:bin
/bin/bash:/sbin:daemon:x:2:2:daemon
/bin/bash:/var/adm:adm:x:3:4:adm
/bin/bash:/var/spool/lpd:lp:x:4:7:lp
/bin/sync:/sbin:sync:x:5:0:sync
/sbin/shutdown:/sbin:shutdown:x:6:0:shutdown
/sbin/halt:/sbin:halt:x:7:0:halt
/bin/bash:/var/spool/mail:mail:x:8:12:mail
/bin/bash:/root:operator:x:11:0:operator
3. awk
3.1 简介
- 取列 指定分隔符
- 过滤
参数 | |
---|---|
awk内置变量 | |
$1 | 第一列 |
$0 | 整行 |
NR | 记录号(行号) |
$NF | 最后一行 |
3.2 取列
[root@oldboy64-yl ~]# echo {1..5} |awk '{print $3}'
3
[root@oldboy64-yl ~]# echo {1..5} |awk '{print $0}'
1 2 3 4 5
[root@oldboy64-yl ~]# echo {1..5} |awk '{print $NF}'
5
[root@oldboy64-yl ~]# awk '{print NR,$0}' pass.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/bin/bash
4 adm:x:3:4:adm:/var/adm:/bin/bash
5 lp:x:4:7:lp:/var/spool/lpd:/bin/bash
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/bin/bash
10 operator:x:11:0:operator:/root:/bin/bash
[root@oldboy64-yl ~]# awk -F':' '{print $1}' pass.txt
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@oldboy64-yl ~]#
- 显示pass.txt的行号和第1列第3列和最后一列的内容
[root@oldboy64-yl ~]# awk -F':' '{print NR,$1,$3,$NF}' pass.txt
1 root 0 /bin/bash
2 bin 1 /bin/bash
3 daemon 2 /bin/bash
4 adm 3 /bin/bash
5 lp 4 /bin/bash
6 sync 5 /bin/sync
7 shutdown 6 /sbin/shutdown
8 halt 7 /sbin/halt
9 mail 8 /bin/bash
10 operator 11 /bin/bash
[root@oldboy64-yl ~]# awk -F':' '{print NR,$1,$3,$NF}' pass.txt|column -t
1 root 0 /bin/bash
2 bin 1 /bin/bash
3 daemon 2 /bin/bash
4 adm 3 /bin/bash
5 lp 4 /bin/bash
6 sync 5 /bin/sync
7 shutdown 6 /sbin/shutdown
8 halt 7 /sbin/halt
9 mail 8 /bin/bash
10 operator 11 /bin/bash
[root@oldboy64-yl ~]#
- awk '条件{动作}'
- 条件 找谁
- 动作 print
- 花括号外面的就是条件
[root@oldboy64-yl ~]# ip a s eth0|awk -F'[ /]+' 'NR==3{print $3}'
10.0.0.200
3.3 取行 + 过滤
> |
---|
>= |
<= |
< |
== |
!= |
- 比大小取行
[root@oldboy64-yl ~]# awk 'NR==3' pass.txt
daemon:x:2:2:daemon:/sbin:/bin/bash
[root@oldboy64-yl ~]# awk 'NR>=3' pass.txt
daemon:x:2:2:daemon:/sbin:/bin/bash
adm:x:3:4:adm:/var/adm:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/bash
operator:x:11:0:operator:/root:/bin/bash
[root@oldboy64-yl ~]# awk 'NR!=3' pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/bash
adm:x:3:4:adm:/var/adm:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/bash
operator:x:11:0:operator:/root:/bin/bash
[root@oldboy64-yl ~]#
- 过滤
[root@oldboy64-yl ~]# awk '/root/' pass.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/bin/bash
[root@oldboy64-yl ~]# awk '/root/,'/oldboy/'' pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/bash
daemon:x:2:2:daemon:/sbin:/bin/bash
adm:x:3:4:adm:/var/adm:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/bash
operator:x:11:0:operator:/root:/bin/bash
- pass.txt 第3列大于1的行
[root@oldboy64-yl ~]# awk -F':' '$3>1' pass.txt
daemon:x:2:2:daemon:/sbin:/bin/bash
adm:x:3:4:adm:/var/adm:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/bash
operator:x:11:0:operator:/root:/bin/bash
[root@oldboy64-yl ~]# awk -F':' '$3>1&&$3<10' pass.txt
daemon:x:2:2:daemon:/sbin:/bin/bash
adm:x:3:4:adm:/var/adm:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/bash
[root@oldboy64-yl ~]#
- 第3列中数字以1或2开头的行
- ^ 某一列中以xxx开头
- $ 某一列中以xxx结尾
[root@oldboy64-yl ~]# awk -F':' '$3~/^[12]/' pass.txt
bin:x:1:1:bin:/bin:/bin/bash
daemon:x:2:2:daemon:/sbin:/bin/bash
operator:x:11:0:operator:/root:/bin/bash
[root@oldboy64-yl ~]#
4. 总结三剑客
共同 | 区别 | |
---|---|---|
grep | 过滤(最快) | 过滤 测试正则表达式(加上颜色) |
sed | 过滤 | 取行 替换 修改文件内容 |
awk | 过滤 | 取列 某一列中有什么(过滤)awk统计计算 |
5. 软硬链接
软连接与硬链接
- 软连接:
也叫符号链接 相当于快捷方式
存放源文件的路径
Linux使用频繁
[root@oldboy64-yl ~]# echo oldboy64 is lnb > oldboy.txt
[root@oldboy64-yl ~]# cat oldboy.txt
oldboy64 is lnb
[root@oldboy64-yl ~]# ln -s oldboy.txt oldboy.txt.bak
ln: failed to create symbolic link ‘oldboy.txt.bak’: File exists
[root@oldboy64-yl ~]# ll oldboy.txt*
-rwxr-xr-x 1 root root 16 Aug 10 01:51 oldboy.txt
-rwxr-xr-x 1 root root 95 Aug 1 21:11 oldboy.txt.bak
lrwxrwxrwx 1 root root 10 Jul 14 17:12 oldboy.txt.soft -> oldboy.txt
[root@oldboy64-yl ~]# ll -d /etc/rc.local /etc/init.d /bin /sbin
lrwxrwxrwx. 1 root root 7 Jun 30 17:47 /bin -> usr/bin
lrwxrwxrwx 1 root root 11 Jun 30 17:47 /etc/init.d -> rc.d/init.d
lrwxrwxrwx 1 root root 13 Jun 30 17:48 /etc/rc.local -> rc.d/rc.local
lrwxrwxrwx. 1 root root 8 Jun 30 17:47 /sbin -> usr/sbin
[root@oldboy64-yl ~]#
- 硬链接:
文件之间inode号码一样的文件 互为硬链接
inode存放文件属性信息 存放block的位置
block 文件的内容(数据)
硬链接可以防止误删除
[root@oldboy64-yl ~]# ln oldboy.txt oldboy.txt.hard
[root@oldboy64-yl ~]# ll oldboy.txt*
-rwxr-xr-x 2 root root 16 Aug 10 01:51 oldboy.txt
-rwxr-xr-x 1 root root 95 Aug 1 21:11 oldboy.txt.bak
-rwxr-xr-x 2 root root 16 Aug 10 01:51 oldboy.txt.hard
lrwxrwxrwx 1 root root 10 Jul 14 17:12 oldboy.txt.soft -> oldboy.txt
[root@oldboy64-yl ~]# ll -i oldboy.txt*
33574997 -rwxr-xr-x 2 root root 16 Aug 10 01:51 oldboy.txt
868058 -rwxr-xr-x 1 root root 95 Aug 1 21:11 oldboy.txt.bak
33574997 -rwxr-xr-x 2 root root 16 Aug 10 01:51 oldboy.txt.hard
33637745 lrwxrwxrwx 1 root root 10 Jul 14 17:12 oldboy.txt.soft -> oldboy.txt
[root@oldboy64-yl ~]#
6. 权限体系
6.1 rwx
文件权限 | |
---|---|
r | 读 是否能查看文件内容 |
w | 写 是否能修改文件内容 需要r配合 |
x | 执行(执行命令或脚本)需要r配合 |
目录权限 书包(教室) | |
---|---|
r | 能否看看屋子 书包内容 需要x配合 |
w | 能否在屋子 创建 删除 物品(文件)重命名(修改)需要x配合 |
x | 是否可以进入目录权限 |
- 删除1个文件 需要对文件所在目录有wx权限
6.2 测试文件的rwx权限
环境:
窗口1 | 窗口2 |
---|---|
root | oldboy |
进行授权 | 进行测试 |
[root@oldboy64-yl ~]# mkdir -p /oldboy/
[root@oldboy64-yl ~]# echo 'pwd'> /oldboy/oldboy.sh
[root@oldboy64-yl ~]# ll /oldboy/oldboy.sh
-rw-r--r-- 1 root root 4 Aug 10 11:23 /oldboy/oldboy.sh
[root@oldboy64-yl ~]# cat /oldboy/oldboy.sh
pwd
[root@oldboy64-yl ~]# chmod 755 /oldboy/oldboy.sh
[root@oldboy64-yl ~]# ll /oldboy/oldboy.sh
-rwxr-xr-x 1 root root 4 Aug 10 11:23 /oldboy/oldboy.sh
[root@oldboy64-yl ~]# /oldboy/oldboy.sh
/root
[root@oldboy64-yl ~]# cd /oldboy/
[root@oldboy64-yl /oldboy]# ./oldboy.sh
/oldboy
[root@oldboy64-yl /oldboy]# chown oldboy.oldboy /oldboy/oldboy.sh
[root@oldboy64-yl /oldboy]# ll /oldboy/oldboy.sh
-rwxr-xr-x 1 oldboy oldboy 4 Aug 10 11:23 /oldboy/oldboy.sh
[root@oldboy64-yl /oldboy]#
- 测试文件的r权限
#root进行修改
[root@oldboy64-yl /oldboy]# chmod u=r /oldboy/oldboy.sh
[root@oldboy64-yl /oldboy]# ll /oldboy/oldboy.sh
-r--r-xr-x 1 oldboy oldboy 4 Aug 10 11:23 /oldboy/oldboy.sh
[root@oldboy64-yl /oldboy]#
#oldboy进行测试
[oldboy@oldboy64-yl ~]$ cat /oldboy/oldboy.sh
pwd
[oldboy@oldboy64-yl ~]$ echo hostname >> /oldboy/oldboy.sh
-bash: /oldboy/oldboy.sh: Permission denied
[oldboy@oldboy64-yl ~]$ /oldboy/oldboy.sh
-bash: /oldboy/oldboy.sh: Permission denied
[oldboy@oldboy64-yl ~]$
- 测试文件的w权限
- 文件只有w权限,需要r权限配合
#root进行修改
[root@oldboy64-yl /oldboy]# chmod u=w /oldboy/oldboy.sh
[root@oldboy64-yl /oldboy]# ll /oldboy/oldboy.sh
--w-r-xr-x 1 oldboy oldboy 4 Aug 10 11:23 /oldboy/oldboy.sh
#oldboy进行测试
[oldboy@oldboy64-yl ~]$ cat /oldboy/oldboy.sh
cat: /oldboy/oldboy.sh: Permission denied
[oldboy@oldboy64-yl ~]$ echo hostname >> /oldboy/oldboy.sh
#root进行测试
[root@oldboy64-yl /oldboy]# cat /oldboy/oldboy.sh
pwd
hostname
- 测试文件的x权限
#root进行修改
[root@oldboy64-yl /oldboy]# chmod u=x /oldboy/oldboy.sh
[root@oldboy64-yl /oldboy]# ll /oldboy/oldboy.sh
---xr-xr-x 1 oldboy oldboy 13 Aug 10 11:34 /oldboy/oldboy.sh
[root@oldboy64-yl /oldboy]#
#oldboy进行测试
[oldboy@oldboy64-yl ~]$ cat /oldboy/oldboy.sh
cat: /oldboy/oldboy.sh: Permission denied
[oldboy@oldboy64-yl ~]$ echo hostname >> /oldboy/oldboy.sh
-bash: /oldboy/oldboy.sh: Permission denied
[oldboy@oldboy64-yl ~]$ /oldboy/oldboy.sh
bash: /oldboy/oldboy.sh: Permission denied
[oldboy@oldboy64-yl ~]$
- 文件的rwx权限
r查看文件的内容
w修改文件内容 需要r配合
x执行 需要r配合
6.3 测试目录的rwx权限
环境:
mkdir /oldboy/test/
touch /oldboy/test/oldboy{01..10}.txt
chown oldboy.oldboy /oldboy/test/
- 目录的r权限
目录的r权限 查看目录的内容 需要x配合
目录的x权限 是否能进入到目录 是否能查看或修改目录中文件属性
#root
[root@oldboy64-yl /oldboy]# chmod u=r /oldboy/test/
[root@oldboy64-yl /oldboy]# ll -d /oldboy/test/
dr--r-xr-x 2 oldboy oldboy 186 Aug 7 19:54 /oldboy/test/
[root@oldboy64-yl /oldboy]#
#oldboy
[oldboy@oldboy64-yl ~]$ ls /oldboy/test/
ls: cannot access /oldboy/test/oldboy02.txt: Permission denied
ls: cannot access /oldboy/test/oldboy03.txt: Permission denied
ls: cannot access /oldboy/test/oldboy04.txt: Permission denied
ls: cannot access /oldboy/test/oldboy05.txt: Permission denied
ls: cannot access /oldboy/test/oldboy06.txt: Permission denied
ls: cannot access /oldboy/test/oldboy07.txt: Permission denied
ls: cannot access /oldboy/test/oldboy08.txt: Permission denied
ls: cannot access /oldboy/test/oldboy09.txt: Permission denied
ls: cannot access /oldboy/test/oldboy10.txt: Permission denied
oldboy02.txt oldboy05.txt oldboy08.txt
oldboy03.txt oldboy06.txt oldboy09.txt
oldboy04.txt oldboy07.txt oldboy10.txt
[oldboy@oldboy64-yl ~]$ ll /oldboy/test/
ls: cannot access /oldboy/test/oldboy02.txt: Permission denied
ls: cannot access /oldboy/test/oldboy03.txt: Permission denied
ls: cannot access /oldboy/test/oldboy04.txt: Permission denied
ls: cannot access /oldboy/test/oldboy05.txt: Permission denied
ls: cannot access /oldboy/test/oldboy06.txt: Permission denied
ls: cannot access /oldboy/test/oldboy07.txt: Permission denied
ls: cannot access /oldboy/test/oldboy08.txt: Permission denied
ls: cannot access /oldboy/test/oldboy09.txt: Permission denied
ls: cannot access /oldboy/test/oldboy10.txt: Permission denied
total 0
-????????? ? ? ? ? ? oldboy02.txt
-????????? ? ? ? ? ? oldboy03.txt
-????????? ? ? ? ? ? oldboy04.txt
-????????? ? ? ? ? ? oldboy05.txt
-????????? ? ? ? ? ? oldboy06.txt
-????????? ? ? ? ? ? oldboy07.txt
-????????? ? ? ? ? ? oldboy08.txt
-????????? ? ? ? ? ? oldboy09.txt
-????????? ? ? ? ? ? oldboy10.txt
[oldboy@oldboy64-yl ~]$
#root
[root@oldboy64-yl ~]# chmod u=rx /oldboy/test/
[root@oldboy64-yl ~]# ll -d /oldboy/test/
dr-xr-xr-x 2 oldboy oldboy 186 Aug 7 19:54 /oldboy/test/
[root@oldboy64-yl ~]#
#oldboy
[oldboy@oldboy64-yl ~]$ ll -d /oldboy/test/
dr-xr-xr-x 2 oldboy oldboy 186 Aug 7 19:54 /oldboy/test/
[oldboy@oldboy64-yl ~]$ ls /oldboy/test/
oldboy02.txt oldboy05.txt oldboy08.txt
oldboy03.txt oldboy06.txt oldboy09.txt
oldboy04.txt oldboy07.txt oldboy10.txt
[oldboy@oldboy64-yl ~]$ ll /oldboy/test/
total 0
-rw-r--r-- 1 root root 0 Aug 7 19:35 oldboy02.txt
-rw-r--r-- 1 root root 0 Aug 7 19:35 oldboy03.txt
-rw-r--r-- 1 root root 0 Aug 7 19:35 oldboy04.txt
-rw-r--r-- 1 root root 0 Aug 7 19:35 oldboy05.txt
-rw-r--r-- 1 root root 0 Aug 7 19:35 oldboy06.txt
-rw-r--r-- 1 root root 0 Aug 7 19:35 oldboy07.txt
-rw-r--r-- 1 root root 0 Aug 7 19:35 oldboy08.txt
-rw-r--r-- 1 root root 0 Aug 7 19:35 oldboy09.txt
-rw-r--r-- 1 root root 0 Aug 7 19:35 oldboy10.txt
- 目录的w权限(极其危险)
- 目录的rwx权限
r是否可以查看目录内容 需要x
w是否可以在目录中创建 删除 修改(文件名)文件 需要x
x是否可以进入(查看目录中文件属性) - 删除1个文件 与什么权限有关
[oldboy@oldboy64-yl ~]$ ll /oldboy/oldboy.sh
-rwxrwxrwx 1 oldboy oldboy 13 Aug 10 11:34 /oldboy/oldboy.sh
[oldboy@oldboy64-yl ~]$ \rm -f /oldboy/oldboy.sh
rm: cannot remove ‘/oldboy/oldboy.sh’: Permission denied
[oldboy@oldboy64-yl ~]$
#root
[root@oldboy64-yl ~]# ll -d /oldboy/
drwxr-xr-x. 13 root root 4096 Aug 10 11:23 /oldboy/
[root@oldboy64-yl ~]# chown oldboy.oldboy /oldboy/
[root@oldboy64-yl ~]# ll -d /oldboy/
drwxr-xr-x. 13 oldboy oldboy 4096 Aug 10 11:23 /oldboy/
#oldboy
[oldboy@oldboy64-yl ~]$ \rm -f /oldboy/oldboy.sh
[oldboy@oldboy64-yl ~]$
6.4 通过权限控制让网站安全
- 什么样的权限 比较安全
- file 644 rw--r--r
- dir 755 rwxr-xr-x
- 属于root root
1. 1台服务器
- www(虚拟用户)运行网站 (服务)用户 通过虚拟用户运行服务
- 网站目录中 file 644 dir 755 都属于www www
- 定时检查网站的文件是否变化 每次代码更新 重新创建指纹库(创建指纹的时候 排除临时文件 缓存文件)
- 限制用户用户上传的类型(代码)
- 上传进来的文件 无法执行(nfs 挂载)
md5sum 用法补充:
- 检查 每个人使用的文件(下载的文件)是否一致
2.对比文件是否被修改过 ***
[root@oldboy64-yl ~]# echo oldboy> oldboy.txt
[root@oldboy64-yl ~]# md5sum oldboy.txt> police.md5
[root@oldboy64-yl ~]# cat police.md5
3fe396c01f03425cb5e2da8186eb090d oldboy.txt
[root@oldboy64-yl ~]# md5sum -c police.md5
oldboy.txt: OK
[root@oldboy64-yl ~]# echo oldboy>> oldboy.txt
[root@oldboy64-yl ~]# md5sum -c police.md5
oldboy.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
[root@oldboy64-yl ~]#
- 测试 网站用户上传目录权限的设置
#root
[root@oldboy64-yl ~]# mkdir -p /app/blog/
[root@oldboy64-yl ~]# mkdir -p /app/blog/uploads/
[root@oldboy64-yl ~]# touch /app/blog/64lnb.html
[root@oldboy64-yl ~]# touch /app/blog/64lnb.jpg
[root@oldboy64-yl ~]# touch /app/blog/64lnb.torrent
#www
[www@oldboy64-yl ~]$ ll /app/blog/uploads/ -d
drwxr-xr-x 2 root root 34 Aug 10 13:37 /app/blog/uploads/
[www@oldboy64-yl ~]$ touch /app/blog/uploads/lidao-4g.torrent touch: cannot touch ‘/app/blog/uploads/lidao-4g.torrent’: Permission denied
#root
[root@oldboy64-yl ~]# ll /app/blog/uploads/ -d
drwxr-xr-x 2 root root 34 Aug 10 13:37 /app/blog/uploads/
[root@oldboy64-yl ~]# chown www.www /app/blog/uploads/
[root@oldboy64-yl ~]# ll /app/blog/uploads/ -d
drwxr-xr-x 2 www www 34 Aug 10 13:37 /app/blog/uploads/
[root@oldboy64-yl ~]#
#www
[www@oldboy64-yl ~]$ ll /app/blog/uploads/ -d
drwxr-xr-x 2 www www 34 Aug 10 13:37 /app/blog/uploads/
[www@oldboy64-yl ~]$ touch /app/blog/uploads/lidao-4g.torrent [www@oldboy64-yl ~]$
2. 网站集群安全(网站架构)
image.pngimage.png
6.5 权限补充
-
特殊权限
suid
set uid 在文件的所有者位置上有个s
执行命令的时候 相当于是root
sticky 粘滞位
Linux /tmp用户的 1777
每个人都可以在这个目录中创建文件
希望每个人只能管理自己的文件
image.png - 隐藏属性 给重要配置和命令加上 配置a 命令i
chattr
a 只能追加
[root@oldboy64-yl ~]# lsattr oldboy.txt
---------------- oldboy.txt
[root@oldboy64-yl ~]# chattr +a oldboy.txt
[root@oldboy64-yl ~]# lsattr oldboy.txt
-----a---------- oldboy.txt
[root@oldboy64-yl ~]# cat oldboy.txt
oldboy
oldboy
[root@oldboy64-yl ~]# echo oldboy >> oldboy.txt
[root@oldboy64-yl ~]# echo oldboy> oldboy.txt
-bash: oldboy.txt: Operation not permitted
[root@oldboy64-yl ~]# \rm -f oldboy.txt
rm: cannot remove ‘oldboy.txt’: Operation not permitted
[root@oldboy64-yl ~]# cat oldboy.txt
oldboy
oldboy
oldboy
[root@oldboy64-yl ~]#
i 不可催毁的 无敌的
lsatty
[root@oldboy64-yl ~]# lsattr oldboy.txt
-----a---------- oldboy.txt
[root@oldboy64-yl ~]# chattr -a oldboy.txt
[root@oldboy64-yl ~]# lsattr oldboy.txt
---------------- oldboy.txt
[root@oldboy64-yl ~]# chattr +i oldboy.txt
[root@oldboy64-yl ~]# lsattr oldboy.txt
----i----------- oldboy.txt
[root@oldboy64-yl ~]# \rm -f oldboy.txt
rm: cannot remove ‘oldboy.txt’: Operation not permitted
[root@oldboy64-yl ~]# ehco 123 > oldboy.txt
-bash: oldboy.txt: Permission denied
[root@oldboy64-yl ~]# echo 1 >> oldboy.txt
-bash: oldboy.txt: Permission denied
[root@oldboy64-yl ~]# mv oldboy.txt /tmp/
mv: cannot move ‘oldboy.txt’ to ‘/tmp/oldboy.txt’: Operation not permitted
7. 用户管理体系
7.1 用户有关的文件
- /etc/passwd 存放用户信息
- /etc/group 存放用户组信息(每个组有几个用户 都有谁)
- /etc/shadow 用户密码信息
- /etc/gshadow 用户组密码信息
- /etc/motd 用户登录系统后显示内容
- /etc/profile 配置环境变量 别名
- /etc/profile.d/ 脚本 用户每次登录系统 会执行下面的脚本 .sh
7.2 用户相关目录 /etc/skel/
- 每个新用户家目录的模板
- 每创建1个新用户 系统会把/etc/skel下面的所有文件 复制到用户家目录中
[root@oldboy64-yl ~]# ll -a /etc/skel/
total 28
drwxr-xr-x. 2 root root 80 Aug 8 00:15 .
drwxr-xr-x. 83 root root 8192 Aug 10 11:09 ..
-rw-r--r-- 1 root root 18 Oct 31 2018 .bash_logout #logout 登出 用户每次退出系统 运行这里面的命令
-rw-r--r-- 1 root root 193 Oct 31 2018 .bash_profile #环境变量
-rw-r--r-- 1 root root 231 Oct 31 2018 .bashrc #存放别名
-rw-r--r-- 1 root root 22 Aug 8 00:15 readme.txt
[root@oldboy64-yl ~]#
sed -i 's@export PS1=@#&@g' /etc/profile
useradd alex
su - alex
#\rm -fr /*
7.3 故障案例 命令行-bash-4.2$ 或 -bash-4.1$
[alex@oldboy64-yl ~]$ su - alex
Password:
Last login: Sat Aug 10 15:53:35 CST 2019 on pts/4
-bash-4.2$
-bash-4.2$
-bash-4.2$
- 原因:当前用户家目录下面环境变量相关的文件被删除 .bashrc .bash_profile
- 解决:复制
-bash-4.2$ cp /etc/skel/.bash* ~
-bash-4.2$ logout
[alex@oldboy64-yl ~]$ su - alex
Password:
Last login: Sat Aug 10 15:55:52 CST 2019 on pts/4
[alex@oldboy64-yl ~]$
7.4 用户相关命令
命令 |
---|
useradd |
userdel |
usermod |
groupadd |
id |
w |
last |
lastlog |
- useradd
-u 指定用户的UID
-g 指定主要组 指定用户属于的用户组(不是指定用户的gid)
-G 指定多个用户组
-s 指定用户使用的命令解释器
-M 不创建家目录
创建1个用户 mysql uid 1008 gid 1008 属于root用户组 命令解释器
是/sbin/nologin 不能登陆系统
[root@oldboy64-yl ~]# useradd -u 1008 -g root -s /sbin/nologin -M mysql
- userdel
默认删除用户的时候 不会删除家目录
-r 删除用户家目录及相关文件
在/etc/passwd 中加上注释 - usermod 修改用户信息
-s
-G - id 查询用户ID信息 用户组信息 是否存在
- w 显示谁登陆系统 并在干啥 查负载
- last 显示所有用户 登陆情况 谁 什么时间 在哪里登陆 什么时候离开
- lastlog 显示所有用户最近1次登陆信息
7.5 sudo
- 精确的授予某个用户 可以以root权限运行命令
- sudo(跳板机)
- visudo 授权(root)vi /etc/sudoers
- sudo 普通用户使用sudo+命令 以root身份运行命令
- 给oldboy设置好密码
角色 | root | oldboy |
---|---|---|
功能 | visudo进行授权 | sudo 命令进行测试 |
- 非交互式设置密码
password oldboy
password --stdin 非交互式设置密码 从管道获取密码
[root@oldboy64-yl ~]# echo 123456|passwd --stdin oldboy
Changing password for user oldboy.
passwd: all authentication tokens updated successfully.
[root@oldboy64-yl ~]#
- 给oldboy授予 运行 ls touch命令权限
visudo
#100
[root@oldboy64-yl ~]# grep 'oldboy' /etc/sudoers
oldboy ALL=(ALL) /bin/ls,/bin/touch
- oldboy进行测试
[oldboy@oldboy64-yl ~]$ sudo -l
Matching Defaults entries for oldboy on oldboy64-yl:
!visiblepw, always_set_home, match_group_by_gid,
always_query_group_plugin, env_reset, env_keep="COLORS
DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS",
env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS
LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION
LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY
LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE",
env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET
XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
User oldboy may run the following commands on oldboy64-yl:
(ALL) /bin/ls, /bin/touch
[oldboy@oldboy64-yl ~]$ sudo ls /root/
10.0.0.200_2019-07-24_etc.tar.gz num.txt
111.txt oldboy
1.txt oldboy01.txt
2.txt oldboy02.txt
50万行日志access.zip oldboy03.txt
access.log oldboy04.txt
all.tar.bz2 oldboy05.txt
all.tar.gz oldboy06.txt
anaconda-ks.cfg oldboy07.txt
blog.md5 oldboy08.txt
cmatrix-1.2a oldboy09.txt
cmatrix-1.2a.tar.gz oldboy10.txt
config.txt oldboy1.txt
etc oldboy2.txt
etc.hosts.tar.gz oldboy.txt
etc.tar oldboy.txt.bak
etc.tar.gz oldboy.txt.hard
etc.zip oldboy.txt.soft
file2.txt opt.zip
file.txt P
file.zip paichu.txt
hosts pass.txt
lidao1.txt pass.txt.bak
lidao2.txt police.md5
lidao3.txt proxy.conf
lidao.txt rc_local.tar.gz
lidao.txtn test.txt
log.tar.gz touch
num.tx var.tar
[oldboy@oldboy64-yl ~]$ touch /root/oldboy
touch: cannot touch ‘/root/oldboy’: Permission denied
[oldboy@oldboy64-yl ~]$ sudo touch /root/oldboy
[oldboy@oldboy64-yl ~]$ ls -l /root/oldboy
-rw-r--r-- 1 root root 0 Aug 10 18:18 /root/oldboy
[oldboy@oldboy64-yl ~]$
- 授予/bin/ 所有命令
[root@oldboy64-yl ~]# grep 'oldboy' /etc/sudoers
#oldboy ALL=(ALL) /bin/ls,/bin/touch
oldboy ALL=(ALL) /bin/*
- 授予/bin/ 所有命令排除 rm ,vi vim ,su
[root@oldboy64-yl ~]# grep 'oldboy' /etc/sudoers
#oldboy ALL=(ALL) /bin/ls,/bin/touch
#oldboy ALL=(ALL) /bin/*
oldboy ALL=(ALL) /bin/*,!/bin/rm,!/bin/vi
- 授权所有命令 并且不需要输入密码 (运维 )
[root@oldboy64-yl ~]# grep 'oldboy' /etc/sudoers
#oldboy ALL=(ALL) /bin/ls,/bin/touch
#oldboy ALL=(ALL) /bin/*
#oldboy ALL=(ALL) /bin/*,!/bin/rm,!/bin/vi
oldboy ALL=(ALL) NOPASSWD:ALL
[root@oldboy64-yl ~]#
网友评论