安全一直計算機領域的熱點話題,Linux系統有非常完善的權限控制來保證文件系統的安全。爲了更加安全的存儲文件,Linux設置了不同層次的權限訪問控制方式。
文件權限就是的文件的訪問權限,具體包括對文件的讀、寫、刪除和執行等。在Linux中每個用戶有不同的權限,普通用戶在自己的家目錄裏對於自己創建的文件有所有的權限,在家目錄之外則可能僅有讀權限。分配好每一個Linux用戶的權限是我們開展工作的第一步,下面我們來看看Linux是怎樣來管理用戶權限的吧。
一、文件都有的三種權限
在Linux中每個文件對應三類用戶都有三種權限:
[root@localhost ~]# touch /tmp/testfile [root@localhost ~]# ll /tmp/testfile -rw-r--r--. 1 root root 0 Aug 3 08:45 /tmp/testfile
我們來依次看看每一部分代表什麼意思吧
-:第一個-代表文件類型,常見的文件類型有普通文件(-),目錄(d),軟鏈接(l),sockets文件(s),管道文件(p)等等
rw-r--r--:第二部分有9個字符,將它依次分成三分分別對應屬主(u)、屬組(g)和其他用戶(o)的權限,每組有r,w,x三種權限。對於文件:r表示可讀(cat,more,less,tail等查看文本內容的命令);w表示可寫和刪除(可用nano,vim等編輯工具更改內容,可用echo追加和覆蓋);x表示可執行。
對於目錄:
r:只能通過ls來查看目錄下的文件,其他操作無法執行
[nieda@localhost testdir]$ ll total 4 dr--r--r--. 2 root root 4096 Aug 3 09:08 test
[nieda@localhost testdir]$ ls test/ ls: cannot access test/config-3.10.0-327.el7.x86_64: Permission denied ls: cannot access test/initramfs-0-rescue-9987c5b50f134797b202165f43b10443.img: Permission denied ls: cannot access test/initramfs-3.10.0-327.el7.x86_64.img: Permission denied ls: cannot access test/initramfs-3.10.0-327.el7.x86_64kdump.img: Permission denied ls: cannot access test/initrd-plymouth.img: Permission denied ls: cannot access test/symvers-3.10.0-327.el7.x86_64.gz: Permission denied ls: cannot access test/System.map-3.10.0-327.el7.x86_64: Permission denied ls: cannot access test/vmlinuz-0-rescue-9987c5b50f134797b202165f43b10443: Permission denied ls: cannot access test/vmlinuz-3.10.0-327.el7.x86_64: Permission denied config-3.10.0-327.el7.x86_64 initramfs-0-rescue-9987c5b50f134797b202165f43b10443.img initramfs-3.10.0-327.el7.x86_64.img initramfs-3.10.0-327.el7.x86_64kdump.img initrd-plymouth.img symvers-3.10.0-327.el7.x86_64.gz System.map-3.10.0-327.el7.x86_64 vmlinuz-0-rescue-9987c5b50f134797b202165f43b10443 vmlinuz-3.10.0-327.el7.x86_64
w:單獨的w權限什麼都不能執行
[nieda@localhost testdir]$ ll total 4 d-w--w--w-. 2 root root 4096 Aug 3 09:08 test
[nieda@localhost testdir]$ rm -f test/config-3.10.0-327.el7.x86_64 rm: cannot remove ‘test/config-3.10.0-327.el7.x86_64’: Permission denied [nieda@localhost testdir]$ mv test aaa mv: cannot move ‘test’ to ‘aaa’: Permission denied [nieda@localhost testdir]$ mkdir test/aaa mkdir: cannot create directory ‘test/aaa’: Permission denied [nieda@localhost testdir]$ cd test/ bash: cd: test/: Permission denied [nieda@localhost testdir]$ ll test/ ls: cannot open directory test/: Permission denied
x:可以切換進入目錄,也可以ls查看文件信息,但必須寫文件全名
[nieda@localhost testdir]$ ll total 4 d--x--x--x. 2 root root 4096 Aug 3 09:08 test
[nieda@localhost test]$ ll ls: cannot open directory .: Permission denied [nieda@localhost test]$ ls config-3.10.0-327.el7.x86_64 -l -rw-r--r--. 1 root root 126426 Aug 3 09:08 config-3.10.0-327.el7.x86_64
對於w的權限,其必須和x搭配使用才能夠發揮作用
[nieda@localhost testdir]$ ll total 4 d-wx-wx-wx. 2 root root 4096 Aug 3 09:08 test
[nieda@localhost testdir]$ rm -rf test/config-3.10.0-327.el7.x86_64 [nieda@localhost testdir]$ ll test/config-3.10.0-327.el7.x86_64 ls: cannot access test/config-3.10.0-327.el7.x86_64: No such file or directory
[nieda@localhost testdir]$ echo aaa >> test/config-3.10.0-327.el7.x86_64 [nieda@localhost testdir]$ cat test/config-3.10.0-327.el7.x86_64 aaa [nieda@localhost testdir]$ touch test/abc [nieda@localhost testdir]$ ll test/abc -rw-rw-r--. 1 nieda nieda 0 Aug 3 09:40 test/abc
在Linux中,rwx可以用八進制來表示,r用4來表示,w是2,x是1
還有一個比較特殊的權限X
對於目錄,加上X權限,目錄裏的子目錄都將有x的權限,裏面的文件如果原來有執行的權限(無論那類用戶)+X後所有的用戶都將有x的權限,否則文件的權限將不做任何的改變:
[root@localhost testdir]# ll total 0 drwxr-xr-x. 3 root root 26 Aug 3 09:53 test [root@localhost testdir]# ll test/ total 0 drwxr-xr-x. 2 root root 6 Aug 3 09:53 dir1 -rw-r--r--. 1 root root 0 Aug 3 09:53 f1
[root@localhost testdir]# chmod +X test [root@localhost testdir]# ll total 0 drwxr-xr-x. 3 root root 26 Aug 3 09:53 test [root@localhost testdir]# ll test/ total 0 drwxr-xr-x. 2 root root 6 Aug 3 09:53 dir1 -rw-r--r--. 1 root root 0 Aug 3 09:53 f1
對於文件,如果原來沒有用戶有執行的權限,則不受X的影響,否則+X後所有的用戶都將有x的權限:
[root@localhost test]# ll total 0 -rw-r--r--. 1 root root 0 Aug 3 09:57 f1 [root@localhost test]# chmod +X f1 [root@localhost test]# ll total 0 -rw-r--r--. 1 root root 0 Aug 3 09:57 f1
[root@localhost test]# chmod g+x f1 [root@localhost test]# ll total 0 -rw-r-xr--. 1 root root 0 Aug 3 09:57 f1 [root@localhost test]# chmod +X f1 [root@localhost test]# ll total 0 -rwxr-xr-x. 1 root root 0 Aug 3 09:57 f1
權限及文件擁有者的管理命令:chmod、chown和chgrp
chmod:更改文件的模式位,可以接多個文件或目錄
選項:
-R:對於目錄,可以遞歸改變其裏面的文件模式
--reference=RFILE FILE...... 參照RFILE的權限,更改後面的文件
chown:更改文件的屬主和屬組,可以接多個文件
常用-R選項,和chmod的-R選項相同
[root@localhost testdir]# ll f1 -rw-r--r--. 1 root root 0 Aug 3 10:54 f1 [root@localhost testdir]# chown nieda: f1 [root@localhost testdir]# ll f1 -rw-r--r--. 1 nieda nieda 0 Aug 3 10:54 f1
[root@localhost testdir]# chown :root f1 [root@localhost testdir]# ll f1 -rw-r--r--. 1 nieda root 0 Aug 3 10:54 f1
如上所示:
當只有nieda: 時將同時改變屬主和屬組,其中冒號可以用.(點號)代替,如果只寫:root將只會改變屬組。目錄也是同樣的效果。
chgrp:更改組所有權,後接多個目錄。-R選項
可以看到,chmod、chown、chgrp的選項幾乎相同,-R和--reference的用法一樣,chmod主要用於更改用戶的權限位,chown改變屬主和屬組,chgrp只能改變屬組
新建文件和目錄的默認權限:umask
[root@localhost testdir]# umask 0022
系統默認的umask值是022,可以才/etc/bashrc中設置
Linux默認創建文件的權限是644,目錄是755。Linux爲了安全將不允許新建的文件有可執行的權限,即其八進制不允許是奇數,如果是奇數則權限加一。
[root@localhost testdir]# umask 135 [root@localhost testdir]# umask 0135 [root@localhost testdir]# touch file [root@localhost testdir]# ll file -rw-r---w-. 1 root root 0 Aug 3 11:12 file [root@localhost testdir]# mkdir dir [root@localhost testdir]# ll -d dir drw-r---w-. 2 root root 6 Aug 3 11:13 dir
文件權限:666減去umask,如上所示將得到531,都是奇數有執行權限必須加一,得到642的權限
目錄權限:777減去umask
umask主要有兩個選項:
[root@localhost testdir]# umask -p umask 0135 [root@localhost testdir]# umask -p >> /etc/bashrc [root@localhost testdir]# tail -1 /etc/bashrc umask 0135
-p選項方便加入到環境變量的配置文件
[root@localhost testdir]# umask -S u=rw,g=r,o=w
-S選項,以ugo的方式顯示,umask也可以用ugo的方式修改
二、文件系統的特殊權限
Linux中普通用戶有很多無法執行的操作,而且生產環境中,root也會被禁止登錄系統,如果系統出現問題怎樣修復呢?爲了讓普通用戶可以臨時執行只有管理員才能執行的命令,Linux設置了特殊權限的機制,總的來說就是u+s,g+s,o+t。
sst:SUID、SGID、Sticky
Linux中的進程權限訪問控制叫做進程的安全上下文:
進程有屬主和屬組,進程對應的可執行二進制文件命令也有屬主和屬組。
進程的屬主和屬組是發起該進程的用戶及其所在的主組
1、任何一個可執行程序文件能不能發起爲進程,取決於發起者是否對該文件有執行權限
2、進程啓動之後,屬主爲發起用戶,屬組爲該用戶所在的組
3、進程訪問文件的權限取決於它的發起者
如果被訪問文件的屬主是該發起者,進程對該文件有屬主權限
如果被訪問文件的屬組是該發起者,進程對該文件有屬組權限
如果發起者是文件的other用戶,進程對該文件有other的權限
可執行文件的SUID權限
當給一個可執行文件賦予u+s的權限,普通用戶執行該命令時,啓動的進程屬主將變爲該二進制文件的屬主
注意:SUID只對可執行的二進制文件有效,對於目錄沒有意義
[nieda@localhost testdir]$ cat /etc/shadow cat: /etc/shadow: Permission denied [nieda@localhost testdir]$ exit exit [root@localhost testdir]# chmod u+s /bin/cat [root@localhost testdir]# ll /bin/cat -rwsr-xr-x. 1 root root 54048 Nov 20 2015 /bin/cat [root@localhost testdir]# su nieda\ > ^C [root@localhost testdir]# su nieda [nieda@localhost testdir]$ cat /etc/shadow root:$6$iSEtT1x0P2vdmDZQ$2CHo49CSt/wXeTRT/hL1Mp3UwaUSYuPzTgKHRBmbgy6FJDU4e/63T8sq9iNrc2wJKmI/7oq5/ks58LXRTdqc./::0:99999:7:::
.........
[root@localhost testdir]# chmod u-s /bin/cat [root@localhost testdir]# ll /bin/cat -rwxr-xr-x. 1 root root 54048 Nov 20 2015 /bin/cat
可執行文件的SGID權限
對於二進制可執行文件:
普通用戶發起進程後,進程屬組變爲二進制文件的屬組,方法同SUID
對於目錄:
默認情況下,用戶創建的文件其屬組是用戶的基本組,當給所在目錄加g+s的權限後,所有用戶在該目錄下創建的文件其屬組將繼承該目錄的屬組。
[root@localhost testdir]# mkdir dir1/dir [root@localhost testdir]# touch dir1/f1 [root@localhost testdir]# ll dir1/ total 0 drw-r---w-. 2 root root 6 Aug 3 12:41 dir -rw-r---w-. 1 root root 0 Aug 3 12:41 f1 [root@localhost testdir]# chmod g+sx dir1/ [root@localhost testdir]# ll -d dir1 drw-r-s-w-. 3 root nieda 25 Aug 3 12:41 dir1 [root@localhost testdir]# mkdir dir1/dir1 [root@localhost testdir]# touch dir1/f2 [root@localhost testdir]# ll dir1/ total 0 drw-r---w-. 2 root root 6 Aug 3 12:41 dir drw-r-S-w-. 2 root nieda 6 Aug 3 12:41 dir1 -rw-r---w-. 1 root root 0 Aug 3 12:41 f1 -rw-r---w-. 1 root nieda 0 Aug 3 12:41 f2 [root@localhost testdir]# ll -d dir1/ drw-r-s-w-. 4 root nieda 45 Aug 3 12:41 dir1/
注意:如果目錄原來的group沒有執行權限,當g+s後目錄的group權限爲S,即該組的成員沒有x的權限,及無法cd進入該目錄,u+s和o+t同樣適用該準則。加g+s後如果默認創建額目錄組沒有x權限,那麼在該目錄下的子目錄組權限是S。
Sticky位
通常如果用戶對一個目錄如果有寫權限(也要加上x的權限啊),那麼該用戶可以刪除該目錄下的任何文件。如果將目錄設置o+t的權限,只有文件的所有者和root可刪除文件,Sticky設置在文件上無意義。
[root@localhost testdir]# ll total 0 drw-r---wx. 2 root root 6 Aug 3 12:58 dir [root@localhost testdir]# touch dir/f{1..5} [root@localhost testdir]# ll dir/ total 0 -rw-r---w-. 1 root root 0 Aug 3 12:58 f1 -rw-r---w-. 1 root root 0 Aug 3 12:58 f2 -rw-r---w-. 1 root root 0 Aug 3 12:58 f3 -rw-r---w-. 1 root root 0 Aug 3 12:58 f4 -rw-r---w-. 1 root root 0 Aug 3 12:58 f5 [root@localhost testdir]# su nieda [nieda@localhost testdir]$ rm -f dir/* #如果目錄沒有讀權限,*將失去作用 [nieda@localhost testdir]$ exit exit [root@localhost testdir]# ll dir/ total 0 -rw-r---w-. 1 root root 0 Aug 3 12:58 f1 -rw-r---w-. 1 root root 0 Aug 3 12:58 f2 -rw-r---w-. 1 root root 0 Aug 3 12:58 f3 -rw-r---w-. 1 root root 0 Aug 3 12:58 f4 -rw-r---w-. 1 root root 0 Aug 3 12:58 f5 [root@localhost testdir]# su nieda [nieda@localhost testdir]$ rm dir/f{1..5} [nieda@localhost testdir]$ exit exit [root@localhost testdir]# ll dir/ total 0
[root@localhost testdir]# chmod o+t dir [root@localhost testdir]# ll total 0 drwxr---wt. 5 nieda nieda 84 Aug 3 14:09 dir [root@localhost testdir]# ll dir/ total 0 drwxr---w-. 2 nieda nieda 6 Aug 3 14:09 dir1 drwxr---w-. 2 nieda nieda 6 Aug 3 14:09 dir2 drwxr---w-. 2 nieda nieda 6 Aug 3 14:09 dir3 -rw-r---w-. 1 root root 0 Aug 3 13:02 f1 -rw-r---w-. 1 root root 0 Aug 3 13:02 f2 -rw-r---w-. 1 root root 0 Aug 3 13:02 f3 -rw-r---w-. 1 root root 0 Aug 3 13:02 f4 -rw-r---w-. 1 root root 0 Aug 3 13:02 f5
[root@localhost testdir]# su nieda [nieda@localhost testdir]$ rm -rf dir/f1 [nieda@localhost testdir]$ rm -rf dir/dir1 [nieda@localhost testdir]$ exit exit [root@localhost testdir]# su mage [mage@localhost testdir]$ rm -f dir/f2 rm: cannot remove ‘dir/f2’: Operation not permitted [mage@localhost testdir]$ rm -rf dir/dir2 rm: cannot remove ‘dir/dir2’: Operation not permitted
其他特殊權限:
chattr :設置特殊權限
+i:不能刪除,改名,更改
+a:只能增加
lsatttr:查看特殊權限
三、訪問控制列表
ACL:Access Control List,可以實現靈活的權限控制
以上說的權限控制其實將所有用戶分成了ugo三類,對於u和g我們可以精確指定,但other的定義太過於寬泛,那麼如何更細緻的管理權限呢?在linux裏我們可以通過ACL來管理某個文件及其特定的用戶和用戶組權限,簡單來說ACL只需掌握三個命令即可:setfacl,getfacl,chacl
https://wiki.archlinux.org/index.php/Access_Control_Lists
centos7默認創建的ext4和xfs有acl的功能,在7之前手動創建的ext4文件系統需要手工添加:
acl生效順序:所有者——自定義用戶——自定義組——其他人
setfacl:設置ACL
setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...
setfacl --restore=file
getfacl:獲取文件的ACL
chacl:更改文件或目錄的ACL
[root@localhost testdir]# getfacl /etc/issue getfacl: Removing leading '/' from absolute path names # file: etc/issue # owner: root # group: root user::rw- group::r-- other::r--
對一個文件設置ACL
[root@localhost testdir]# ll total 0 -rw-r--r--. 1 root root 0 Aug 3 14:58 file
[root@localhost testdir]# setfacl -m u:mage:rwx file [root@localhost testdir]# echo xxx > file [root@localhost testdir]# cat file xxx [root@localhost testdir]# getfacl file # file: file # owner: root # group: root user::rw- user:mage:rwx group::r-- mask::rwx other::r-- [root@localhost testdir]# ll file -rw-rwxr--+ 1 root root 4 Aug 3 15:01 file
可以看到設置ACL以後在其權限位的最後面有個+
setfacl的參數
-m 修改指定文件的acl,不能和-x混合使用
-x 刪除後續參數
-b 刪除所有acl設定參數
-k 移除預設的acl參數
-R 遞歸設置acl參數
-d 預設目錄的acl參數
ACL中的mask值是自定義的最大權限,默認acl有x的權限,其中新建的文件也不會有x的權限
getfaclfile1 | setfacl--set-file=-file2 複製file1的acl權限給file2
mask隻影響除了所有者和other之外的用戶和組的最大權限
Mask需要與用戶的權限進行邏輯與運算後,才能變成有限的權限
用戶或組的設置必須存在於mask權限設定範圍內纔會生效。
--set選項會把原有的ACL項都刪除,用新的替代,需要注意的是一定要包含UGO的設置,不能象-m一樣只是添加ACL就可以.
主要的文件操作命令cp和mv都支持ACL,只是cp命令需要加上-p 參數。但是tar等常見的備份工具是不會保留目錄和文件的ACL信息