淺談文件權限
Linux系統中的每個文件和目錄都有訪問許可權限,用他來確定誰能通過何種方式對文件和目錄進行訪問和操作。
文件權限:
文件或目錄的訪問權限分為只讀,只寫和可執行三種。
文件的權限主要針對三類對象進行定義:
owner: 屬主, u
group: 屬組, g
other: 其他, o
每個文件針對每類訪問者都定義了三種權限:
r: Readable 可讀
w: Writable 可寫
x: eXcutable 可執行
ls -l 查看文件屬性:
[xiaoshui@localhost data]$ ls -l total 16 -rw-rw-r-- 1 xiaoshui root 4 Aug 4 16:25 f1
可以看出文件屬性信息一共被空格分為7個字段,第一個字段就是文件的類型和權限
如上圖所示,第一個-為文件的類型,如果為-,則表示為普通文件,后面以三個為一組,前三個為文件所有者的權限,中間三個為文件所屬組用戶的權限,后面三個對應其他用戶的權限。
可讀可寫可執行對目錄和文件有什么作用
文件:
r: 可使用文件查看類工具獲取其內容
w: 可修改其內容
x: 可以把此文件提請內核啟動為一個進程
目錄:
r: 可以使用ls查看此目錄中文件列表
w: 可在此目錄中創建文件,也可刪除此目錄中的文件
x: 可以使用ls -l查看此目錄中文件列表,可以cd進入此
權限匹配:
(1)判斷進程的屬主,是否為被訪問的文件屬主;如果是,則應用屬主的權限;否則進入第2步;
(2)判斷進程的屬主,是否屬于被訪問的文件屬主;如果是,則應用屬組的權限;否則進入第3步;
(3)應用other的權限;
chmod:
chmod[OPTION]… OCTAL-MODE FILE…
-R: 遞歸修改權限
chmod[OPTION]… MODE[,MODE]… FILE…
MODE:
修改一類用戶的所有權限:
u= g= o= ug= a= u=,g=
修改一類用戶某位或某些位權限
u+ u-g+ g-o+ o-a+ a-+ –
chmod[OPTION]… –reference=RFILE FILE…
參考RFILE文件的權限,將FILE的修改為同RFILE;
r=讀取屬性 //值=4
w=寫入屬性 //值=2
x=執行屬性 //值=1
[root@localhost testdir]# chmod u+x,g+w file //為文件file屬主加上x,屬組加上w [root@localhost testdir]# chmod u=rwx,g=rw,o=r file //設置文件屬主權限rwx,組權限rw,其他人r [root@localhost testdir]# chmod 764 file //為文件設置屬主rwx,組權限rw,其他人r [root@localhost testdir]# chmod a+x file //為文件屬主屬組和其他人全都加上x
注意:X:只給目錄x權限,不給文件x權限,如果使用-R遞歸,目錄中的目錄則會附加x權限,目錄中的文件有x權限,則會生效,如果沒有,則保持不變。
[root@localhost test2]# ll total 0 -rwxrw-rw- 1 root root 0 Aug 5 08:34 f1 -rw-rw-rw- 1 root root 0 Aug 5 08:35 f2 -rw-rw-rw- 1 root root 0 Aug 5 08:35 f3 [root@localhost data]# chmod -R g=rwX test2 [root@localhost data]# cd test2 [root@localhost test2]# ll total 0 -rwxrwxrw- 1 root root 0 Aug 5 08:34 f1 -rw-rw-rw- 1 root root 0 Aug 5 08:35 f2 -rw-rw-rw- 1 root root 0 Aug 5 08:35 f3
權限練習:
當用戶xiaoming對/testdir 目錄無執行權限時,意味著無法做哪些操作?
drwxr-xrw- 16 root root 4096 Aug 4 09:18 testdir drwxrwxrwt. 27 root root 4096 Aug 4 19:20 tmp drwxr-xr-x. 13 root root 4096 Jul 25 20:07 usr drwxr-xr-x. 21 root root 4096 Aug 4 16:45 var [xiaoming@localhost /]$ [xiaoming@localhost /]$ cd testdir/ -bash: cd: testdir/: Permission denied [xiaoming@localhost /]$ ls testdir/ ls: cannot access testdir/dir1: Permission denied ls: cannot access testdir/dir2: Permission denied ls: cannot access testdir/dir3: Permission denied ls: cannot access testdir/dir4: Permission denied ls: cannot access testdir/dir5: Permission denied ls: cannot access testdir/user4: Permission denied ls: cannot access testdir/user5: Permission denied ls: cannot access testdir/user6: Permission denied ls: cannot access testdir/user7: Permission denied ls: cannot access testdir/user8: Permission denied ls: cannot access testdir/user9: Permission denied ls: cannot access testdir/user10: Permission denied ls: cannot access testdir/mytest: Permission denied ls: cannot access testdir/hello: Permission denied dir1 dir3 dir5 mytest user4 user6 user8 dir2 dir4 hello user10 user5 user7 user9
因為xiaoming不屬于root組,所以匹配other,對testdir目錄有rw權限,但是沒有x權限,可以看出不可cd至此目錄,可ls查看文件列表,但不可ls -l 查看文件屬性。
當用戶xiaoming對/testdir 目錄無讀權限時,意味著無法做哪些操作?
drwxr-x-wx 16 root root 4096 Aug 4 09:18 testdir drwxrwxrwt. 27 root root 4096 Aug 4 19:29 tmp drwxr-xr-x. 13 root root 4096 Jul 25 20:07 usr drwxr-xr-x. 21 root root 4096 Aug 4 16:45 var [xiaoming@localhost /]$ [xiaoming@localhost /]$ cd testdir/ [xiaoming@localhost testdir]$ ls ls: cannot open directory .: Permission denied [xiaoming@localhost testdir]$ ls -l ls: cannot open directory .: Permission denied [xiaoming@localhost testdir]$ touch file1 [xiaoming@localhost testdir]$ touch file2 [xiaoming@localhost testdir]$ rmdir file1
[root@localhost testdir]# ls dir1 dir3 dir5 hello user10 user5 user7 user9 dir2 dir4 file2 mytest user4 user6 user8
可以看出當xiaoming對testdir擁有wx,無r時,可cd至此目錄,不可查看目錄內的文件列表與文件屬性,可創建和刪除文件,可查看文件的內容。
當用戶xiaoming對/testdir 目錄無寫權限時,該目錄下的只讀文件file1是否可修改和刪除?
drwxr-xr-x 16 root root 4096 Aug 4 19:31 testdir drwxrwxrwt. 27 root root 4096 Aug 4 19:36 tmp drwxr-xr-x. 13 root root 4096 Jul 25 20:07 usr drwxr-xr-x. 21 root root 4096 Aug 4 16:45 var [xiaoming@localhost /]$ [xiaoming@localhost testdir]$ ll total 8 drwxrwxr-x+ 4 root root 22 Jul 28 14:27 dir1 drwxrwxr-x+ 4 root root 22 Jul 28 14:28 dir2 drwxrwxr-x+ 2 root root 6 Jul 28 14:29 dir3 drwxrwxr-x+ 2 root root 6 Jul 28 14:29 dir4 drwxrwxr-x+ 4 root root 28 Jul 28 14:29 dir5 -rw-r--r-- 1 root root 0 Aug 4 19:41 file [xiaoming@localhost testdir]$ rm file rm: remove write-protected regular empty file ‘file’? y rm: cannot remove ‘file’: Permission denied [root@localhost testdir]# echo hello > file [xiaoming@localhost testdir]$ cat file hello [xiaoming@localhost testdir]$ echo 1 > file -bash: file: Permission denied
可以看出,因為xiaoming對testdir無w權限,所以不能刪除file, 因為擁有可讀權限,所以可以查看文件內容,沒有w權限,所以不可修改文件內容。
Linux文件系統上的特殊權限
SUID:
默認情況下:用戶發起的進程,進程的屬主是其發起者;因此,其以發起者的身份在運行;
SUID的功用:用戶運行程序時,如果此程序擁有SUID權限,那么程序運行為進程時,進程的屬主不再是發起者,而是程序文件本身的屬主;
管理文件的SUID權限:
chmod u+|-s file…
展示位置:屬主的執行權限位
如果屬主原本有執行權限,顯示為小寫s
否則,顯示為大寫S;
[root@localhost data]# chmod u+s /usr/bin/nano [root@localhost data]# ll /usr/bin/nano -rwsr-xr-x. 1 root root 205904 Jun 10 2014 /usr/bin/nano [xiaoming@localhost testdir]$ nano /etc/shadow user1:$5$JHmiPG4G$yolBfbwVY1RGXI2fJUzXoBgZupX1H9.hSn9HUELjjW0:0:0:999$ user2:!!:17016:0:99999:7::: user3:!!:17016:0:99999:7::: 12:!!:17016:0:99999:7::: jane:$5$hG95UcdW$3yv0tgu6Z1vI3asDrzbCbI4CgjhAh.YfZ5Kuu7G8BJ0:17017:0:$ lilei:!!:17017:0:99999:7::: tomre:$5$aYH1hhNZ$lYgaK6Euqap7JmiLsAIENM4TkYBJOwlN.sfrLFyBZ17:17017:0$ tomch:!!:17017:0:99999:7::: jixingshui:$5$H7neghml$pW3z37zBxWVDDux7SKKLbgVB/jS5qBR.dr/Ca77uIq8:17$ xiaosming:!!:17017:0:99999:7::: xiaoming::17017:0:99999:7::: [ Wrote 67 lines ] ^G Get Help^O WriteOut^R Read Fil^Y Prev Pag^K Cut Text^C Cur Pos ^X Exit ^J Justify ^W Where Is^V Next Pag^U UnCut Te^T To Spell
將/usr/bin/nano的文件設為suid權限,此時普通用戶運行nano進程時,普通用戶會繼承root的權限,然后寫入/etc/shadow文件,將xiaoming用戶設為空密碼,然后就可登錄了。
經上實驗,suid是一個非常危險的權限,所以一定要慎重賦予
SGID:
功用:當用戶屬組有寫權限,且有SGID權限時,那么所有屬于此目錄的屬組,且以屬組身份在此目錄中新建文件或目錄時,新文件的屬組不是用戶的基本組,而是此目錄的屬組;
展示位置:屬組的執行權限位
如果屬組原有執行權限,顯示為小寫s;
否則,顯示為大寫S
[root@localhost data]# chmod g+s testdir/ [root@localhost data]# ll total 0 drwxrwsrwx 2 root root 24 Aug 4 20:44 testdir [xiaoshui@localhost data]$ cd testdir/ [xiaoshui@localhost testdir]$ touch f2 [xiaoshui@localhost testdir]$ ll total 0 -rw-rw-r-- 1 xiaoming root 0 Aug 4 20:43 f1 -rw-rw-r-- 1 xiaoshui root 0 Aug 4 20:44 f2 [xiaoshui@localhost testdir]$ id uid=1001(xiaoshui) gid=1001(xiaoshui) groups=1001(xiaoshui)
當testdir目錄上附加了sgid權限的時候,普通用戶在其下創建文件時候文件的屬組會自動變為此目錄的屬組
STICKY:
功用:對于屬組或全局可寫的目錄,組內的所有用戶或系統上的所有用戶對在此目錄中都能創建新文件或者刪除所有的已有文件;如果此目錄設置為Sticky權限,則每個用戶能創建新文件,且只能刪除自己的文件
管理文件的Sticky權限:
chmod o+|-t FILE
展示位置:其他用戶的執行權限位
如果其他用戶原有執行權限,顯示為小寫t;
否則,顯示為大寫T
系統上的/tmp和/var/tmp目錄默認均有sticky權限
facl:file access control lists
文件的額外賦權機機制:
在原來的ugo之外,另一層讓普通用戶能夠控制賦權給另外的用戶或組的賦權機制。
getfacl命令:
getfacl FILE…
user:USERNAME:mode
group:GROUPNAME:MODE
setfacl命令:
賦權給用戶:
setfacl -m u:USERNAME:MODE
賦權給組:
setfacl -m g:GROUPNAME:MODE
撤銷賦權:
setfacl -x u:USERNAME file…
setfacl -x u:GROUPNAME file…
[root@localhost testdir]# ll total 4 -rw-rw---- 1 xiaoshui root 14 Aug 4 20:58 f1 -rw-rw-r-- 1 xiaoshui root 0 Aug 4 20:44 f2 [xiaoshui@localhost testdir]$ cat f1 cat: f1: Permission denied [root@localhost testdir]# setfacl -m u:xiaoshui:r f1 [xiaoshui@localhost testdir]$ cat f1 kdhjfkfhhjhkj [xiaoxiao@localhost testdir]$ cat f1 cat: f1: Permission denied
本來文件f1對其他用戶無權限,所以xiaoshui用戶當然也查看不了,root使用setfacl給f1設置xiaoshui用戶可讀,所以xiaoshui便可以訪問了。使用其他普通用戶依然無法查看f1.
ACL文件上的group權限是mask 值(自定義用戶, 自定義組,擁有組的最大權限),而非傳統的組權限,mask只影響除所有者和other的之外的人和組的最大權限,需要與用戶的權限進行邏輯與運算后,才能變成有限的權限(Effective Permission)
原創文章,作者:我的滑板鞋,如若轉載,請注明出處:http://www.www58058.com/28962
文章的層次結構清晰明了,內容豐滿,有理論有實踐。