1. 程式人生 > >linux系統上文件的特殊權限介紹

linux系統上文件的特殊權限介紹

文件的特殊權限位及facl

1、特殊文件權限示例

[root@localhost test]# ls -ld /bin/passwd /tmp/

-rwsr-xr-x. 1 root root 27832 Jun 10 2014 /bin/passwd

drwxrwxrwt. 10 root root 247 Oct 28 22:17 /tmp/


2、特殊權限介紹:SUID、SGID、STICKY

安全上下文:

進程以某用戶的身份進行運行,進程是發起此進程用戶的代理,因此以此用戶的身份和權限完成所有操作。

權限的匹配模型:

進程的屬主,是否為被訪問文件的屬主,如果是,則應用屬主權限;否則,判斷進程的屬主,是否屬於訪問文件的屬組,如果是,則應用屬組權限;否則應用其他用戶權限;


SUID:

默認情況下,用戶發起的進程,進程的屬主是其發起者;因此,其以發起者的身份運行。


SUID的功能:

用戶運行某程序時,如果此程序擁有SUID權限,那些程序運行為進程時,其進程屬主不是發起者,而是程序文件自己的屬主。

SGID的功能:

當目錄屬組有寫權限且有SGID權限時,那麽所有屬於此目錄的屬組且以屬組身份在此目錄中新建文件或目錄時,此文件的屬組不是用戶的基本組,而是此目錄的屬組。

管理SUID權限:

chmod u+s file

chmod u-s file

管理SGID權限:

chmod g+s file

chmod g-s file

SUID權限展示位置:在屬主的執行權限位

如果屬主原本有執行權限,顯示為小寫“s”;

如果屬主原本沒有執行權限,則顯示為大寫“S”

SUID權限展示位置:在屬組的執行權限位

如果屬組原本有執行權限,顯示為小寫“s”;

如果屬組原本沒有執行權限,則顯示為大寫“S”

[root@localhost test]# cp /bin/ls /tmp/test/sls

[root@localhost test]# ll sls

-rwxr-xr-x. 1 root root 117656 Oct 29 21:50 sls

[root@localhost test]# chmod u+s sls

[root@localhost test]# ll sls

-rwsr-xr-x. 1 root root 117656 Oct 29 21:50 sls

[root@localhost test]# su user

bash-4.2$ /tmp/test/sls -l sls

-rwsr-xr-x. 1 root root 117656 Oct 29 21:50 sls



[root@localhost tmp]# ls -ld test/

drwxrwxrwx. 2 user user 128 Oct 29 21:50 test/

[root@localhost tmp]# chmod g+s test/

[root@localhost tmp]# ll -d test/

drwxrwsrwx. 2 user user 128 Oct 29 21:50 test/

[root@localhost tmp]# su lily

bash-4.2$ touch file

bash-4.2$ ls -l /tmp/test/file

-rw-r--r--. 1 lily user 0 Oct 29 22:05 /tmp/test/file //有SUID權限


bash-4.2$ ls -ld /tmp/test1/lily

drwxr-xr-x. 2 lily lily 6 Oct 29 22:08 /tmp/test1/lily //沒有SUID權限

STICKY功能:

對於屬組或全局可寫的目錄,組內的所有用戶或系統上的所有用戶對在此目錄中都能創建新文件或刪除所有已有的文件;如果為此目錄設置sticky權限,則每個用戶都能創建新文件,且只能刪除自己的文件。



註意:默認系統上的/tmp和/var/tmp目錄默認均有sticky權限;

管理STICKY權限:

chmod o+t file

chmod o-t file


STICKY權限展示位置:在其他用戶的執行權限位

如果其他用戶原本有執行權限,顯示為小寫“t”

如果其他用戶原本沒有執行權限,則顯示為大寫“T”


[root@localhost tmp]# mkdir sticky

[root@localhost tmp]# chmod o+t sticky/

[root@localhost tmp]# ls sticky/ -ld

drwxrwxrwt. 2 root root 6 Oct 29 22:15 sticky/

[root@localhost tmp]#

[root@localhost tmp]# su lily

bash-4.2$ cd /tmp/sticky

bash-4.2$ touch lily

bash-4.2$ cd /tmp/sticky1

bash-4.2$ touch lily1

bash-4.2$ exit


[root@localhost tmp]# su lucy

[lucy@localhost tmp]$ cd /tmp/sticky

[lucy@localhost sticky]$ touch lucy

[lucy@localhost sticky]$ ll

total 0

-rw-r--r--. 1 lily lily 0 Oct 29 22:18 lily

-rw-rw-r--. 1 lucy lucy 0 Oct 29 22:21 lucy

[lucy@localhost sticky]$ rm lily

rm: remove write-protected regular empty file ‘lily’? y

rm: cannot remove ‘lily’: Operation not permitted

[lucy@localhost sticky]$


[lucy@localhost sticky]$ cd /tmp/sticky1

[lucy@localhost sticky1]$ ll

total 0

-rw-r--r--. 1 lily lily 0 Oct 29 22:19 lily1

[lucy@localhost sticky1]$ touch lucy1

[lucy@localhost sticky1]$ ll

total 0

-rw-r--r--. 1 lily lily 0 Oct 29 22:19 lily1

-rw-rw-r--. 1 lucy lucy 0 Oct 29 22:23 lucy1

[lucy@localhost sticky1]$ rm lily1

rm: remove write-protected regular empty file ‘lily1’? y

[lucy@localhost sticky1]$ ll

total 0

-rw-rw-r--. 1 lucy lucy 0 Oct 29 22:23 lucy1

[lucy@localhost sticky1]$












管理特殊權限的另一種方法:(八進制數字表示,0-7)

SUID SGID STICKY

2^2 2^1 2^0

特殊權限位在最高位置展示,如:chmod 1777 file1

基於八進制方式賦權時,可於默認的三位八進制數字左側在加一位八進制數字。

umask默認為四位。



3、facl:file access control lists,文件訪問控制列表

文件的額外賦權機制:

在原有的u、g、o之外,另一層讓普通用戶能控制賦權給另外的用戶或組的賦權機制。


查看文件的facl:

getfacl [-aceEsRLPtpndvh] file ...

getfacl [-aceEsRLPtpndvh] -

如:

[root@localhost tmp]# getfacl aaa

# file: aaa

# owner: root

# group: root

user::rw-

group::r--

other::r--


設置文件的facl:

setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...

setfacl --restore=file


setfacl -m u:USERNAME:MODE FILE

setfacl -m g:GROUPNAME:MODE FILE

如:

[root@localhost test]# setfacl -m u:user:rwx a.dan

[root@localhost test]# getfacl a.dan

# file: a.dan

# owner: root

# group: root

user::rw-

user:user:rwx

group::r--

mask::rwx

other::r--



撤銷文件的facl:

setfacl -x u:USERNAME FILE

setfacl -x g:GROUPNAME FILE

如:

[root@localhost test]# setfacl -x g:root b.dan


如果文件設置了facl,用ls -l命令則會顯現“+”

[root@localhost test]# ll

total 0

-rw-r--r--. 1 basher basher 0 Oct 28 21:28 abfstab.dan

-rw-rwxr--+ 1 root root 0 Oct 28 21:28 a.dan



有了facl的安全上下文:

先是檢查屬主,再檢查屬主facl

先是檢查屬組,再檢查屬組facl

然後在檢查其他;

linux系統上文件的特殊權限介紹