Linux 文件系統上的權限

文件系統上的權限是指文件系統上的文件和目錄的權限,由于Linux是一種多用戶的操作系統,而且允許同一時間登錄多個用戶操作,所以我們就需要一定的管理機制來對限定不同用戶對同一文件或目錄的操作權限。

文件系統上的權限主要針對三類對象(訪問者)進行定義:
owner:屬主 u; g 屬組 group; o: 其它 other
每個文件針對每類訪問者都定義了三種權限:r:讀 w:寫 x:執行

1)對文件來說:
r: 可讀,可以使用類似cat等命令查看文件內容
w: 可寫,可以編輯此文件,并修改內容
x: 可執行,eXecutable,可以在命令提示符下當作命令提交給內核運行

[root@centos7 ~]#ll bin/backup.sh     
-rwxr-xr-x. 1 root root 43 Jun  2 17:48 bin/backup.sh

以上信息可以看出,root用戶對backup.sh腳本文件有讀寫執行的權限,而root組的成員和其它用戶有讀和執行的權限

2)對于目錄的權限,我們可以通過一系列實驗來總結出來,比如先設定其它用戶對目錄是只讀權限,再看其它用戶對目錄及其下級文件的各個操作權限(先不考慮目錄下的文件的權限影響,假定對其它用戶有rwx權限)

[root@centos7 ~]#ll /app
total 4
-rw-r--r--. 1 root root  0 Jun  4 11:14 test1.txt
-rw-r--r--. 1 root root 21 Jun  4 11:55 test.txt

對于/app目錄來,ffu用戶為其它用戶,對目錄只讀
1.ffu是否可以查看目錄及下級文件呢?

[ffu@centos7 ~]$cd /app
-bash: cd: /app: Permission denied
[ffu@centos7 ~]$ls -l /app
ls: cannot access /app/test1.txt: Permission denied
ls: cannot access /app/test.txt: Permission denied
total 0
-????????? ? ? ? ?            ? test1.txt
-????????? ? ? ? ?            ? test.txt
[ffu@centos7 ~]$cat /app/test.txt
cat: /app/test.txt: Permission denied

可以看出ffu可以執行ls以列出目錄下的文件列表,但不能cd如目錄和查看文件信息及內容

2.ffu是否能對目錄下級文件進行修改呢?

[ffu@centos7 ~]$vi /app/test.txt
                                                                     
~                                                                                  
"/app/test.txt" [Permission Denied]                              0,0-1         All
[ffu@centos7 ~]$echo hehe >> /app/test.txt
-bash: /app/test.txt: Permission denied
[ffu@centos7 ~]$touch /app/test2.txt
touch: cannot touch ‘/app/test2.txt’: Permission denied

可以看出ffu不能對目錄下的文件進行修改,也不能創建新文件
3.ffu是否能刪除目錄下級文件呢?

[ffu@centos7 ~]$rm /app/test.txt
rm: cannot remove ‘/app/test.txt’: Permission denied

可以看出ffu沒有權限刪除/app目錄下的文件

類似的我們也可以設置/app目錄對其它用戶權限為-w-;–r;rw-…總結如下:

 Linux 文件系統上的權限

從上面的實驗結果,我們可以知道對目錄來說:
r: 可以對此目錄執行ls以列出內部的所有文件,但不能訪問文件
w: 可以在此目錄創建文件,修改文件名字,刪除目錄下的文件,但是要配合x權限才生效
x: 可以使用cd切換進此目錄,但可以cat /file 訪問查看文件(已知文件名);配合r權限可使用ls -l查看內部文件的詳細信息
這里可以看出能不能刪除文件是與文件權限是沒有關系的,而是與其上級目錄權限有關。實際上,因為目錄的數據塊儲存的是其下級的文件名,所以我們刪除的只是文件名,文件內容的數據塊還在。
對于目錄來說,還有個X權限,是x的特殊形式,它允許只對目錄及下級目錄加執行權限,而文件不會增加。舉例來說:
要對目錄/app及下級目錄/dir/dir1增加對其它用戶的執行權限,而對文件不加

