1、文件權限:
文件的權限主要針對三類對象進行定義:
owner:屬主,u表示
group :屬組,g表示
Other:其他, o表示
每個文件或目錄針對每類訪問者都定義了三種權限:
文件:
r:可以使用文件查看類工具查看文件內容;
w:可以修改其內容;
x:執行該文件(二進制文件);
目錄:
r:可以查看目錄內容(顯示子目錄、文件列表);
W:修改文件內容(在目錄中新建、移動、刪除文件或子目錄);
X:可以使用ls -l 查看此目錄中的文件列表,可以cd進入此目錄;
注:x只給目錄x權限,不給文件x權限;
文件權限對應表:
2、chmod命令:修改文件權限
chmod [OPTION]… MODE[,MODE]… FILE…
-R:遞歸修改權限;
修改一類用戶的所有權限:
u= g= o= ug= a= u= g=
修改一類用戶某位或某些位權限:
u+ u- g+ g- o+ o- a+ a- + –
例:
chmod u+wx,g-r,o=rx /testdir/file2
文件權限對應的八進制數字:
r=4 w=2 0=1
例:
chmod 755 /bin/cat
chmod a=rwX /testdir
rwX:
對于文件不會增加x執行權限(前提是文件本身沒有x執行權限);
對于目錄會增加x執行權限;
參考其他文件來設置權限:
chmod [OPTION]… –reference=RFILE FILE…
-R:遞歸創建屬主屬組
例:
chmod –reference /etc/passwd /testdir
3、chown命令:用來設置文件的屬主和屬組:
chown [OPTION]… [OWNER][:[GROUP]] FILE…
例:
chown root:sysadmins /testdir/file1
chown -R root /testdir/
4、chgrp命令:修改文件的屬組
chgrp [OPTION]… GROUP FILE…
-R:遞歸修改文件的屬組;
例:
chgrp user1 /testdir/file
chgrp -R user1 /testdir
chgrp [OPTION]… –reference=RFILE FILE…
例:
chgrp –reference /etc/passwd /testdir
5、umask值:
umask值可以用來保留創建文件默認權限;
新建文件權限:666–umask,如果所得結果某位存在執行(奇數)權限,則將其權限+1
例:
666-127=54-1則+1=640
新建目錄權限:777-umask;
例:
777-127=650
umask :查看系統默認umask碼;
umask :為系統臨時設置umask碼;
umask -s :模仿的rwx的形式顯示出umask碼;
umask -p :輸出的umask碼可以被調用;
umask -p > /etc/bashrc
umask -p > ~/.bashrc
6、Linux文件系統上的特殊權限:
任何一個可執行程序文件能不能啟動為進程:取決于發起者對程序文件是否擁有執行權限;
suid 和 sgid所對應的八進制數字:
suid=4 sgid=2
suid :只對二進制程序文件有效;設置在目錄上無效;
對二進制文件設定suid:
chmod u+s /etc/cat
或
chmod 4755 /etc/cat
取消對二進制文件的suid:
chmod u-s /etc/cat
sgid:通常用于創建一個協作目錄,一旦某目錄被設定了sgid,則對此目錄有寫權限的用戶在此目錄中創建的文件所屬的組為此目錄的屬組;
對目錄設定sgid:
chmod g+s dir
對目錄取消sgid:
chmod g-s dir
例:
[root@centos6 testdir]# chmod g+s /testdir/
[root@centos6 testdir]# chown :it /testdir/
[root@centos6 testdir]# ls -ld /testdir/
drwxrwsrwx+ 2 root it 4096 Aug 4 14:37 /testdir/
[root@centos6 testdir]# touch aa bb cc
[root@centos6 testdir]# ll
total 12
-rw-rw-r–+ 1 root it 0 Aug 4 14:43 aa
-rw-rw-r–+ 1 root it 0 Aug 4 14:43 bb
-rw-rw-r–+ 1 root it 0 Aug 4 14:43 cc
[root@centos6 testdir]#
suid權限映射位:
s:屬主擁有x權限;
S:屬主沒有x權限;
guid權限映射位:
s:group擁有x權限;
S:group沒有x權限;
sticky權限位映射
t:other擁有x權限;
T:other沒有x權限;
7、sticky 粘滯位:
Sticky粘滯位:只對目錄有效;設置在文件上無意義;
在目錄中設置sticky位以后,只有文件的所有者或root可以刪除文件;其他用戶只能查看文件內容;
注:具有寫權限的目錄通常用戶可以刪除該目錄中的任何文件,無論該文件的權限或擁有者;
Sticky位對應的八進制數字:
添加sticky位=1 取消sticky位=0
為目錄設置sticky位:
chmod o+t dir 或 chmod 1777 dir
例:
[root@centos6 testdir]# chmod o+t /testdir/ 或 chmod 1777 /testdir
[root@centos6 testdir]# ll -d /testdir/
drwxrwsrwt+ 2 root it 4096 Aug 4 14:51 /testdir/
為目錄取消sticky位權限:
chmod o-t dir 或 chmod 0777 dir
例:
[root@centos6 testdir]# chmod o-t /testdir/
[root@centos6 testdir]# ls -ld /testdir/
drwxrwsrwx+ 2 root it 4096 Aug 4 14:51 /testdir/
[root@centos6 testdir]#
8、設定文件特定屬性:
chattr命令:用于改變文件的屬性
chattr [options] file
+i:鎖定文件或目錄,不能刪除更名修改文件;
-i :取消對文件的鎖定;可以刪除更名修改文件;
+A:鎖定文件的訪問時間改變;
-A:取消對文件訪問時間的鎖定;
+a:只能向文件中添加數據,而不能刪除,多用于服務器日志文件,/var/log/messages;
例:
chattr +i (-i) file 示例:
[root@localhost ~]# chattr +i user
[root@localhost ~]# lsattr user
—-i———– user
[root@localhost ~]#
[root@localhost ~]# rm -rf user
rm: 無法刪除"user": 不允許的操作
[root@localhost ~]# mv user aa
mv: 無法將"user" 移動至"aa": 不允許的操作
[root@localhost ~]# echo aaaa >> user
bash: user: 權限不夠
[root@localhost ~]#chattr -i user
chattr +a(-a) file 示例:
[root@localhost ~]# chattr +a user
[root@localhost ~]# lsattr user
—–a———- user
[root@localhost ~]# rm -rf user
rm: 無法刪除"user": 不允許的操作
[root@localhost ~]# mv user bb
mv: 無法將"user" 移動至"bb": 不允許的操作
[root@localhost ~]# echo nihao >> user
[root@localhost ~]# cat user
nihao
[root@localhost ~]# chattr -a user
lsattr命令:查看文件特定的屬性
lsattr file
例:
[root@localhost ~]# lsattr user
—–a———- user
[root@localhost ~]#
9、ACL訪問控制列表:
ACL:實現靈活的權限管理,除了文件的所有者,所屬組和其他人,可以對更多的用戶設置權限;
centos7,默認創建的xfs和ext4文件系統有ACL功能;
Centos7,之前的版本,默認手工創建的ext4文件系統無ACL功能,需手動創建:
tune2fs -o acl /dev/sdb1
mount -o acl /dev/sdb1 /mnt
ACL生效順序:所有者、自定義用戶、自定義組,其他人
設置setfacl訪問控制:
setfacl -m u:zheng:rw user (設置單個用戶setfacl)
setfacl -m g:it:rw user (設置組setfacl)
-m:創建setfacl;
-x:刪除setfacl ;
-b:清空setfacl控制;
-M:批量為文件設置setfacl訪問控制權限;
-X:批量刪除文件的setfacl訪問控制權限;
-Rm:對目錄遞歸設置setfacl訪問控制;
-k:刪除默認的acl權限;
查看setfacl訪問控制屬性:
getfacl file | dir
給單個用戶設置setfacl訪問控制:
[root@localhost testdir]# setfacl -m u:zheng:rw user
[root@localhost testdir]# getfacl user
# file: user
# owner: root
# group: root
user::rw-
user:zheng:rw-
group::r–
mask::rw-
other::r–
[root@localhost testdir]#
給組設置setfacl訪問控制:
[root@localhost testdir]# setfacl -m g:it:rwx user
[root@localhost testdir]# getfacl user
# file: user
# owner: root
# group: it
user::rw-
user:zheng:rw-
group::r–
group:it:rwx
mask::rwx
other::r–
[root@localhost testdir]#
刪除單個用戶的setfacl訪問控制權限:
[root@localhost testdir]# setfacl -x u:zheng user
[root@localhost testdir]# getfacl user
# file: user
# owner: root
# group: it
user::rw-
group::r–
group:it:rwx
mask::rwx
other::r–
[root@localhost testdir]#
刪除組的setfacl訪問控制權限:
root@localhost testdir]#setfacl -x g:it user
徹底清空文件的setfacl訪問控制:
root@localhost testdir]# setfacl -b user
[root@localhost testdir]# getfacl user
# file: user
# owner: root
# group: it
user::rw-
group::r–
other::r–
[root@localhost testdir]# ll
總用量 4
-rw-r–r–. 1 root it 3 8月 4 19:37 user
[root@localhost testdir]#
批量設置文件的setfacl訪問控制:
vim編輯acl.txt文件
[root@localhost testdir]# vim acl.txt
u:zheng:rwx
g:it:rw
保存此文件;
[root@localhost testdir]# setfacl -M acl.txt user
[root@localhost testdir]# getfacl user
# file: user
# owner: root
# group: it
user::rw-
user:zheng:rwx
group::r–
group:it:rw-
mask::rwx
other::r–
[root@localhost testdir]#
批量刪除文件的setfacl訪問控制權限:
vim編輯acl2.txt文件
[root@localhost testdir]# vim acl2.txt
[root@localhost testdir]# setfacl -X acl2.txt user
[root@localhost testdir]# getfacl user
# file: user
# owner: root
# group: it
user::rw-
group::r–
group:it:rw-
mask::rw-
other::r–
[root@localhost testdir]#
setfacl訪問控制列表中,用戶或組的設置必須存在于mask權限設定范圍內才會生效;
mask只影響除所有者和other以外的人和組的最大權限;只能等于或小于mask的權限, 不能超過mask的權限;
設定mask權限:
[root@localhost testdir]# setfacl -m mask::rwx user
[root@localhost testdir]# getfacl user
# file: user
# owner: root
# group: it
user::rw-
group::r–
group:it:rw-
mask::rwx
other::r–
[root@localhost testdir]#
設置目錄默認setfacl訪問控制權限:
[root@localhost testdir]# setfacl -m d:u:zheng:rwx dir
d:(default)表示對目錄設置遞歸的setfacl訪問控制權限;在該目錄下新創建的文件也默認繼承在目錄中設置的setfacl訪問控制權限;
刪除目錄默認的setfacl訪問控制權限;
[root@localhost testdir]# setfacl -k dir
復制file1文件的setfacl權限給分file2:
getfacl file1 | setfacl –set-file=- file2
[root@bogon testdir]# getfacl user | setfacl –set-file=- user1
[root@bogon testdir]# getfacl user1
# file: user1
# owner: root
# group: root
user::rw-
user:zheng:rw-
group::r–
mask::rw-
other::r–
[root@bogon testdir]#
ACL備份和恢復:
主要的文件操作命令cp和mv都支持ACL,只是cp命令需要加上 -p選項;但是tar等常見的備份壓縮工具是不會保留目錄和文件的ACL信息。
備份/testdir目錄下的ACL訪問控制屬性到/root/acl.bak
[root@bogon testdir]# getfacl -R * >> /root/acl.bak
清空/testdir目錄下的所有ACL訪問控制列表:
[root@bogon testdir]# setfacl -R -b *
使用ll命令查看/testdir/目錄下的子目錄和文件,在權限位最后已沒有了“+”號;
[root@bogon testdir]# ll
總用量 0
drwxr-xr-x. 2 root root 26 8月 4 21:16 dir
-rw-r–r–. 1 root root 0 8月 4 22:57 user
-rw-r–r–. 1 root root 0 8月 4 22:58 user1
[root@bogon testdir]#
恢復/testdir/目錄下ACL訪問控制:
[root@bogon testdir]# setfacl -R –set-file=/root/acl.bak ./
使用ll命令查看/testdir/目錄下的子目錄和文件,在權限位最后已經有了“+”號;
[root@bogon testdir]# ll
總用量 12
drw-rw-r–+ 2 root root 26 8月 4 21:16 dir
-rw-rw-r–+ 1 root root 0 8月 4 22:57 user
-rw-rw-r–+ 1 root root 0 8月 4 22:58 user1
[root@bogon testdir]#
原創文章,作者:zhengyibo,如若轉載,請注明出處:http://www.www58058.com/29340
寫的很簡潔清晰,總體來說還可以。