1、權限簡介
操作系統中每個文件都擁有特定的權限、所屬用戶和所屬組。權限是操作系統用來限制資源訪問的機制,在Linux中權限一般分為讀(readable)、寫(writable)和執行(excutable),分為三組。分別對應文件的屬主(owner),屬組(group)和其他用戶(other),通過這樣的機制來限制哪些用戶、哪些組可以對特定的文件進行什么樣的操作。
2、文件和目錄權限的區別
對文件和目錄而言,讀寫執行表示不同的意義
對文件
r | 可以使用cat查看文件的內容 |
w | 可以修改文件的內容 |
x | 可以將其運行為二進制文件 |
對目錄
r | 可以查看目錄下列表 |
w | 可以創建和刪除目錄下文件 |
x | 可以使用cd進入目錄 |
注:文件的x權限一般關閉,防止可執行的二進制病毒文件自動運行
目錄的x權限一般開啟,否則r和w權限將失去意義
3、權限的控制
<1>修改文件的屬主屬組
chown [OPTION]… FILE…
-R:遞歸修改文件的屬主屬組
chgrp [OPTION]… FILE…
[root@centos7 testdir]# touch f1 f2 [root@centos7 testdir]# ll total 0 -rw-r--r--. 1 root root 0 Aug 4 13:22 f1 -rw-r--r--. 1 root root 0 Aug 4 13:22 f2 [root@centos7 testdir]# chown zhao:zhao f1 [root@centos7 testdir]# chown :zhao f2 [root@centos7 testdir]# touch f3 [root@centos7 testdir]# chgrp zhao f3 [root@centos7 testdir]# ll total 0 -rw-r--r--. 1 zhao zhao 0 Aug 4 13:22 f1 -rw-r--r--. 1 root zhao 0 Aug 4 13:22 f2 -rw-r--r--. 1 root zhao 0 Aug 4 13:23 f3
<2>修改文件的權限屬性
●chmod [OPTION]… MODE[,MODE]… FILE…
-R:遞歸修改文件的權限
下面是三種修改權限的方法:
賦權表示法:操作一類用戶的所有權限位
授權表示法:操作一類用戶的一個權限位
數字表示法:用數字的形式表示權限
[root@centos7 testdir]# touch f1 [root@centos7 testdir]# ll f1 -rw-r--r--. 1 root root 0 Aug 4 14:13 f1 [root@centos7 testdir]# chmod o=rw f1 [root@centos7 testdir]# ll f1 -rw-r--rw-. 1 root root 0 Aug 4 14:13 f1 [root@centos7 testdir]# chmod o+x f1 [root@centos7 testdir]# ll f1 -rw-r--rwx. 1 root root 0 Aug 4 14:13 f1 [root@centos7 testdir]# chmod 644 f1 [root@centos7 testdir]# ll f1 -rw-r--r--. 1 root root 0 Aug 4 14:13 f1
chmod [OPTION]… –reference=RFILE FILE…
[root@centos7 testdir]# ll total 0 -rw-r--r--. 1 root root 0 Aug 4 14:13 f1 -rw-rw-r--. 1 root root 0 Aug 4 14:18 f2 [root@centos7 testdir]# chmod --reference=f1 f2 [root@centos7 testdir]# ll total 0 -rw-r--r--. 1 root root 0 Aug 4 14:13 f1 -rw-r--r--. 1 root root 0 Aug 4 14:18 f2 [root@centos7 testdir]#
●chgrp [GROUPNAME]…
修改文件的數組
●chown [OWNER][:[GROUP]] FILE…
修改文件的屬主和屬組
●chattr [FILE]
設置文件的特定屬性
-i:不能刪除,改名,修改文件
-a:只能增加
lsattr [FILE]
顯示特定屬性
[root@centos7 testdir]# chattr +i f1 [root@centos7 testdir]# rm -f f1 rm: cannot remove ‘f1’: Operation not permitted [root@centos7 testdir]# echo f1 > f1 -bash: f1: Permission denied [root@centos7 testdir]# mv f1 f2 mv: cannot move ‘f1’ to ‘f2’: Operation not permitted [root@centos7 testdir]# chattr -i f1 [root@centos7 testdir]# chattr +a f1 [root@centos7 testdir]# lsattr f1 -----a---------- f1 [root@centos7 testdir]# echo f1 > f1 -bash: f1: Operation not permitted [root@centos7 testdir]# echo f1 >> f1 [root@centos7 testdir]# cat f1 f1
3、三種特殊權限及其意義
在談特殊權限之前,我們先需要了解一下安全上下文
安全上下文:計算機上的二進制文件一旦運行為程序,必定是以某個用戶的身份在運行。如果此身份與某個被訪問的資源屬主一致,則應用資源屬主權限;與屬組一致,應用屬組權限;與其他用戶一致,應用其他用戶權限。換言之,用戶的訪問權限,取決于發起此進程的用戶。但是SUID、SGID這倆類特殊權限將與上述觀點不一致。用戶運行某程序時,如果程序擁有SUID權限,那么,此進程運行為程序時,用戶將應用程序的屬主權限去訪問文件。SGID同理。
三種權限的應用范圍:
SUID | 作用在二進制文件上 |
SGID | 作用在二進制文件上 |
SGID | 也可以作用在目錄上 |
SBIT | 作用在目錄上 |
下面詳細分析三種權限的應用
<1>SUID作用:應用擁有SUID權限的二進制的屬主權限
[root@centos7 testdir]# ll /etc/shadow -r--------. 1 root root 3933 Aug 4 18:34 /etc/shadow [zhao@centos7 testdir]$ ll /bin/nano -rwxr-xr-x. 1 root root 205904 Jun 10 2014 /bin/nano [root@centos7 testdir]# ll /bin/nano -rwsr-xr-x. 1 root root 205904 Jun 10 2014 /bin/nano [root@centos7 testdir]# su zhao [zhao@centos7 testdir]$ nano /etc/shadow qi:!!:17017:0:99999:7::: ---->將倆個感嘆號去掉,取消鎖定--->qi:!!:17017:0:99999:7::: [zhao@centos7 testdir]$ su qi [qi@centos7 testdir]$
注:危險操作,切勿模仿
<2>SGID作用在二進制文件上,作用:應用擁有SGID權限的二進制的屬主權限,同SUID,不舉例。
<3>SBIT作用在目錄上時:如果用戶對目錄有寫權限,對目錄下的文件有讀權限,雖然不能修改別人的文件,卻可以創建自己的文件和刪除別人的文件。此時SBIT可以避免刪除別人的文件
示例:在/testdir下,要求自己可以創建和查看自己的文件,能查看別人的文件,但不能刪除和修改別人的文件
[root@centos7 testdir]# su user1 [user1@centos7 /testdir]$ touch f1 [user1@centos7 /testdir]$ chmod g-w f1 [user1@centos7 /testdir]$ su Password: [root@centos7 testdir]# su user2 [user2@centos7 /testdir]$ touch f2 [user2@centos7 /testdir]$ chmod g-w f2 [user2@centos7 /testdir]$ ll f1 f2 -rw-r--r--. 1 user1 user1 0 Aug 4 20:08 f1 -rw-r--r--. 1 user2 user2 0 Aug 4 20:09 f2 [user2@centos7 /testdir]$ cat f1 [user2@centos7 /testdir]$ su Password: [root@centos7 testdir]# chmod o+t . [root@centos7 testdir]# ll -d . drwxrwxrwt. 2 root it 24 Aug 4 20:09 . [root@centos7 testdir]# su user1 [user1@centos7 /testdir]$ echo content > f2 f2: Permission denied. [user1@centos7 /testdir]$ rm -f f2 rm: cannot remove ‘f2’: Operation not permitted
*SGID作用在目錄上時,在此目錄下創建文件時,屬組將自動屬于目錄的屬組
示例:在/testdir下,創建一個組,要求組內的某些用戶自己可以創建查看修改自己的文件,也能查看修改組內其他人的文件,不屬于此組的用戶對文件無任何權限。
步驟:root添加用戶和組g1
用戶創建自己的文件并且把屬組改為g1
root修改目錄的屬組和對其他用戶的訪問權限并且將屬組置為SGID權限
[root@centos7 testdir]# useradd user1 [root@centos7 testdir]# useradd user2 [root@centos7 testdir]# groupadd g1 [root@centos7 testdir]# gpasswd -a user1 g1 Adding user user1 to group g1 [root@centos7 testdir]# gpasswd -a user2 g1 Adding user user2 to group g1 [root@centos7 testdir]# su user1 [user1@centos7 testdir]$ touch f1 [user1@centos7 testdir]$ chgrp g1 f1 [user1@centos7 testdir]$ su Password: [root@centos7 testdir]# su user2 [user2@centos7 testdir]$ touch f2 [user2@centos7 testdir]$ chgrp g1 f2 [user2@centos7 testdir]$ su Password: [root@centos7 testdir]# ll f1 f2 -rw-rw-r--. 1 user1 g1 0 Aug 4 20:44 f1 -rw-rw-r--. 1 user2 g1 0 Aug 4 20:45 f2 [root@centos7 testdir]# chgrp g1 . [root@centos7 testdir]# chmod o= . [root@centos7 testdir]# chmod g+s . [root@centos7 testdir]# ll -d . drwxrws---. 2 root g1 24 Aug 4 20:45 .
4、ACL應用
ACL權限控制主要目的是提供傳統的owner,group,other的read,wirte,execute權限之外的具體權限設置,可以針對單一用戶或組來設置特定的權限。
<1>查看acl權限
getfacl FILENAME
<2>設置acl權限
語法格式
setfacl [OPTION] [FILENAME|DIRECTORY]
常用選項
-m | 添加acl規則 |
-x | 刪除acl規則 |
-M | 從文件中讀取acl規則 |
-X | 從文件中刪除acl規則 |
-m d | 設置默認acl規則,執針對目錄有效 |
-k | 清除默認acl規則 |
-b | 清除所有的acl規則 |
-R | 遞歸設置acl規則 |
<3>備份和恢復acl
第一步:設置目錄及目錄下文件的acl
[root@centos7 dir]# setfacl -m u:user1:r f1 [root@centos7 dir]# setfacl -m g:user2:r-x . [root@centos7 dir]# getfacl -R . # file: . # owner: root # group: g1 # flags: -s- user::rwx group::r-x group:user2:r-x mask::r-x other::r-x # file: f1 # owner: root # group: g1 user::rw- user:user1:r-- group::r-- mask::r-- other::r--
第二步:備份這些acl至特定文件
[root@centos7 dir]# getfacl -R . > /tmp/acl.txt
第三步:清除原有的目錄及文件的acl
[root@centos7 dir]# setfacl -b -R . [root@centos7 dir]# getfacl -R . # file: . # owner: root # group: g1 # flags: -s- user::rwx group::r-x other::r-x # file: f1 # owner: root # group: g1 user::rw- group::r-- other::r--
第四步:還原acl
[root@centos7 dir]# setfacl -R --set-file=/tmp/acl.txt . [root@centos7 dir]# getfacl -R . # file: . # owner: root # group: g1 # flags: -s- user::rw- user:user1:r-- group::r-- group:user2:r-x #effective:r-- mask::r-- other::r-- # file: f1 # owner: root # group: g1 user::rw- user:user1:r-- group::r-- group:user2:r-x #effective:r-- mask::r-- other::r--
原創文章,作者:mfwing,如若轉載,請注明出處:http://www.www58058.com/29231
文章的層次結構清晰明了,如果能多一些自己對操做部分的理解與總結就更好了。