[root@centos7 app]#ll -d /app
drwxr-xr--. 5 root root 4096 Jun  4 13:13 /app
[root@centos7 app]#ll /app
total 16
drwxr--r--. 2 root root 4096 Jun  4 13:06 dir
drwxr--r--. 2 root root 4096 Jun  4 13:13 dir2
-rw-r--r--. 1 root root    0 Jun  4 11:14 test1.txt
-rw-r--r--. 1 root root   21 Jun  4 11:55 test.txt
[root@centos7 app]#chmod -R o+X /app
[root@centos7 app]#ll /app          
total 16
drwxr--r-x. 2 root root 4096 Jun  4 13:06 dir
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir2
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir3
-rw-r--r--. 1 root root    0 Jun  4 11:14 test1.txt
-rw-r--r--. 1 root root   21 Jun  4 11:55 test.txt
[root@centos7 app]#ll -d /app
drwxr-xr-x. 5 root root 4096 Jun  4 13:13 /app

這里其實是有一個條件的:目錄下文件ugo都沒有x權限才可以,否則全部加上x權限。以test1.txt為例:

[root@centos7 app]#ll
total 16
drwxr--r-x. 2 root root 4096 Jun  4 13:06 dir
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir2
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir3
-rw-r-xr--. 1 root root    0 Jun  4 11:14 test1.txt
-rw-r--r--. 1 root root   21 Jun  4 11:55 test.txt
[root@centos7 app]#chmod -R o+X /app
[root@centos7 app]#ll
total 16
drwxr--r-x. 2 root root 4096 Jun  4 13:06 dir
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir2
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir3
-rw-r-xr-x. 1 root root    0 Jun  4 11:14 test1.txt
-rw-r--r--. 1 root root   21 Jun  4 11:55 test.txt

3)權限位的八進制數表示

Linux 文件系統上的權限


Linux文件系統上的特殊權限
除了上文提到的權限模型r,w,x;在Linux文件系統上對應ugo位又有三個特殊權限:SUID、SGID、Sticky
一、安全上下文
前提:進程有屬主和屬組;文件有屬主和屬組
(1) 任何一個可執行程序文件能不能啟動為進程,取決發起者對程序文件是否擁有執行權限
(2) 啟動為進程之后,其進程的屬主為發起者,進程的屬組為發起者所屬的組
(3) 進程訪問文件時的權限,取決于進程的發起者
    (a) 進程的發起者,同文件的屬主:則應用文件屬主權限
    (b) 進程的發起者,屬于文件屬組:則應用文件屬組權限
    (c) 應用文件“其它”權限
例如:
1.用戶:ffu
    可執行文件:-rwxr-xr-x. 1 root root 115K Nov  6  2016 /bin/ls
    ffu能不能啟動為進程,首先他不是可執行文件的屬主,其次不屬于root組,所以只能以other身份運行,other用戶是具有執行權限;執行后,ls進程的屬主為ffu(發起者)
2.進程:屬主ffu 屬組ffu
    對象:-rw-r–r–. 1 root root 21 Jun  4 11:55 /app/test.txt
    進程訪問文本test.txt的時候,首先對比進程和文本文件的屬主是否一樣,為否,則看屬組;注意不是對比兩個屬組,而是看進程的運行身份ffu是否屬于文本文件的屬組root,為否,以other身份訪問文件。
    以上都是嚴格按照順序,一旦匹配到不會向下匹配

二、SUID
占據的是屬主的執行權限位;SUID只對二進制可執行程序有效,設置在目錄上無意義
與上面所提到的不一樣,對于設定了SUID的可執行文件啟動為進程之后,其進程的屬主為原程序文件的屬主,而不再是進程的發起者
權限設定:
chmod u+s FILE…
chmod u-s FILE…
權限位的八進制數表示為4

舉例:
設定test.txt只對root有讀權限,則ffu作為文件的other用戶,不能通過cat來訪問文件

[ffu@centos7 app]$ll test.txt
-r--------. 1 root root 21 Jun  4 11:55 test.txt
[ffu@centos7 app]$/app/cat test.txt
/app/cat: test.txt: Permission denied

為/app/cat文件加上SUID后,ffu可以訪問文件test.txt了,因為啟動為進程之后,進程的屬主是cat文件的屬主即root

[root@centos7 app]#chmod u+s /app/cat
[root@centos7 app]#ll /app/cat
-rwsr-xr-x. 1 root root 54080 Jun  4 14:58 /app/cat
[ffu@centos7 app]$/app/cat test.txt
This is a test file.

