標籤:特殊許可權 linux 許可權 centos
一、Linux下目錄與檔案的許可權
chown 變更檔或目錄的所屬主,所屬組
/etc/passwd 設定檔存放使用者資訊
useradd 添加使用者
chown 所屬主:所屬組 檔案或目錄
chown -R 更改目錄及目錄下面的檔案所有許可權
chown -v 可視化顯示許可權更改
[[email protected] tmp]# chown user1:root aa[[email protected] tmp]# ls -ldrwxr-xr--. 2 user1 root 4096 Mar 18 16:41 aa-rw-------. 1 root root 0 Nov 13 09:25 yum.log[[email protected] tmp]# ls -l aa/-rw-r--r--. 1 root root 0 Mar 18 16:41 bb[[email protected] tmp]# chown -R user1:user1 aa[[email protected] tmp]# ls -ld aa/drwxr-xr--. 2 user1 user1 4096 Mar 18 16:41 aa/[[email protected] tmp]# ls -l aa/-rw-r--r--. 1 user1 user1 0 Mar 18 16:41 bb
chmod 更改rwx許可權 r=4 w=2 x=1
chmod -R 更改目錄及目錄下面的檔案許可權
chmod -v 可視化顯示許可權更改
[[email protected] tmp]# mkdir aa[[email protected] tmp]# ls -ld aa/drwxr-xr-x. 2 root root 4096 Mar 18 16:46 aa/[[email protected] tmp]# touch aa/bb[[email protected] tmp]# ls -l aa/-rw-r--r--. 1 root root 0 Mar 18 16:46 bb[[email protected] tmp]# chmod -Rv 666 aamode of `aa‘ changed to 0666 (rw-rw-rw-)mode of `aa/bb‘ changed to 0666 (rw-rw-rw-)
umask 指定目錄或檔案的預設許可權,root使用者是022,普通使用者是002;
更改umask值 umask 001
[[email protected] ~]# umask0022[[email protected] ~]# umask 001[[email protected] ~]# umask0001[[email protected] ~]# mkdir aa[[email protected] ~]# touch bb[[email protected] ~]# ls -ldrwxrwxrw-. 2 root root 4096 Mar 18 16:26 aa-rw-rw-rw-. 1 root root 0 Mar 18 16:26 bb
如果更改umask值為001,目錄許可權為776 rwxrwxrw- 檔案許可權為666 rw-rw-rw- 檔案根本不需要x許可權,所以許可權還是不變。
root下預設目錄為755 rwxr-xr-x,檔案為644 rw-r--r--
目錄最大許可權為777 rwxrwxrwx 777
----w--w- 022
rwxr-xr-x 755
檔案最大許可權為666 rw-rw-rw- 666
----w--w- 022
rw-r--r-- 644
一個目錄必須具有rx許可權,可以進入目錄 cd,可以列出目錄內容 ls
目錄若沒有x執行許可權,我們不能cd到這個目錄下,但是可以ls這個目錄。
[[email protected] tmp]# mkdir aa[[email protected] tmp]# ls -ld aa/drwxr-xr-x. 2 root root 4096 Mar 18 16:33 aa/[[email protected] tmp]# chmod 754 aa[[email protected] tmp]# ls -ld aa/drwxr-xr--. 2 root root 4096 Mar 18 16:33 aa/[[email protected] tmp]# su - user1[[email protected] ~]$ cd /tmp/[[email protected] tmp]$ cd aa-bash: cd: aa: Permission denied[[email protected] tmp]$ ls -ld aa/drwxr-xr--. 2 root root 4096 Mar 18 16:33 aa/
普通使用者預設目錄為775 rwxrwxr-x,檔案為664 rw-rw-r--
二、修改檔案、目錄的特殊許可權
chattr
+i 增加後,檔案不能被刪除、重新命名、設定串連、寫入、新增資料
[[email protected] tmp]# chattr +i aa[ [email protected] tmp]# touch aa/cc touch: cannot touch `aa/cc‘: Permission denied [ [email protected] tmp]# chattr -i aa [ [email protected] tmp]# touch aa/cc [ [email protected] tmp]# chattr +i aa [ [email protected] tmp]# rm -rf aa/cc rm: cannot remove `aa/cc‘: Permission denied [ [email protected] tmp]# mv aa bb mv: cannot move `aa‘ to `bb‘: Operation not permitted
+a 增加該屬性後,只能追加不能刪除,非root使用者不能修改屬性
檔案只能追加,目錄下可以建立子目錄與檔案,可以寫檔案,不能刪除子目錄和檔案,也不能改名。
[ [email protected] tmp]# touch 11 [ [email protected] tmp]# ls -l -rw-r--r--. 1 root root 0 Mar 18 17:10 11 [ [email protected] tmp]# echo ‘abc‘>11 [ [email protected] tmp]# cat 11 abc [ [email protected] tmp]# chattr +a 11 [ [email protected] tmp]# echo ‘def‘>11 bash: 11: Operation not permitted [ [email protected] tmp]# echo ‘def‘>>11 [ [email protected] tmp]# cat 11 abc def[ [email protected] tmp]# chmod 666 11chmod: changing permissions of `11‘: Operation not permitted [ [email protected] tmp]# chown user1:root 11 chown: changing ownership of `11‘: Operation not permitted[ [email protected] tmp]# rm -rf 11 rm: cannot remove `11‘: Operation not permitted
lsattr 查看檔案或目錄的特殊許可權
[[email protected] tmp]# lsattr-------------e- ./yum.log----i--------e- ./aa
lsattr -d 查看目錄的特殊屬性
[[email protected] tmp]# lsattr -d aa -----a-------e- aa
lsattr -R 列出目前的目錄及子目錄的特殊屬性
[ [email protected] tmp]# lsattr -R -------------e- ./yum.log -----a-------e- ./aa ./aa: -------------e- ./aa/bb -------------e- ./aa/cc
set_uid 使普通使用者臨時擁有執行這條命令所屬主的許可權
set_uid=4 set_gid=2 stick_bit=1
[ [email protected] ~]# umask 0022[ [email protected] ~]# which passwd /usr/bin/passwd[ [email protected] ~]# ls -l /usr/bin/passwd - rwsr-xr-x. 1 root root 25980 2月 22 2012 /usr/bin/passwd
rws=rwx+s 小s說明檔案具有rwx的許可權 大S說明檔案沒有x許可權
passwd所屬主為root使用者,有了rws許可權後普通使用者也具有root的許可權,所以普通使用者可以自己更改密碼。
執行者必須對檔案擁有x許可權(否則即使設定了SUID,由於你對檔案根本無法執行SUID此時也無效,此時許可權位顯示S);
舉例說明:
[ [email protected] tmp]# chmod u-sx /usr/bin/passwd [ [email protected] tmp]# ls -l /usr/bin/passwd -rw-r-xr-x. 1 root root 25980 Feb 22 2012 /usr/bin/passwd[[email protected] ~]# chmod u+s /usr/bin/passwd [[email protected] ~]# ls -l /usr/bin/passwd-rwSr-xr-x. 1 root root 25980 2月 22 2012 /usr/bin/passwd
大寫S 說明檔案沒有x許可權,不能執行,沒有意義。
chmod u-s /usr/bin/passwd 可執行檔檔案、二進位的才可以設定s許可權;
[ [email protected] tmp]# chmod u-s /usr/bin/passwd [ [email protected] tmp]# ls -l /usr/bin/passwd -rwxr-xr-x. 1 root root 25980 Feb 22 2012 /usr/bin/passwd
u-s 後檔案具有rwx許可權
[ [email protected] ~]# ls -l /usr/bin/passwd -rwsr-xr-x. 1 root root 25980 2月 22 2012 /usr/bin/passwd[ [email protected] tmp]# chmod 755 /usr/bin/passwd [ [email protected] tmp]# ls -l /usr/bin/passwd -rwxr-xr-x. 1 root root 25980 Feb 22 2012 /usr/bin/passwd [ [email protected] tmp]# chmod 4755 /usr/bin/passwd [ [email protected] tmp]# ls -l /usr/bin/passwd -rwsr-xr-x. 1 root root 25980 Feb 22 2012 /usr/bin/passwd
本身passwd是755 ,最前面加了suid 這個值為4,所以這個passwd的許可權是4755
[ [email protected] ~]# chmod 6755 /usr/bin/passwd [ [email protected] ~]# ls -l /usr/bin/passwd -rwsr-sr-x. 1 root root 25980 2月 22 2012 /usr/bin/passwd
6是代表suid4+sgid2=6 然後本身的許可權是755,所以是6755
set_gid 使普通使用者臨時具有檔案所屬組的許可權,可以作用於檔案和目錄
chmod g+s 目錄名
更改完成後在目錄下面建立立的目錄都是,父目錄所屬組的屬性
建立立的目錄擁有父目錄所屬組的許可權,繼承父目錄的。
[ [email protected] tmp]# mkdir aa [ [email protected] tmp]# ls -ld aa/ drwxr-xr-x. 2 root root 4096 Mar 19 10:04 aa/[[email protected] tmp]# chown user1:user1 aa[[email protected] tmp]# ls -ld aa/drwxr-xr-x. 3 user1 user1 4096 Mar 19 10:05 aa/[[email protected] tmp]# chmod g+s aa/[ [email protected] tmp]# ls -ld aa/ drwxr-sr-x. 4 user1 user1 4096 Mar 19 10:07 aa/[[email protected] tmp]# mkdir aa/cc[ [email protected] tmp]# ls -l aa drwxr-sr-x. 2 root user1 4096 Mar 19 10:06 cc [ [email protected] tmp]# touch aa/1.txt [ [email protected] tmp]# ls -l aa/1.txt -rw-r--r--. 1 root user1 0 Mar 19 10:07 aa/1.txt[ [email protected] tmp]# chmod g-s aa/ [ [email protected] tmp]# ls -ld aa drwxr-xr-x. 4 user1 user1 4096 Mar 19 10:07 aa [ [email protected] tmp]# mkdir aa/dd [ [email protected] tmp]# ls -l aa/ drwxr-sr-x. 2 root user1 4096 Mar 19 10:06 cc drwxr-xr-x. 2 root root 4096 Mar 19 10:10 dd
去掉guid的s許可權後,在目錄建立的目錄不會繼承父目錄的屬性;
sticky_bit 防刪除位元,rwt 屬性,只有建立者可以刪除自己的,其他使用者不可以刪除。root具有至高無上的許可權都可以刪除。
chmod o+t 目錄名
[ [email protected] ~]# ls -ld /tmp/ drwxrwxrwt. 3 root root 4096 3月 19 19:30 /tmp/[ [email protected] tmp]# mkdir 11[ [email protected] tmp]# chmod 777 11 [ [email protected] tmp]# ls -ld 11/ drwxrwxrwx 2 root root 4096 3月 19 19:40 11/ [ [email protected] tmp]# chmod o+t 11/ [ [email protected] tmp]# ls -ld 11/ drwxrwx rwt 2 root root 4096 3月 19 19:40 11/
目錄必須有x許可權才可以進去,小寫t說明目錄有x許可權,T說明沒有x許可權,檔案是T顯示的。
[ [email protected] 11]# su - test1 [ [email protected] ~]$ cd /tmp/11/ [ [email protected] 11]$ mkdir 12[ [email protected] 11]$ touch 123 drwxrwxr-x 2 test1 test1 4096 3月 19 20:14 12 -rw-rw-r-- 1 test1 test1 0 3月 19 20:15 123 drwxr-xr-x 2 root root 4096 3月 19 20:14 root[ [email protected] 11]# su - test2 [ [email protected] ~]$ cd /tmp/11/ [ [email protected] 11]$ ls -l drwxrwxr-x 2 test1 test1 4096 3月 19 20:14 12 -rw-rw-r-- 1 test1 test1 0 3月 19 20:15 123 drwxr-xr-x 2 root root 4096 3月 19 20:14 root [ [email protected] 11]$ rm -rf 12 rm: 無法刪除"12": 不允許的操作 [ [email protected] 11]$ rm -rf 123 rm: 無法刪除"123": 不允許的操作 [ [email protected] 11]$ touch 234.txt [ [email protected] 11]$ ls -l drwxrwxr-x 2 test1 test1 4096 3月 19 20:14 12 -rw-rw-r-- 1 test1 test1 0 3月 19 20:15 123 -rw-rw-r-- 1 test2 test2 0 3月 19 20:16 234.txt drwxr-xr-x 2 root root 4096 3月 19 20:14 root
可以建立目錄或檔案,但是不能刪除其他使用者的目錄或檔案。
本文出自 “模範生的學習部落格” 部落格,請務必保留此出處http://8802265.blog.51cto.com/8792265/1622566
linux下目錄與檔案的許可權及特殊許可權