十七、linux文件系統上的特殊權限
suid:s表示,sgid:s表示,sticky:t表示
安全上下文
前提:進程有屬主和屬組,文件有屬主和屬組
任何一個可執行程序文件能不能啟動為進程,取決發起者對程序是否擁有執行權限
啟動為進程之后,其進程的屬主為發起者,進程的屬組為發起者所屬的組
進程訪問文件是的權限,取決于進程的發起者
進程的發起者,同文件的屬主,則應用文件屬主權限
進程的發起者,屬于文件屬組,則應用文件屬組權限
應用文件其他權限
可執行文件上suid權限
任何一個可執行程序文件能不能啟動為進程,取決于發起者對程序文件是否擁有執行權限
啟動為進程之后,其進程的屬主為原程序文件的屬主
suid只對二進制可執行程序有效
suid設置在目錄上無意義
chmod u+s file
chmod u-s file
[root@localhost testdir]# ll total 0 -rwxrwxrwx 1 root wang 0 Aug 4 03:33 file1 -rwxrwxrwx 1 root wang 0 Aug 4 03:35 file2 [root@localhost testdir]# chmod u+s file1 [root@localhost testdir]# ll total 0 -rwsrwxrwx 1 root wang 0 Aug 4 03:33 file1 -rwxrwxrwx 1 root wang 0 Aug 4 03:35 file2 [root@localhost testdir]# chmod u-s file1 [root@localhost testdir]# ll total 0 -rwxrwxrwx 1 root wang 0 Aug 4 03:33 file1 -rwxrwxrwx 1 root wang 0 Aug 4 03:35 file2
可執行文件上sgid權限
任何一個可執行程序文件能不能啟動為進程,取決發起者對程序文件是否擁有執行權限
啟動文件為進程之后,其進程的屬主為源程序文件的屬組
chmod g+s file
chmod g-s file
[root@localhost testdir]# chmod g+s file1 [root@localhost testdir]# ll total 0 -rwxrwsrwx 1 root wang 0 Aug 4 03:33 file1 -rwxrwxrwx 1 root wang 0 Aug 4 03:35 file2 [root@localhost testdir]# chmod g-s file1 [root@localhost testdir]# ll total 0 -rwxrwxrwx 1 root wang 0 Aug 4 03:33 file1 -rwxrwxrwx 1 root wang 0 Aug 4 03:35 file2
目錄上的sgid權限
默認情況下,用戶創建文件是,其屬組為此用戶所屬的主組
一旦某目錄被設定了sgid,則對此目錄有寫權限的和用戶在此目錄中創建的文件所屬的組為此目錄的屬組
通常用于創建一個協作目錄
chmod g+s file
chmod g-s file
[root@localhost testdir]# mkdir 1234 [root@localhost testdir]# ll -a total 8 drwxr-xr-x 3 1000 user1 41 Aug 4 06:56 . dr-xr-xr-x. 18 root root 4096 Aug 4 00:17 .. drwxr-xr-x 2 root root 6 Aug 4 06:56 1234 -rw-r--r--+ 1 root root 0 Aug 4 04:49 file1 -rwxrwxrwx 1 root wang 4 Aug 4 04:32 file2 [root@localhost testdir]# chmod o+w 1234 [root@localhost testdir]# useradd 1 [root@localhost testdir]# su - 1 [1@localhost ~]$ cd /testdir/1234 [1@localhost 1234]$ touch file1 [1@localhost 1234]$ ll file1 -rw-rw-r-- 1 1 1 0 Aug 4 06:58 file1 [1@localhost 1234]$ exit logout [root@localhost testdir]# chmod g+s 1234 [root@localhost testdir]# su - 1 Last login: Thu Aug 4 06:57:23 CST 2016 on pts/1 [1@localhost ~]$ cd /testdir/1234 [1@localhost 1234]$ touch file2 [1@localhost 1234]$ ll total 0 -rw-rw-r-- 1 1 1 0 Aug 4 06:58 file1 -rw-rw-r-- 1 1 root 0 Aug 4 06:58 file2
suicky位
具有寫權限的用戶通常用戶可以刪除該目錄中的任何文件,無論該文件的權限或擁有權
在目錄設置sticky位,只有文件的所有者或root可以刪除該文件
sticky設置在文件上無意義
chmod o+s file
chmod o-s file
特殊權限數字法
suid:chmod 4777 file
sgid:chmod 2777 file
sticky:chmod 1777 dir
權限為映射
suid:user,占據屬主的執行權限位
s:屬主擁有x權限
S:屬主沒有x權限
sgid:group,占據屬組的執行權限位
s:group屬組擁有x權限
S:group沒有x權限
sticky:other,占據other的執行權限位
t:other擁有x權限
T:other沒有x權限
設置文件特定屬性
chattr option file
+A:鎖定訪問時間,訪問不刷新時間
+i:鎖定文件不能刪除,改名,更改
+a:鎖定文件,能看,能修改,不能改名,不能刪除
lsattr 顯示特定屬性
作用:避免誤操作,誤刪除。
[root@localhost testdir]# chattr +i file2 [root@localhost testdir]# rm -rf file2 rm: cannot remove ‘file2’: Operation not permitted [root@localhost testdir]# echo "xxx" > file2 -bash: file2: Permission denied [root@localhost testdir]# lsattr file2 ----i----------- file2
十八、訪問控制列表
acl:access control list。實現靈活的權限管理
除了文件的所有者,所屬組和其他人,可以對更多的用戶設置權限
Centos7默認創建的xfs和ext4文件系統有acl功能
Centos7之前版本,默認手工創建的ext4文件系統無acl功能
acl生效順序:所有者,自定義用戶,自定義組,其他人
setfacl option (d)(ug):username:權限 file
u:用戶
g:組
d:在這個目錄下新建的文件有acl權限
-m:修改acl權限
-x:刪除acl權限
-M:調用腳本修改acl權限
-X:調用腳本刪除acl權限
useradd wangwenjie [root@localhost testdir]# touch file1 [root@localhost testdir]# setfacl -m g:wangwenjie:rwx file1 [root@localhost testdir]# getfacl file1 # file: file1 # owner: root # group: root user::rw- group::r-- group:wangwenjie:rwx mask::rwx other::r-- [root@localhost testdir]# setfacl -x g:wangwenjie file1 [root@localhost testdir]# getfacl file1 # file: file1 # owner: root # group: root user::rw- group::r-- mask::r-- other::r--
setfacl -k dir:刪除默認acl權限
setfacl -b file:清除所有acl權限
[root@localhost testdir]# ll total 0 -rw-r--r--+ 1 root root 0 Aug 4 07:10 file1 [root@localhost testdir]# setfacl -b file1 [root@localhost testdir]# ll total 0 -rw-r--r-- 1 root root 0 Aug 4 07:10 file1
getfacl:查看特殊權限
getfacl file1 | setfacl –set-file=-file2 復制file的acl權限給file2
[root@localhost testdir]# setfacl -m g:wangwenjie:rwx file1 [root@localhost testdir]# getfacl file1 # file: file1 # owner: root # group: root user::rw- group::r-- group:wangwenjie:rwx mask::rwx other::r-- [root@localhost testdir]# touch file2 [root@localhost testdir]# getfacl file1 | setfacl --set-file=- file2 [root@localhost testdir]# getfacl file2 # file: file2 # owner: root # group: root user::rw- group::r-- group:wangwenjie:rwx mask::rwx other::r--
acl文件上的group權限是mask值(自定義用戶,自定義組,擁有組的最大權限),而非傳統的組權限。
默認acl權限給x,文件也不會x權限
bash cal 不能刪除
mask只影響除所有者和other的之外的人和組的最大權限
nask需要與用戶的權限進行邏輯與運算后,才能變成有限的權限
用戶或組的設置存在于mask權限設定范圍內才會生效
setfacl -m mask::權限 file
–set選項會把所有的acl想都刪除,用新的代替,需要注意的是一定要包含ugo的設置,不能像-m一樣只是添加acl就可以
setfacl –set u::權限,g::權限,o::權限 file
備份和恢復acl
主要的文件操作命令cp和mv都支持acl,只是cp命令需要加上-p參數,但是tar等常見的備份工具是不會保留目錄和文件的acl信息。
cp -p 保留權限
getfacl -R 目錄 > file1 把目錄里的acl權限備份到file1文件里
setfacl -R -b 目錄 刪除目錄的所有acl權限
setfacl -R –set-file=file1 目錄 通過file1文件把目錄的acl權限還原
原創文章,作者:DYW,如若轉載,請注明出處:http://www.www58058.com/28960
文章的層次結構清晰明了,內容豐滿,有理論有實踐。