三、SGID
占據的是屬組的執行權限位;權限位的八進制數表示為2
1.對二進制可執行文件
對于設定了SGID的可執行文件啟動為進程之后,其進程的屬主雖然仍是進程的發起者,但是繼承了原程序文件的屬組的權限
權限設定:
chmod g+s FILE…
chmod g- s FILE…

舉例:
設定設定test.txt只對屬組root有讀權限,則ffu作為文件的other用戶,不能通過cat來訪問文件;
為/app/cat文件加上SGID后,ffu可以訪問文件test.txt了,因為啟動為進程之后,ffu繼承了原程序文件屬組的權限

[root@centos7 ~]#chmod g+s /app/cat
[root@centos7 ~]#ll /app/cat
-rwxr-sr-x. 1 root root 54080 Jun  4 14:58 /app/cat
[ffu@centos7 ~]$/app/cat /app/test.txt
This is a test file.

2.對目錄
默認情況下,用戶創建文件時,其屬組為此用戶所屬的主組。一旦某目錄被設定了SGID,則對此目錄有寫權限的用戶在此目錄中創建的文件所屬的組為此目錄的屬組;通常用于創建一個協作目錄
權限設定:
chmod g+s DIR…
chmod g-s DIR…
舉例:
ffu在/app/dir目錄下創建文件,屬主屬組均為ffu

[ffu@centos7 app]$ll -d dir 
drwxrwxr-x. 2 ffu ffu 4096 Jun  4 16:56 dir
[ffu@centos7 app]$touch dir/f{1,2}
[ffu@centos7 app]$ll dir
total 0
-rw-rw-r--. 1 ffu ffu 0 Jun  4 17:01 f1
-rw-rw-r--. 1 ffu ffu 0 Jun  4 17:01 f2

把user1加到ffu組中,user1在/app/dir目錄下創建文件,屬主屬組均為user1

[root@centos7 ~]#groupmems -g ffu -a user1
[user1@centos7 dir]$touch f{3,4}
[user1@centos7 dir]$ll
total 0
-rw-rw-r--. 1 ffu   ffu   0 Jun  4 17:01 f1
-rw-rw-r--. 1 ffu   ffu   0 Jun  4 17:01 f2
-rw-rw-r--. 1 user1 user1 0 Jun  4 17:04 f3
-rw-rw-r--. 1 user1 user1 0 Jun  4 17:04 f4

為/app/dir目錄加上SGID后,user1在/app/dir目錄下創建文件f5,屬組均為ffu即目錄dir的屬組

[root@centos7 ~]#chmod g+s /app/dir
[root@centos7 ~]#ll /app/dir -d    
drwxrwsr-x. 2 ffu ffu 4096 Jun  4 17:04 /app/dir
[user1@centos7 dir]$touch f5
[user1@centos7 dir]$ll
total 0
-rw-rw-r--. 1 ffu   ffu   0 Jun  4 17:01 f1
-rw-rw-r--. 1 ffu   ffu   0 Jun  4 17:01 f2
-rw-rw-r--. 1 user1 user1 0 Jun  4 17:04 f3
-rw-rw-r--. 1 user1 user1 0 Jun  4 17:04 f4
-rw-rw-r--. 1 user1 ffu   0 Jun  4 17:08 f5

四、Sticky
占據的是other的執行權限位;sticky 設置在文件上無意義
權限設定:
chmod o+t DIR…
chmod o-t DIR…
權限位的八進制數表示為1
舉例:
ffu和user1對/app/dir目錄均有寫權限,他們對目錄下所以文件都具有刪除權限

[user1@centos7 dir]$rm -v f2
removed ‘f2’
[ffu@centos7 app]$rm -v dir/f4
removed ‘dir/f4’
[root@centos7 ~]#rm -v /app/dir/f5
removed ‘/app/dir/f5’

實際上,有寫權限的目錄通常用戶可以刪除該目錄中的任何文件,無論該文件的權限或擁有權

在目錄設置Sticky位,只有文件的所有者、目錄的屬主、root可以刪除該文件。仍然引用上例,對/app/dir加上Sticky位:

[root@centos7 ~]#chmod o+t /app/dir
[root@centos7 ~]#ll /app/dir -d
drwxrwsr-t. 2 ffu ffu 4096 Jun  4 17:20 /app/dir

