1. 程式人生 > >linux用戶權限 -> 系統特殊權限

linux用戶權限 -> 系統特殊權限

威脅 log 無法刪除 lsa 控制系統 swd 數字 sticky useradd

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

linux用戶權限 -> 系統特殊權限