1. 權限模型
linux中一切皆文件,權限即讀(r)、寫(w)、執行(x)這些文件的權限;
而同時權限又與用戶密不可分,權限即某個文件的屬主(owner),屬組(group),以及不在組內的其他用戶(other)分別對此文件的具有的操作權限。
另外,用戶在linux中一切活動都是由進程來完成的,進程是用戶的代理人,一般來說進程由哪個用戶執行,該進程就擁有哪個用戶的權限。
所以,在linux中的權限模型生效機制是,判斷進程發起者是它想要訪問的文件的屬主、屬組還是其他用戶,是哪一類用戶則以這類用戶對此文件的權限來判斷是否具有相應的操作權限,有權限則生效,沒權限則拒絕。(使用"su -c"來運行的進程,擁有變換身份後的用戶的權限。)
2. 普通權限r/w/x
這個比較容易理解,這裏不做介紹,可以參照此[鏈接]。
3. 特殊權限suid/sgid/sticky bit
3.1 suid/sgid
衆所周知,/etc/passwd文件存放的是所有用戶的賬號和基本信息,而用戶口令則加密存放在/etc/shadow中。
像這樣的文件不可能給普通用戶寫權限,而/etc/shadow更是什麼權限都沒給,但是每個用戶都可以使用/bin/passwd命令來更改自己的密碼,這就是通過suid實現的,-rwsr-xr-x中的s。
[root@localhost ~]$ ls -l /bin/passwd /etc/passwd /etc/shadow -rwsr-xr-x. 1 root root 27832 Jun 10 2014 /bin/passwd -rw-r--r-- 1 root root 1763 Mar 8 07:32 /etc/passwd ---------- 1 root root 1144 Mar 8 10:16 /etc/shadow
suid 即讓其他用戶不必變換至屬主身份便可以在運行某進程時擁有該進程程序文件屬主的權限。
[mageedu@localhost tmp]$ ls -l test test.sh -rw------- 1 twoyang twoyang 12 Mar 12 11:06 test -rwsr-xr-x 1 twoyang twoyang 32 Mar 12 11:10 test.sh [mageedu@localhost tmp]$ cat test cat: test: Permission denied [mageedu@localhost tmp]$ cat test.sh #!/bin/bash echo test [mageedu@localhost tmp]$ ./test.sh test [mageedu@localhost tmp]$
sgid同理,不過是針對組來講的。即讓其他用戶不必變換至屬組身份便可以在運行某進程時擁有該進程程序文件屬組的權限。
3.2 sticky bit
/tmp目錄是所有用戶共有的臨時文件夾,所有用戶都擁有讀寫權限。當一個文件夾中的文件所有人都可以讀寫,就會存在文件被別人誤刪的問題。但在可以看到在/tmp中卻並不存在這樣的問題,這就是通過sticky bit實現的,drwxrwxrwt中的t。
[root@localhost ~]# ls -dl /tmp/ drwxrwxrwt. 13 root root 4096 Mar 12 11:19 /tmp/ [root@localhost ~]# su - mageedu [mageedu@localhost tmp]$ ls -l total 1 -rwxrwxrwx 1 twoyang twoyang 12 Mar 12 11:06 test [mageedu@localhost tmp]$ rm -f test rm: cannot remove ‘test’: Operation not permitted
sticky bit 即除了目錄的屬主和root用戶有權限刪除此目錄下的文件,其它用戶不能刪除屬主不是自己的文件。但是修改是可以的,只要文件屬主給了此文件其它用戶可以修改的權限。
3.3 設置特殊權限suid/sgid/sticky bit
增加特殊權限:
suid:chmod u+s xxx sgid: chmod g+s xxx sticky bit : chmod o+t xxx
刪除特殊權限
suid:chmod u-s xxx sgid: chmod g-s xxx sticky bit : chmod o-t xxx
suid/sgid/sticky bit組成一組權限位,類似r/w/x,所以也可以這樣增加特殊權限。
suid:chmod 4755 xxx sgid: chmod 2755 xxx sticky bit : chmod 1777 xxx
這時候,常常會聯想到umask設置爲四位,比如0022,認爲第一位也是跟特殊權限相關,其實不是,第一位的0只是表示後面三位是八進制數。
[root@localhost ~]# umask 1022 -bash: umask: 1022: octal number out of range
可見,1002被解析成了10, 0, 2三個數字,而10不是8進制數值,因此出現了上述所示的錯誤:octal number out of range。
最後,在一些文件設置了特殊權限後,字母不是小寫的s或者t,而是大寫的S和T,那代表此文件的特殊權限沒有生效,是因爲你尚未給它對應用戶的x權限。
[twoyang@localhost tmp]$ ls -dl testdir/ test.sh drwxr--r-T 2 twoyang twoyang 6 Mar 12 23:08 testdir/ -rwSr--r-- 1 twoyang twoyang 0 Mar 12 23:09 test.sh [twoyang@localhost tmp]$ chmod u+x test.sh [twoyang@localhost tmp]$ chmod o+x testdir/ [twoyang@localhost tmp]$ ls -dl testdir/ test.sh drwxr--r-t 2 twoyang twoyang 6 Mar 12 23:08 testdir/ -rwsr--r-- 1 twoyang twoyang 0 Mar 12 23:09 test.sh
4. facl:file access control lists
在原來的u,g,o之外,另一層讓普通用戶能控制賦權給另外的用戶或組的賦權機制。
[root@localhost ~]# ls -l share.sh -rwx------ 1 root root 123 Mar 29 18:09 share.sh [root@localhost ~]# setfacl -m u:twoyang:rx share.sh [root@localhost ~]# ls -l share.sh -rwxr-x---+ 1 root root 123 Mar 29 18:09 share.sh [root@localhost ~]# setfacl -m g:twoyang:x share.sh [root@localhost ~]# getfacl share.sh # file: share.sh # owner: root # group: root user::rwx user:twoyang:r-x group::--- group:twoyang:--x mask::r-x other::--- [root@localhost ~]# setfacl -x u:twoyang share.sh [root@localhost ~]# setfacl -x g:twoyang share.sh [root@localhost ~]# getfacl share.sh # file: share.sh # owner: root # group: root user::rwx group::--- mask::--- other::---