user1和user2均為屬組ffu的成員,分別創建文件f2\f3

[ffu@centos7 dir]$ll
total 0
-rw-rw-r--. 1 ffu   ffu 0 Jun  4 17:53 f1
-rw-rw-r--. 1 user1 ffu 0 Jun  4 17:53 f2
-rw-rw-r--. 1 user2 ffu 0 Jun  4 17:53 f3

user1不能刪除f1,f3

[user1@centos7 dir]$rm -v f1
rm: cannot remove ‘f1’: Operation not permitted
[user1@centos7 dir]$rm -v f3
rm: cannot remove ‘f3’: Operation not permitted

user2不能刪除f1,f2

[user2@centos7 dir]$rm -v f1
rm: cannot remove ‘f1’: Operation not permitted
[user2@centos7 dir]$rm -v f2
rm: cannot remove ‘f2’: Operation not permitted

由于ffu是目錄屬主,可以刪除f2,f3

[ffu@centos7 dir]$rm -v f2
removed ‘f2’
[ffu@centos7 dir]$rm -v f3
removed ‘f3’

最后,上文提到SUID、SGID、Sticky權限位的八進制數分別為4,2,1;三個特殊位可以組成一組權限:suidsgidsticky 允許我們通過八進制數字與普通權限一起對目錄及文件進行修改

比如:
chmod 4755 /app/dir   修改SUID
chmod 5755 /app/dir   修改SUID、Sticky


原創文章,作者:ffu,如若轉載,請注明出處:http://www.www58058.com/77140

(0)
ffuffu
上一篇 2017-06-04 19:16
下一篇 2017-06-04 21:31

相關推薦

  • 系統基礎之文件查找工具find

    文件查找:        在運維人員操作系統時,要接觸大量的文件,為了避免忘記文件存放位置的尷尬,就需要我們有一種文件查找工具的幫忙,下面是兩個文件查找工具的詳解,locate以及find,分別分享給大家. 第一款工具: Locate locate – find files by name loc…

    Linux干貨 2016-08-16
  • MariaDB

    Mariadb 結構化數據–>關系型數據庫 范式:Entry(每一行來描述一個整體) 半結構化數據–>YAML,XML,JSON 非結構化數據–>日志文件 NoSQL 關系型數據庫:事務能力 ACID測試(原子性,一致性,隔離性,持久性) MariaDB or MySQL: 層次模型–>…

    Linux干貨 2016-11-15
  • N26-第十四周

    1、系統的INPUT和OUTPUT默認策略為DROP;[root@centos7 ~]# iptables -P INPUT DROP[root@centos7 ~]# iptables -P OUTPUT DROP1、限制本地主機的web服務器在周一不允許訪問;新請求的速率不能超過100個每秒;web服務器包含了admin字符串的頁面不允許訪問;web服務…

    Linux干貨 2017-06-20
  • Linux文件管理命令和bash基礎特性

    1、Linux上的文件管理命令都有哪些,其常用的使用方法及其相關示例演示  文件管理命令:mkdir,rmdir,cp,mv,rm,cat,tac,head,tail,more,less mkdir命令:   mkdir – make directories   mkdir [OPTION]… DIREC…

    2017-07-13
  • 三次握手和四次揮手

    今天來聊一下事實標準協議TCP/IP中傳輸層里TCP協議中,主機與服務器建立連接時的三次握手,和斷開連接時的四次揮手。 本博文分兩部分介紹,    一:狀態詳解    二:三次握手和四次揮手狀態介紹 這里總共涉及到十種狀態,其實總共有十一種狀態,接下來分別介紹一下它們; 一:狀態詳解 CLOSED:關閉—&…

    2017-09-01
  • 推薦-BtrFS學習總結

    一、原理介紹1.可擴展性2.數據一致性的相關特性3.多設備管理及快照二、命令介紹與實戰1.命令介紹2.命令實戰1)環境準備,添加3塊磁盤/dev/sd{b,c,d}如下:2)創建btrfs文件系統3)查看已創建的btrfs文件系統4)掛載btrfs文件系統,啟用壓縮功能5)在線調整文件系統大小6)往當前btrfs文件系統添加設備,以達到在線擴容擴容目的7)執…

    Linux干貨 2016-06-03
欧美性久久久久