檔案基本權限
rwx r-x r-x user1 user1 FILENAME
類型 擁有者的權限 所屬組的權限 其他人的權限 擁有者 屬組 對象
對于檔案:r讀 w寫 x執行
對于目錄:r讀(看到目錄裡面有什麼) ls
w建檔案、删除、移動 touch mkdir rm mv cp
x進入 cd
第一個表示檔案的類型,如d目錄,-(f)普通檔案,L連結,b塊裝置,c字元裝置,p管道檔案,s套接字檔案
修改權限的相關指令:
chmod
chmod [ugoa][+-=][rwx] 檔案或目錄
使用數字表示權限
r=4,w=2,x=1,使用者權限=4+2+1=rwx
[root@localhost ~]# chmod 622 a.txt
[root@localhost ~]# ll a.txt
-rw--w--w- 1 root root 0 Feb 15 07:52 a.txt
[root@localhost ~]#
chown
作用:修改檔案擁有者和所屬組
文法:chown USER:GROUP 對象
chown USER 對象
chown :GROUP 對象
[root@localhost ~]# chown rm:bin a.txt
-rw--w--w- 1 rm bin 0 Feb 15 07:52 a.txt
[root@localhost ~]# chown daemon a.txt
-rw--w--w- 1 daemon bin 0 Feb 15 07:52 a.txt
[root@localhost ~]# chown :sys a.txt
-rw--w--w- 1 daemon sys 0 Feb 15 07:52 a.txt
-R遞歸(目錄下的所有内容全部更改,否則隻修改目錄)
[root@localhost ~]# chown rm test/ -R
[root@localhost ~]# ll -d test/
dr-xr-xr-x 2 rm root 30 Feb 15 08:21 test/
[root@localhost ~]# ll test/
total 0
-rw-r--r-- 1 rm root 0 Feb 15 08:21 a.txt
-rw-r--r-- 1 rm root 0 Feb 15 08:21 b.txt
一個檔案隻有讀的權限,擁有者是否可以寫這個檔案? 檔案所有者一定可以寫檔案
[root@localhost ~]# ll /home/rm/rm.txt
-r--r--r-- 1 rm rm 30 Feb 15 08:01 /home/rm/rm.txt
[root@localhost ~]# su - rm
[rm@localhost ~]$ vim rm.txt
結果:可以正常寫入,注意:儲存時用wq!
特殊權限:
SUID SGID Stickybit
s對應的數值為:u 4,g 2,o 1
SUID:
限定:隻能設定在二進制可執行程式上面。對目錄文本設定無效。
功能:程式運作時的權限從執行者變更成程式所有者。
[root@localhost ~]# which passwd
/usr/bin/passwd
[root@localhost ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jan 29 2014 /usr/bin/passwd
[root@localhost ~]# which less
/usr/bin/less
[root@localhost ~]# less /etc/shadow ##root使用者可以通過less通路passwd檔案
[root@localhost ~]#
[root@localhost ~]# su - u01
上一次登入:五 12月 8 03:03:06 CST 2017pts/1 上
[u01@localhost ~]$ less /etc/shadow
/etc/shadow: 權限不夠
[root@localhost ~]# chmod u+s /usr/bin/less
等同于
[root@localhost ~]# chmod 4755 /usr/bin/less
[u01@localhost ~]$ less /etc/shadow ##可以通過less通路shadow和passwd檔案
[u01@localhost ~]$ less /etc/passwd
SGID
限定:既可以給二進制可執行程式設定,也可以給目錄設定。
功能:在設定了SGID權限的目錄下建立檔案時,新建立的檔案的所屬組會繼承上級目錄的所屬組
[root@localhost ~]# mkdir 123
[root@localhost ~]# id u01
uid=2022(u01) gid=2022(u01) 組=2022(u01)
[root@localhost ~]# chown :u01 123/
[root@localhost ~]# ll -d 123
drwxr-xr-x. 2 root u01 6 12月 8 04:38 123
[root@localhost ~]# touch 123/123.txt
[root@localhost ~]# ll 123
總用量 0
-rw-r--r--. 1 root root 0 12月 8 04:39 123.txt
加上SGID
[root@localhost ~]# chmod g+s 123
[root@localhost ~]# touch 123/456.txt
-rw-r--r--. 1 root u01 0 12月 8 04:41 456.txt
Stickybit
限定:隻作用于目錄
功能:目錄下建立的檔案隻有root、檔案建立者、目錄所有者才能删除
[root@localhost ~]# ll -d /tmp/
drwxrwxrwt. 14 root root 4096 Feb 15 09:08 /tmp/
[root@localhost ~]# chmod 1777 /share/
[root@localhost ~]# ll -d /share/
drwxrwxrwt 2 root root 6 Feb 15 09:10 /share/
[u01@localhost ~]$ cd /share/
[u01@localhost share]$ ls
[u01@localhost share]$ touch 123.txt
[root@localhost ~]# useradd u02
[root@localhost ~]# su - u02
[u02@localhost ~]$ cd /share/
[u02@localhost share]$ rm -rf 123.txt
rm: cannot remove ‘123.txt’: Operation not permitted
擴充ACL
[root@localhost ~]# ll 123.txt
-rw-r--r--. 1 root root 64 12月 8 01:16 123.txt
[root@localhost ~]# getfacl 123.txt ##檢視acl權限
user::rw-
group::r--
other::r--
[root@localhost ~]# setfacl -m u:u01:rw 123.txt ##設定acl給u01使用者讀寫的權限
[root@localhost ~]# getfacl 123.txt
user:u01:rw-
mask::rw-
對目錄進行設定
[root@localhost ~]# setfacl -R -m u:u02:rw 123
(-R一定要在-m前面,表示目錄下所有檔案)
[root@localhost ~]# getfacl !$
getfacl 123
user::rwx
user:u02:rw-
group::r-x
mask::rwx
other::r-x
删除acl
[root@localhost ~]# setfacl -x u:u01 123.txt #删除單個使用者的acl權限
[root@localhost ~]# setfacl -b 123.txt #删除所有使用者的acl權限
實戰-建立一個讓root都無法删除的黑客檔案
Linux檔案系統擴充屬性:chattr(添權重限) lsattr(取消權限)
+a 隻能追加内容
+i 不能被修改
[root@localhost ~]# echo "123456" >123.txt
[root@localhost ~]# cat 123.txt
123456
[root@localhost ~]# chattr +a 123.txt
[root@localhost ~]# echo "789" >123.txt
-bash: 123.txt: 不允許的操作
[root@localhost ~]# echo "789" >>123.txt
789
[root@localhost ~]# rm -rf 123.txt
rm: 無法删除"123.txt": 不允許的操作
[root@localhost ~]# chattr +i 123.txt
[root@localhost ~]# echo "123456" >>123.txt
-bash: 123.txt: 權限不夠
[root@localhost ~]# lsattr 123.txt ##檢視
----ia---------- 123.txt
[root@localhost ~]# chattr -i 123.txt ##取消i權限
[root@localhost ~]# chattr -a 123.txt ##取消a權限
[root@localhost ~]# lsattr 123.txt
---------------- 123.txt
實際應用:
[root@localhost ~]# chattr +a /etc/passwd
[root@localhost ~]# chattr +a /etc/shadow
##還能針對日志檔案進行設定
本文轉自信自己belive51CTO部落格,原文連結: http://blog.51cto.com/11638205/2048699,如需轉載請自行聯系原作者