linux用户权限 -> 系统特殊权限

set_uid

运行一个命令的时候,相当于这个命令的所有者,而不是执行者的身份。

suid的授权方法

suid    4000 权限字符s(S),用户位置上的x位上设置。
授权方法:chmod 4755 passwd
        chmod  u+s  passwd

suid的作用

1.让普通用户拥有二进制文件的所属主权限,二进制文件需要有执行权限。
2.如果设置的二进制文件没有执行权限,那么suid的权限显示就是大S。
3.特殊权限suid仅对二进制可执行程序有效,其他文件或目录则无效。
4.如果普通用户需要操作没有权限的文件,为对应的命令赋予Suid权限。
注意: suid双刃剑, 是一个比较危险的功能, 对系统安全有一定的威胁。

set_gid

运行一个命令的时候,相当于是这个命令所在的用户组

sgid授权方法

suid    2000 权限字符s(S),取决于属组位置上的x。
授权方法:chmod 2755  directory
        chmod  g+s  directory

sgid作用

1.针对用户组权限位修改,用户创建的目录或文件所属组和该目录的所属组一致。
2.当某个目录设置了sgid后,在该目录中新建的文件不在是创建该文件的默认所属组
3.使用sgid可以使得多个用户之间共享一个目录的所有文件变得简单。

sticky粘滞位

sticky对目录有写权限的用户仅仅可以删除目录里属于自己的文件,不能删除其他用户的文件
系统中存在的/tmp目录是经典的粘滞位目录,谁都有写权限,因此安全成问题,常常是木马第一手跳板。

sticky授权方法

粘滞位     1000 权限字符t(T),其他用户位的x位上设置。
授权方法:chmod 1755  /tmp
         chmod o+t /tmp

sticky作用

1.让多个用户都具有写权限的目录,并让每个用户只能删自己的文件。
2.特殊sticky目录表现在others的x位,用小t表示,如果没有执行权限是T
3.一个目录即使它的权限为"777"如果是设置了粘滞位,除了目录的属主和"root"用户有权限删除,除此之外其他用户都不允许删除该目录。

查看隐藏属性

[root@Test01 ~]# lsattr /tmp
-------------e- /tmp/100m
其中e为默认值

修改隐藏属性chattr

1)    使用chattr修改
+增加,-减少,=设置仅有

2)    相关参数如下
a    只允许向文件中增加内容(包含root用户)例:chattr +a a.txt
i    不允许修改文件内容(包含root用户)    例:chattr +i a.txt
A    文件或目录每次被访问不会修改atime。可避免I/O过度访问磁盘
s    若删除,则从磁盘空间删除
c    对文件自动压缩,读取是自动解压
u    若删除,还在磁盘中,可以恢复文件
View Code
a:让文件或目录仅可追加内容
i:不得任意更动文件或目录

//创建文件并设置属性
[root@xuliangwei ~]# touch file_a file_i
[root@xuliangwei ~]# lsattr file_a file_i
---------------- file_a
---------------- file_i

//设置属性
[root@xuliangwei ~]# chattr +a file_a
[root@xuliangwei ~]# chattr +i file_i
[root@xuliangwei ~]# lsattr file_a file_i
-----a---------- file_a
----i----------- file_i

//a权限, 无法覆盖写入和删除文件
[root@xuliangwei ~]# echo "aa" > file_a
bash: file_a: Operation not permitted
[root@xuliangwei ~]# rm -f file_a
rm: cannot remove ‘file_a’: Operation not permitted

//a权限, 只能追加, 适用于日志文件
[root@xuliangwei ~]# echo "aa" >> file_a

//i权限, 无法写入, 无法删除
[root@xuliangwei ~]# echo "i" > file_i
bash: file_i: Permission denied
[root@xuliangwei ~]# echo "i" >> file_i
bash: file_i: Permission denied
[root@xuliangwei ~]# rm -f  file_i
rm: cannot remove ‘file_i’: Operation not permitted

//解除限制
[root@tianyun ~]# chattr -a file100 
[root@tianyun ~]# chattr -i file200

 进程掩码umask

umask用于控制系统权限, 默认系统权限较大, 需要靠Umask来变更权限
默认新建文件,系统默认最大权限为666
默认新建目录,系统默认最大权限是777

我们在新建文件和目录的默认权限会受到umask的影响, umask表示要减掉的权限。
创建目录权限值为777-umask
创建普通文件权限值为644-umask

umask涉及到的相关文件/etc/bashrc /etc/profile ~/.bashrc ~/.bash_profile

注意umask影响的范围
shell (vim,touch) --umask--> 新文件或目录权限
vsftpd --umask--> 新文件或目录权限
samba --umask--> 新文件或目录权限
useradd --umask--> 用户 HOME

1.假设umask值为:022(所有位为偶数)
//文件的起始权限值
6 6 6  -  0 2 2  = 6 4 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
示例1: 在shell进程中创建文件
//查看当前用户的umask权限
[root@xuliangwei ~]# umask
0022
[root@xuliangwei ~]# touch file0022
[root@xuliangwei ~]# mkdir dir0022
[root@xuliangwei ~]# 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
示例2: 修改shell umask值(临时生效)
[root@xuliangwei ~]# umask 000
[root@xuliangwei ~]# mkdir dir000
[root@xuliangwei ~]# touch file000
[root@xuliangwei ~]# 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
示例3: 修改shell umask值(永久生效, 强烈不建议修改) 
[root@xuliangwei ~]# vim /etc/profile
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then 
umask 002
else
umask 022
fi

//立即在当前 shell 中生效
[root@xuliangwei ~]# source /etc/profile
示例4: 通过umask决定新建用户HOME目录的权限 
[root@xuliangwei ~]# vim /etc/login.defs
UMASK 077
[root@xuliangwei ~]# useradd dba
[root@xuliangwei ~]# ll -d /home/dba/
drwx------. 4 dba dba 4096 311 19:50 /home/dba/

[root@tianyun ~]# vim /etc/login.defs
UMASK 000
[root@xuliangwei ~]# useradd sa
[root@xuliangwei ~]# ll -d /home/sa/
drwxrwxrwx. 4 sa sa 4096 311 19:53 /home/sa/

特殊权限练习题

创建三个用户, 分别为curlylarrymoe这些用户都是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
View Code

猜你喜欢

转载自www.cnblogs.com/tim1blog/p/9746553.html