2023.5.31 Linux系统特殊权限

发布时间 2023-05-31 22:59:40作者: 必兮相语--

03.Linux系统特殊权限
1.Linux系统特殊权限
1.1set_uid


1.2set_gid
1.3sticky粘滞位
2.权限属性chattr
3.进程掩码umask
4.特殊权限练习题



1.Linux系统特殊权限
1. suid 以⽂件的所属⽤户身份执⾏命令, ⽽不是以当前⽤户的身份执⾏命令
2.将⽬录设置为 sgid 后续如果在该⽬录下创建⽂件, 都将与该⽬录的所属组保持⼀致
3. sticky 任何⼈都可以在该⽬录下创建⽂件以及⽬录, 但只能删除⾃⼰创建的⽂件和⽬录
4.如何设置特殊权限
⽤符号表示: setuid=u+s; setgid=g+s; sticky=o+t
⽤数值表示: setuid=4; setgid=2; sticky=1


-rwsr-xr-x. 1 root root 54080 Nov 5 2016 /usr/bin/cat
 root⽤户执⾏cat,最终运⾏的身份是root
 bgx⽤户执⾏cat, 最终运⾏的身份是bgx
suid
 root⽤户执⾏,最终运⾏的身份是root
 bgx⽤户执⾏,最终运⾏的身份是root
drwxr-srwx. 2 root devops 29 Jun 14 21:01 /opt/
 bgx⽤户登陆后, 在/opt/⽬录创建⽂件或者⽬录属主和属组是
   最终属主和属组  bgx devops
 xlw⽤户登陆后, 在/opt/⽬录创建⽂件或者⽬录属主和属组是
   最终属主和属组 xlw devops
 root⽤户登陆后, 在/opt/⽬录创建⽂件或者⽬录属主和属组是
   最终属主和属组  root devops
drwxrwxrwt. 7 root root 93 Jun 14 21:47 /tmp/

1.1set_uid
⽂件的执⾏有效身份为⽂件的拥有者,⽽不是执⾏者的身份。

 

演示

//检查passwd⽂件权限,匿名⽤户没有权限查看
[root@wing ~]# chmod 640 /etc/passwd
[root@wing ~]# ll /etc/passwd
-rw-r----- 1 root root 1548 Dec 9 18:45 /etc/passwd
//使⽤普通⽤户⽆法查看
[wing@wing root]$ cat /etc/passwd
cat: /etc/passwd: Permission denied
//给cat命令赋予suid
[root@wing ~]# chmod 4755 /bin/cat
[root@wing ~]# ll /bin/cat
-rwsr-xr-x. 1 root root 48568 Mar 23 2017 /bin/cat
suid  4000 权限字符s(S),⽤户位置上的x位上设置。
授权⽅法:chmod 4755 passwd
   chmod u+s passwd

1.2set_gid
演示案例
sgid授权⽅法
sgid作⽤
1.针对⽤户组权限位修改,⽤户创建的⽬录或⽂件所属组和该⽬录的所属组⼀致。
2.当某个⽬录设置了sgid后,在该⽬录中新建的⽂件不在是创建该⽂件的默认所属组
3.使⽤sgid可以使得多个⽤户之间共享⼀个⽬录的所有⽂件变得简单。
1.3sticky粘滞位
sticky 对⽬录有写权限的⽤户仅仅可以删除⽬录⾥属于⾃⼰的⽂件,不能删除其他⽤户的⽂件
系统中存在的 /tmp ⽬录是经典的粘滞位⽬录,谁都有写权限,因此安全成问题,常常是⽊⻢第⼀⼿跳板。
sticky 授权⽅法
sticky作⽤
1.让多个⽤户都具有写权限的⽬录,并让每个⽤户只能删⾃⼰的⽂件。
2.特殊sticky⽬录表现在others的x位,⽤⼩t表示,如果没有执⾏权限是T
3.⼀个⽬录即使它的权限为"777"如果是设置了粘滞位,除了⽬录的属主和"root"⽤户有权限删除,除此之外
其他⽤户都不允许删除该⽬录。
2.权限属性chattr
设置⽂件属性(权限),针对所有⽤户,包括 root
[root@wing /]# chmod 777 /opt/
[root@wing /]# chmod g+s /opt/
[root@wing /]# su - wing
[wing@wing ~]$ mkdir /opt/sgid_test
[wing@wing ~]$ ll /opt/sgid_test -d
drwxrwsr-x. 2 wing root 4096 Nov 14 00:27 /opt/sgid_test
suid  2000 权限字符s(S),取决于属组位置上的x。
授权⽅法:chmod 2755 directory
   chmod g+s directory
粘滞位   1000 权限字符t(T),其他⽤户位的x位上设置。
授权⽅法:chmod 1755 /tmp
    chmod o+t /tmp
a:让⽂件或⽬录仅可追加内容
i:不得任意更动⽂件或⽬录

