一、sudo提权
1.提权文件
vim /etc/sudoers
....
【 用户 白名单(登录限制) 权限 】
100 root ALL=(ALL) ALL
101 wawa ALL=(ALL) /usr/bin/touch,/usr/bin/mkdir
....
[wawa@c7-100 ~]$ mkdir /wawa
mkdir: 无法创建目录"/wawa": 权限不够
[wawa@c7-100 ~]$ sudo mkdir /wawa
我们信任您已经从系统管理员那里了解了日常注意事项。
总结起来无外乎这三点:
#1) 尊重别人的隐私。
#2) 输入前要先考虑(后果和风险)。
#3) 权力越大,责任越大。
[sudo] wawa 的密码:
[wawa@c7-100 ~]$ ll -d /wawa
drwxr-xr-x 2 root root 6 7月 31 09:33 /wawa
2.查看自己可以有哪些权限
[wawa@c7-100 ~]$ sudo -l
匹配 %2$s 上 %1$s 的默认条目:
!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
用户 wawa 可以在 c7-100 上运行以下命令:
(ALL) /usr/bin/touch, /usr/bin/mkdir
二、系统文件默认权限
[hhh@c7-100 ~]$ vim /etc/profile
....
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022 //umask默认权限
fi
....
-------------------------------------------
gt //大于
ge //大于等于
lt //小于
le //小于等于
eq //等于
ne //不等于
-------------------------------------------
创建目录默认权限:755+022=777 //目录最大权限默认777
创建文件默认权限:644+022=666 //文件默认最大权限666
创建目录的默认权限设置
umask 022 //创建目录的结果就是 777 - 022(umask)=755
创建文件的默认权限设置
umask 022 //创建文件的结果就是 666 - 022(umask)=644
-------------------------------------------
如果说,umask设置为奇数
创建目录不影响,创建文件需要再奇数位再+1
三、系统特殊权限
suid //让所有用户在使用这个命令文件的时候,都拥有属主一样的权利
sgid //让所有用户在使用这个命令文件的时候,都拥有属组一样的权利
sbit //粘滞位,让只有属主能够编辑和删除这个文件或目录,其他用户只能查看
1.suid(4)
只针对二进制命令设置
[root@c7-100 ~]# chmod u+s /usr/bin/cp 或者 chmod 4666 1.txt
[root@c7-100 ~]# ll /usr/bin/cp
-rwsr-xr-x. 1 root root 155176 8月 20 2019 /usr/bin/cp
小s代表有执行权限,大S代表没有执行权限
2.sgid(2)
只针对二进制命令设置
[root@c7-100 ~]# chmod g+s /usr/bin/cp 或者 chmod 2666 1.txt
[root@c7-100 ~]# ll /usr/bin/cp
-rwsr-sr-x. 1 root root 155176 8月 20 2019 /usr/bin/cp
3.sbit粘滞位(1)
一般情况下,这个权限都是给目录设置的
含义:让一个目录下,只有属主能操作文件,其他用户只能看,不能删除
[root@c7-100 ~]# chmod 1777 /test 或者 chmod o+t /test
[root@c7-100 ~]# ll -d /test
drwxrwxrwt. 4 root root 71 7月 25 11:21 /test
四、文件上锁
文件上锁 【chattr +i 文件名 】
[root@c7-100 ~]# chattr +i ceshi.sh
[root@c7-100 ~]# rm -rf ceshi.sh
rm: 无法删除"ceshi.sh": 不允许的操作
查看文件是否上锁【lsattr 文件名】
[root@c7-100 ~]# lsattr ceshi.sh
----i----------- ceshi.sh
文件解锁 【chattr -i 文件名】
[root@c7-100 ~]# chattr -i ceshi.sh
[root@c7-100 ~]# lsattr ceshi.sh
---------------- ceshi.sh
--------------------------------------------
如何避免黑客利用我们的chattr上锁功能命令解锁并对重要文件进行篡改
答:将命令移到其他目录下,并改名在不需要用到时删除执行权限,用的时候再加上执行权限
五、系统的特殊符号
通配符
1.【*】
删除当前目录下的所有(隐藏文件是不删除的)
rm -rf ./*
练习*
创建测试环境
[root@c7-100 test]# touch {1..10}.txt
[root@c7-100 test]# touch {1..10}.doc
#文件名过滤
[root@c7-100 ~]# ls ./*.txt
./10.txt ./1.txt ./2.txt ./3.txt ./4.txt ./5.txt ./6.txt ./7.txt ./8.txt ./9.txt
[root@c7-100 ~]# ls ./*.doc
./10.doc ./1.doc ./2.doc ./3.doc ./4.doc ./5.doc ./6.doc ./7.doc ./8.doc ./9.doc
#只查看中间带0的文件
[root@c7-100 ~]# ls ./*0*
./10.doc ./10.txt
2.【?】匹配任意一个字符
[root@c7-100 ~]# ls ./?.txt //几个问号就是识别几个字符
./1.txt ./2.txt ./3.txt ./4.txt ./5.txt ./6.txt ./7.txt ./8.txt ./9.txt
[root@c7-100 ~]# ls ./??.txt
./10.txt ./19.txt ./28.txt ./37.txt ./46.txt ./55.txt ./64.txt ./73.txt ./82.txt ./91.txt
./11.txt ./20.txt ./29.txt ./38.txt ./47.txt ./56.txt ./65.txt ./74.txt ./83.txt ./92.txt
./12.txt ./21.txt ./30.txt ./39.txt ./48.txt ./57.txt ./66.txt ./75.txt ./84.txt ./93.txt
./13.txt ./22.txt ./31.txt ./40.txt ./49.txt ./58.txt ./67.txt ./76.txt ./85.txt ./94.txt
./14.txt ./23.txt ./32.txt ./41.txt ./50.txt ./59.txt ./68.txt ./77.txt ./86.txt ./95.txt
./15.txt ./24.txt ./33.txt ./42.txt ./51.txt ./60.txt ./69.txt ./78.txt ./87.txt ./96.txt
./16.txt ./25.txt ./34.txt ./43.txt ./52.txt ./61.txt ./70.txt ./79.txt ./88.txt ./97.txt
./17.txt ./26.txt ./35.txt ./44.txt ./53.txt ./62.txt ./71.txt ./80.txt ./89.txt ./98.txt
./18.txt ./27.txt ./36.txt ./45.txt ./54.txt ./63.txt ./72.txt ./81.txt ./90.txt ./99.txt
3.【[]】匹配中括号中任意一个字符
[root@c7-100 ~]# touch {a..z}.doc
#只查看以a或者b开头的文件
[root@c7-100 ~]# ls ./[ab].*
./a.doc ./b.doc
#只查看以a或者b开头的文件【取反】
[root@c7-100 ~]# ls ./[!ab].*
[root@c7-100 ~]# ls ./[^ab].*
./1.txt ./7.txt ./C.doc ./F.doc ./I.doc ./L.doc ./O.doc ./R.doc ./U.doc ./X.doc
./2.txt ./8.txt ./d.doc ./g.doc ./j.doc ./m.doc ./p.doc ./s.doc ./v.doc ./y.doc
./3.txt ./9.txt ./D.doc ./G.doc ./J.doc ./M.doc ./P.doc ./S.doc ./V.doc ./Y.doc
./4.txt ./A.doc ./e.doc ./h.doc ./k.doc ./n.doc ./q.doc ./t.doc ./w.doc ./z.doc
./5.txt ./B.doc ./E.doc ./H.doc ./K.doc ./N.doc ./Q.doc ./T.doc ./W.doc ./Z.doc
./6.txt ./c.doc ./f.doc ./i.doc ./l.doc ./o.doc ./r.doc ./u.doc ./x.doc
#匹配全是英文字符开头的文件
[root@c7-100 ~]# ls ./[a-z].*
./a.doc ./C.doc ./f.doc ./H.doc ./k.doc ./M.doc ./p.doc ./R.doc ./u.doc ./W.doc ./z.doc
./A.doc ./d.doc ./F.doc ./i.doc ./K.doc ./n.doc ./P.doc ./s.doc ./U.doc ./x.doc
./b.doc ./D.doc ./g.doc ./I.doc ./l.doc ./N.doc ./q.doc ./S.doc ./v.doc ./X.doc
./B.doc ./e.doc ./G.doc ./j.doc ./L.doc ./o.doc ./Q.doc ./t.doc ./V.doc ./y.doc
./c.doc ./E.doc ./h.doc ./J.doc ./m.doc ./O.doc ./r.doc ./T.doc ./w.doc ./Y.doc
[root@c7-100 ~]# ls ./[a-Z].*
./a.doc ./C.doc ./f.doc ./H.doc ./k.doc ./M.doc ./p.doc ./R.doc ./u.doc ./W.doc ./z.doc
./A.doc ./d.doc ./F.doc ./i.doc ./K.doc ./n.doc ./P.doc ./s.doc ./U.doc ./x.doc ./Z.doc
./b.doc ./D.doc ./g.doc ./I.doc ./l.doc ./N.doc ./q.doc ./S.doc ./v.doc ./X.doc
./B.doc ./e.doc ./G.doc ./j.doc ./L.doc ./o.doc ./Q.doc ./t.doc ./V.doc ./y.doc
./c.doc ./E.doc ./h.doc ./J.doc ./m.doc ./O.doc ./r.doc ./T.doc ./w.doc ./Y.doc
##############################################################################
[root@c7-100 ~]# touch a b
[root@c7-100 ~]# ls ./[ab]
./a ./b
################################################################################
[root@c7-100 ~]# ls ./[0-9]*
4.【{}】生成序列信息
[root@c7-100 ~]# touch {a..z}.doc
[root@c7-100 ~]# touch {1..10}.doc
[root@c7-100 ~]# touch {001..100}.txt
################################
{1..100..3}
[root@c7-100 ~]# touch {01..10..3}.txt //最后的3是间隔,每到第三个文件创建
[root@c7-100 ~]# ll
总用量 0
-rw-r--r-- 1 root root 0 7月 31 15:06 01.txt
-rw-r--r-- 1 root root 0 7月 31 15:06 04.txt
-rw-r--r-- 1 root root 0 7月 31 15:06 07.txt
-rw-r--r-- 1 root root 0 7月 31 15:06 10.txt
[root@c7-100 ~]# touch {a..z..3}.txt
[root@c7-100 ~]# ll
总用量 0
-rw-r--r-- 1 root root 0 7月 31 15:06 01.txt
-rw-r--r-- 1 root root 0 7月 31 15:06 04.txt
-rw-r--r-- 1 root root 0 7月 31 15:06 07.txt
-rw-r--r-- 1 root root 0 7月 31 15:06 10.txt
-rw-r--r-- 1 root root 0 7月 31 15:07 a.txt
-rw-r--r-- 1 root root 0 7月 31 15:07 d.txt
-rw-r--r-- 1 root root 0 7月 31 15:07 g.txt
-rw-r--r-- 1 root root 0 7月 31 15:07 j.txt
-rw-r--r-- 1 root root 0 7月 31 15:07 m.txt
-rw-r--r-- 1 root root 0 7月 31 15:07 p.txt
-rw-r--r-- 1 root root 0 7月 31 15:07 s.txt
-rw-r--r-- 1 root root 0 7月 31 15:07 v.txt
-rw-r--r-- 1 root root 0 7月 31 15:07 y.txt
#############################################################
#拓展:
[root@c7-100 ~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.bak
[root@c7-100 ~]# ll /etc/sysconfig/network-scripts/ifcfg-eth*
-rw-r--r-- 1 root root 177 7月 25 08:47 /etc/sysconfig/network-scripts/ifcfg-eth0
-rw-r--r-- 1 root root 177 7月 31 15:08 /etc/sysconfig/network-scripts/ifcfg-eth0.bak
[root@c7-100 ~]# echo {1..5}{a..g}
1a 1b 1c 1d 1e 1f 1g 2a 2b 2c 2d 2e 2f 2g 3a 3b 3c 3d 3e 3f 3g 4a 4b 4c 4d 4e 4f 4g 5a 5b 5c 5d 5e 5f 5g
[root@c7-100 ~]# echo {1,2}{a,b}
1a 1b 2a 2b
[root@c7-100 ~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0{,.bak}
[root@c7-100 ~]# ll /etc/sysconfig/network-scripts/ifcfg-eth*
-rw-r--r-- 1 root root 177 7月 25 08:47 /etc/sysconfig/network-scripts/ifcfg-eth0
-rw-r--r-- 1 root root 177 7月 31 15:08 /etc/sysconfig/network-scripts/ifcfg-eth0.bak
系统特殊符号
1.【;】分号
#两个命令的分割符号;
[root@c7-100 ~]# echo 111 ; echo 222
111
222
[root@c7-100 ~]# echo 111 echo 222
111 echo 222
[root@c7-100 ~]# echoo 111 ; echo 222
-bash: echoo: 未找到命令
222
2.【#】注释
shell中的注释
3.【|】管道
将前面的命令执行结果交给后面的
[root@c7-100 ~]# find ./ -name "*.txt" | xargs tar zcvf old.tar.gz
./1.txt
./11.txt
./21.txt
./31.txt
./41.txt
./51.txt
./61.txt
./71.txt
./81.txt
./91.txt
#############################################
[root@c7-100 ~]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@c7-100 ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
4.引号和转义字符(撬棍)
单引号【''】: #所见即所得;
双引号【""】: #可以解析变量或者特殊字符
反引号【``】: #优先执行反引号中的命令;
[root@c7-100 ~]# wahh=123456
[root@c7-100 ~]# echo $wahh
123456
[root@c7-100 ~]# echo "$wahh"
123456
[root@c7-100 ~]# echo "\$wahh"
$wahh
[root@c7-100 ~]# echo '$wahh'
$wahh
####################################
[root@c7-100 ~]# tar zcvf old02.tar.gz `find ./ -name "*.txt"`
./1.txt
./11.txt
./21.txt
./31.txt
./41.txt
./51.txt
./61.txt
./71.txt
./81.txt
./91.txt
5.$?返回上一个命令的值
正确返回0,错误返回非0
10-完