你可能不知道的的linux檔案許可權管理
目錄的 rwx 許可權
當前使用者:vagrant:vagrant
建立testdir
目錄,進入testdir
目錄內。建立檔案test
。
$ mkdir testdir $ cd testdir $ touch test
修改testdir
許可權為000
,嘗試執行ls testdir
$ chmod 000 testdir $ ls testdir/ ls: cannot open directory testdir/: Permission denied
修改testdir
許可權為400
,嘗試執行ls testdir
$ chmod 400 testdir ls -l testdir/ ls: cannot access testdir/test: Permission denied total 0 -????????? ? ? ? ?? test
結果:能夠讀取目錄下檔案列表,但是看不到具體檔案資訊(許可權、大小、使用者組、時間等),儘管當前使用者是/testdir/test
的擁有者
且具有rwx
許可權。
擁有目錄的r
許可權可以讀取目錄下的檔案列表。
繼續,嘗試進入testdir
目錄。
$ cd testdir/ -bash: cd: testdir/: Permission denied
看來r
許可權並不能讓我們具有進入目錄。
我們增加一個x
許可權試試。
~$ chmod 500 testdir/ ~$ cd testdir/ ~/testdir$ ls -l total 0 -rw-rw-r-- 1 vagrant vagrant 0 Nov 19 08:16 test
成功進入。
擁有目錄的x
許可權能夠讓我們進入到目錄下。在此工作目錄下,我們可以檢視檔案列表及檔案的屬性資訊。
嘗試刪除test
檔案或者新建檔案test1
。
~/testdir$ rm test rm: cannot remove ‘test’: Permission denied ~/testdir$ touch test1 touch: cannot touch ‘test1’: Permission denied
擁有目錄的r x
許可權並不能允許我們改變目錄的內容。目錄裡的檔案列表可以看做是目錄的內容。
擁有目錄的w
許可權可以對目錄的內容進行增刪。
~/testdir$ chmod 700 . ~/testdir$ rm test ~/testdir$ touch test1 ~/testdir$ ls -l total 0 -rw-rw-r-- 1 vagrant vagrant 0 Nov 19 08:30 test1
umask
在上面的例子裡,我們建立的新檔案的許可權是664
(-rw-rw-r--),為什麼預設許可權會是664
,我如果想改變新檔案的預設許可權怎麼辦?
控制檯輸入 umask:
$ umask 0002
umask
是許可權的補碼。檔案的預設許可權是666 - umask
。
如果我們建立的檔案不想讓其他使用者有r
許可權,則修改補碼為0006
即可。
~/testdir$ umask 0006 ~/testdir$ touch test2 ~/testdir$ ls -l | grep test2 -rw-rw---- 1 vagrant vagrant 0 Nov 19 08:38 test2
為什麼檔案的預設許可權不是777 - umask
呢?因為新建的檔案預設不具有可執行許可權,所以只考慮rw
許可權的話,這波操作自然是666
了。
目錄預設具有x
許可權,當umask
是0002
時,建立的目錄的預設許可權應該是777 - 0002 = 775
:
~/testdir$ mkdir dir1 ~/testdir$ ls -l | grep dir1 drwxrwxr-x 2 vagrant vagrant 4096 Nov 19 08:39 dir1
特殊許可權
SUID
一般來說檔案許可權是rwx
。我們檢視一下passwd
(修改密碼命令)的許可權:
~/testdir$ ls -l /usr/bin/passwd -rwsr-xr-x 1 root root 47032 May 162017 /usr/bin/passwd
細心點你會發現它的使用者許可權的x
位竟然是s
。這個許可權叫SUID
,僅對二進位制程式有效。
當用戶具有該檔案的執行許可權時,執行該檔案會短暫的獲取該檔案所有者許可權的支援。
比如:所有使用者的密碼存在/etc/shadow
這個檔案裡,且該檔案的許可權預設是-r-------- root root
,僅root
使用者具有強制寫入許可權,那為什麼普通使用者還能修改自己的密碼呢?就是因為passwd
命令具有SUID
許可權,使用者執行該命令時會獲得檔案所有者root
的許可權支援,從而修改自己的密碼。
SGID
當group
的x
位置變成s
時,說明該檔案具有SGID
許可權。
SGID
許可權對二進位制程式有效。類似SUID
,使用者在具有檔案的x
許可權時,執行該檔案,會獲取該檔案所屬使用者組的許可權支援。
除了二進位制程式外,SGID
也可以設定在目錄上。
若使用者對該目錄具有SGID
許可權:
使用者在此目錄下的有效使用者組將會變成該目錄的使用者組。
如果使用者具有該目錄的w
許可權,則使用者在此目錄下建立的檔案的使用者組與此目錄的使用者組相同。
該許可權對於專案開發很重要。
SBIT
該許可權目前只對目錄有效:
當用戶對此目錄具有w,x
許可權,使用者在該目錄下建立資料夾或目錄後,僅自己和root
才有許可權刪除該檔案。
Others
的x
許可權位若為t
,則說明資料夾具有SBIT
許可權。
比如/tmp
目錄:
$ ls -l / | grep tmp drwxrwxrwt4 rootroot4096 Nov 19 09:09 tmp $ sudo -s # touch test root@vagrant-ubuntu-trusty-64:/tmp# exit exit vagrant@vagrant-ubuntu-trusty-64:/tmp$ rm test rm: remove write-protected regular empty file ‘test’? y rm: cannot remove ‘test’: Operation not permitted
如何設定以上三種許可權
如果在普通的許可權設定的“三個數字”前再加一個數字,那前面這個數字就代表這幾個許可權了:
- 4 為 SUID
- 2 為 SGID
- 1 為 SBIT
比如:
# chmod 777 /tmp # ls -l / | grep tmp drwxrwxrwx4 rootroot4096 Nov 19 09:17 tmp # chmod 1777 /tmp # ls -l / | grep tmp drwxrwxrwt4 rootroot4096 Nov 19 09:17 tmp
End。