3.进程掩码umask
umask ⽤于控制系统权限, 默认系统权限较⼤, 需要靠Umask来变更权限
默认新建⽂件,系统默认最⼤权限为666
默认新建⽬录,系统默认最⼤权限是777
我们在新建⽂件和⽬录的默认权限会受到 umask 的影响, umask 表示要减掉的权限。
创建⽬录权限值为 777-umask
创建普通⽂件权限值为 644-umask
//创建⽂件并设置属性
[root@wing ~]# touch file_a file_i
[root@wing ~]# lsattr file_a file_i
---------------- file_a
---------------- file_i
//设置属性
[root@wing ~]# chattr +a file_a
[root@wing ~]# chattr +i file_i
[root@wing ~]# lsattr file_a file_i
-----a---------- file_a
----i----------- file_i
//a权限, ⽆法覆盖写⼊和删除⽂件
[root@wing ~]# echo "aa" > file_a
bash: file_a: Operation not permitted
[root@wing ~]# rm -f file_a
rm: cannot remove ‘file_a’: Operation not permitted
//a权限, 只能追加, 适⽤于⽇志⽂件
[root@wing ~]# echo "aa" >> file_a
//i权限, ⽆法写⼊, ⽆法删除
[root@wing ~]# echo "i" > file_i
bash: file_i: Permission denied
[root@wing ~]# echo "i" >> file_i
bash: file_i: Permission denied
[root@wing ~]# rm -f file_i
rm: cannot remove ‘file_i’: Operation not permitted
//解除限制
[root@tianyun ~]# chattr -a file100
[root@tianyun ~]# chattr -i file200
umask`涉及到的相关⽂件`/etc/bashrc /etc/profile ~/.bashrc ~/.bash_profile

注意umask影响的范围
shell (vim,touch) --umask--> 新⽂件或⽬录权限
vsftpd --umask--> 新⽂件或⽬录权限
samba --umask--> 新⽂件或⽬录权限
useradd --umask--> ⽤户 HOME
示例1: 在 shell 进程中创建⽂件
示例2: 修改 shell umask 值(临时⽣效)
1.假设umask值为:022(所有位为偶数)
//⽂件的起始权限值
6 6 6 - 0 2 2 = 6 4 4
6 6 6 - 0 0 2 = 6 6 4
2.假设umask值为:045(其他⽤户组位为奇数)
//计算出来的权限。由于umask的最后⼀位数字是5,所以,在其他⽤户组位再加1。
6 6 6 -  0 4 5 = 6 2 1
3.默认⽬录权限计算⽅法
7 7 7 - 0 2 2 = 7 5 5
umask 044  //umask所有位全为偶数时
示例:mkdir d044  //⽬录733
示例:touch f044  //⽂件622
umask 023  //umask值的部分或全部位为奇数时
示例:mkdir d023  //⽬录754
示例:touch f023  //⽂件644
umask 035  //umask值的所有位为奇数时
示例:mkdir d035  //⽬录742
示例:touch f035  //⽂件642
//查看当前⽤户的umask权限
[root@wing ~]# umask
0022
[root@wing ~]# touch file0022
[root@wing ~]# mkdir dir0022
[root@wing ~]# ll -d file0022 dir0022/
drwxr-xr-x 2 root root 6 Jan 24 09:02 dir0022/
-rw-r--r-- 1 root root 0 Jan 24 09:02 file0022

示例3: 修改 shell umask 值(永久⽣效, 强烈不建议修改)
示例4: 通过 umask 决定新建⽤户 HOME ⽬录的权限
示例 5:例如 vsftpd 进程 /etc/vsftpd/vsftpd.conf
4.特殊权限练习题
[root@wing ~]# umask 000
[root@wing ~]# mkdir dir000
[root@wing ~]# touch file000
[root@wing ~]# ll -d dir000 file000
drwxrwxrwx 2 root root 6 Jan 24 09:04 dir000
-rw-rw-rw- 1 root root 0 Jan 24 09:04 file000
[root@wing ~]# vim /etc/profile
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002
else
umask 022
fi
//⽴即在当前 shell 中⽣效
[root@wing ~]# source /etc/profile
[root@wing ~]# vim /etc/login.defs
UMASK 077
[root@wing ~]# useradd dba
[root@wing ~]# ll -d /home/dba/
drwx------. 4 dba dba 4096 3 ⽉ 11 19:50 /home/dba/
[root@tianyun ~]# vim /etc/login.defs
UMASK 000
[root@wing ~]# useradd sa
[root@wing ~]# ll -d /home/sa/
drwxrwxrwx. 4 sa sa 4096 3 ⽉ 11 19:53 /home/sa/
[root@tianyun ~]# yum -y install vsftpd
[root@tianyun ~]# man vsftpd.conf
anon_umask
local_umask
lab permissions setup

考试必考: 创建三个⽤户, 分别为 curly``larry``moe 这些⽤户都是 stooges 组的成员。
这些⽤户帐号密码都为 password
1.要求以上⽤户和组可以在 /home/stooges ⽬录⾥访问,创建,删除⽂件
2.其他⽤户⼀律不允许访问该⽬录
3.在该⽬录下新建的⽂件会⾃动属于 stooges 组拥有
//创建⽤户,组
useradd curly
useradd larry
useradd moe
groupadd stooges
//创建密码
echo "password" |passwd --stdin moe
echo "password" |passwd --stdin larry
echo "password" |passwd --stdin curry
//将⽤户加组
gpasswd -a larry stooges
gpasswd -a moe stooges
gpasswd -a curly stooges
//创建⽬录并配置权限
mkdir /home/stooges
chmod 770 /home/stooges
chown .stooges /home/stooges
chmod g+s /home/stooges