1. 程式人生 > 實用技巧 >Linux系統中的特殊許可權以及許可權的優先順序比較

Linux系統中的特殊許可權以及許可權的優先順序比較

stickyid 粘制位

針對目錄: 如果一個目錄stickyid開啟,那麼這個目錄中的檔案,只能被檔案所有人刪除
chmod o+t dir
eg:
mkdir /pub
chmod 777 /pub

su - westos ----> touch /pub/westosfile
exit
su - otherusername --------> touch /pub/usernamefile
rm -fr /pub/usernamefile #可以刪除
rm -fr /pub/westosfile #不屬於自己的檔案也可以刪除
如何解決此問題:
chmod 1777 /pub
chmod o+t /pub

以上兩條命令都可以開啟pub目錄的t許可權
再次在不同使用者下執行命令時,發現屬於自己的檔案能刪除,不屬於自己的檔案不能刪除。
rm: cannot remove ‘westosfile’: Operation not permitted
在這裡插入圖片描述

sgid 強制位

針對目錄: 目錄中新建的檔案自動歸屬到目錄的所屬組中
chmod g+s dir
eg:
mkdir /mnt/westosdir
chmod 777 /mnt/westosdir
chgrp westostest /mnt/westosdir
watch -n1 ls -lR /mnt ##監控/mnt
root —> touch /mnt/westosdir/file ##是誰建立的檔案組就是誰的

chmod g+s /mnt/westosdir
root —> touch /mnt/westosdir/file1 ##file1自動複製了/mnt/westosdir目錄組
#只針對二進位制的可執行檔案(c程式)
#當執行二進位制可執行檔案時都是用檔案擁有組身份執行,和執行使用者無關
eg:
su - westos
/bin/cat
ps ax -o user,group,comm | grep cat ##所有程序中的使用者,使用者組,名字 | 過濾 cat 命令
westos westos cat

用root使用者身份
chmod g+s /bin/cat
su - westos
/bin/cat
ps ax -o user,group,comm | grep cat

westos root cat
在這裡插入圖片描述除去s許可權後的比較

suid 冒險位

#只針對二進位制的可執行檔案(c程式)
#當執行二進位制可執行檔案時都是用檔案擁有者身份執行,和執行使用者無關
chmod u+s file

eg:
su - westos
/bin/cat
ps ax -o user,group,comm | grep cat
westos westos cat

用root使用者身份
chmod u+s /bin/cat
su - westos
/bin/cat
ps ax -o user,group,comm | grep cat
root westos cat
在這裡插入圖片描述

acl許可權列表

Aiccess Control Lists #訪問控制列表
功能:在列表中可以設定特殊使用者對與特殊檔案有特殊許可權
如:-rw-rw---- 1 root caiwu 0 Apr 18 09:03 westosfile

末尾沒有"+"代表acl列表未開啟

如:-rw-rw----+ 1 root caiwu 0 Apr 18 09:03 westosfile

加號出現acl列表功能開啟

#acl列表許可權讀取
getfacl westosfile

顯示內容分析

file: westosfile #檔名稱
owner: root #檔案擁有者
group: root #檔案擁有組
user::rw- #檔案擁有者許可權
user:lg:rw- #特殊指定使用者許可權
group::r-- #檔案擁有組許可權
group:westos:— #特殊指定的使用者組的許可權
mask::rw- #能夠賦予特殊使用者和特殊使用者組的最大許可權閥值
other::r-- #其他人的許可權
注意:當檔案許可權列表開啟,不要用ls -l 的方式來讀取檔案的許可權
acl列表的控制
setfacl -m u:lg:rw westosfile #設定
setfacl -m g:westos:rw westosfile
setfacl -m u::rwx westosfile
setfacl -m g::0 westosfile
setfacl -x u:lg westosfile ##刪除列表中的lee
setfacl -b westosfile #關閉
在這裡插入圖片描述在這裡插入圖片描述

acl 許可權優先順序

擁有者 > 特殊指定使用者 > 許可權多的組 >許可權少的組 > 其他

acl mask 控制
#mask是能夠賦予指定使用者許可權的最大閥值

問題
當設定完畢檔案的acl列表之後用chmod縮小了檔案擁有組的權力
mask會發生變化

恢復:
setfacl -m m:許可權 檔案

acl 列表的預設許可權
setfacl -m u:lee:rwx /mnt/westosdir ##只對於/mnt/westosdir目錄本身生效
setfacl -Rm u:lee:rwx /mnt/westosdir ##對於/mnt/westosdir目錄和目錄中已經存在的內容生效

以上的命令之針對與存在的檔案生效,新建檔案是不會被設定的
setfacl -m d:u:lee:rwx /mnt/westosdir/ ##針對與/mnt/westosdir目錄中新建檔案生效

attr許可權

attr許可權限制所有使用者
i #不能作任何的更改
a #能新增不能刪除

lsattr dir|file ##檢視attr許可權
chattr +i|+a|-i|-a dir|file ##設定attr許可權
在這裡插入圖片描述
在這裡插入圖片描述在這裡插入圖片描述在這裡插入圖片描述