查看文件信息,使用ls -l命令,執行後其結果以下:bash
[root@test-01 ~]# ls -l 總用量 4 drwxr-xr-x. 2 root root 6 12月 19 08:17 4 -rw-------. 1 root root 973 12月 12 17:09 anaconda-ks.cfg
如上面顯示,一個文件類型由第一位表示,文件或目錄的權限由後面9個權限位組成,三位一組,2-4位表示文件或目錄的全部者的權限,5-7位表示文件所屬組對這個文件的權限,8-10位表示出去全部者和所屬組外其餘用戶對它的權限。權限表示順序是是否可讀、是否可寫、是否可執行=rwx ,好比說沒有寫的權限,就表示爲r-x 。 其中r=4 w=2 x=1 rwx=7 ,rw-=6 , --x=1 , rw-r--r--=644 。unix
[root@test-01 tmp]# ls -la 1 總用量 4 drwxr-xr-x. 2 root root 6 12月 19 21:13 . drwxrwxrwt. 9 root root 4096 12月 21 15:38 .. [root@test-01 tmp]# chmod 700 1 [root@test-01 tmp]# !ls ls -la 1 總用量 4 drwx------. 2 root root 6 12月 19 21:13 . drwxrwxrwt. 9 root root 4096 12月 21 15:38 .. [root@test-01 tmp]#
這樣就更改了目錄tmp/1的權限。drwxr-xr-x.這個字符串最後一個.表示這個目錄或文件受制於SELinux。code
chmod -R 命令更改目錄的權限時會將該目錄下全部的子目錄,子文件都更改權限。級聯更改。字符串
chmod u=rwx,g=r,o=r filname。這條命令當中,u 表示全部者,g 表示所屬組,o表示其餘用戶。 chmod a-x filname,其中a表示全部,這條命令的意思是,對於這個文件,全部者、所屬組、其餘用戶都沒有執行權限;chmod a+x _filname_這條命令表示對於這個文件,全部者,所屬組,其餘用戶都有執行權限。同步
使用格式爲 :chown _username _filnameit
[root@test-01 ~]# useradd lc1 [root@test-01 ~]# useradd lc2 [root@test-01 ~]# ls -l /tmp/yum.log -rw-------. 1 root root 0 12月 12 17:07 /tmp/yum.log [root@test-01 ~]# chown lc1 /tmp/yum.log [root@test-01 ~]# !ls ls -l /tmp/yum.log -rw-------. 1 lc1 root 0 12月 12 17:07 /tmp/yum.log [root@test-01 ~]#
命令格式爲:chgrp_ username__ filename_test
[root@test-01 ~]# !ls ls -l /tmp/yum.log -rw-------. 1 lc1 root 0 12月 12 17:07 /tmp/yum.log [root@test-01 ~]# chgrp lc1 /tmp/yum.log [root@test-01 ~]# !ls ls -l /tmp/yum.log -rw-------. 1 lc1 lc1 0 12月 12 17:07 /tmp/yum.log [root@test-01 ~]#
*使用chown 也能夠更改文件的所屬組,其使用格式爲:chown username:group filename,這條命令中若是username省略,表示只更改所屬組。date
chattr的格式爲chattr [+-=] 參數 [文件或目錄] 參數以下: A:增長該屬性後,表示文件或目錄的atime將不可修改 s:增長該屬性後,會將數據同步寫入磁盤中 a:增長該屬性後,表示只能追加內容,不能刪除,只能root用戶設定該屬性 c:增長該屬性後,表示自動壓縮該文件,讀取時會自動解壓 i:增長該屬性後,表示文件不能刪除、重命名、設定連接、寫入及新增數據 經常使用的參數是a和ifile
[root@test-01 ~]# chattr +a /tmp/1 [root@test-01 ~]# lsattr /tmp/1 [root@test-01 ~]# lsattr /tmp ---------------- /tmp/yum.log -----a-A-------- /tmp/1 [root@test-01 ~]# chattr -A /tmp/1 [root@test-01 ~]# lsattr /tmp/1 [root@test-01 ~]# lsattr /tmp ---------------- /tmp/yum.log -----a---------- /tmp/1 [root@test-01 ~]# mkdir /tmp/1/2 [root@test-01 ~]# rmdir /tmp/1/2 rmdir: 刪除 "/tmp/1/2" 失敗: 不容許的操做 [root@test-01 ~]#
一個文件被賦予這個a的屬性後,只能追加內容,不能刪除文件裏面的內容權限
[root@test-01 ~]# vi /tmp/1/1.txt [root@test-01 ~]# chattr +a /tmp/1/1.txt [root@test-01 ~]# !head head -n 10 /etc/passwd >> /tmp/1/1.txt [root@test-01 ~]# vi /tmp/1/1.txt E325: ATTENTION Found a swap file by the name "/tmp/1/1_txt.swp" owned by: root dated: Sat Dec 23 03:30:18 2017 file name: /tmp/1/1.txt modified: no /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown ~ ~ ~ "/tmp/1/1.txt" "/tmp/1/1.txt" E212: Can't open file for writing Press ENTER or type command to continue [root@test-01 ~]# rm -f /tmp/1/1.txt rm: 沒法刪除"/tmp/1/1.txt": 不容許的操做 [root@test-01 ~]#
當一個文件被賦予i屬性時,不能刪除,不能重命名不能寫入,不能新增數據
[root@test-01 ~]# head -n 10 /etc/passwd >> /tmp/1/1.txt -bash: /tmp/1/1.txt: 權限不夠 [root@test-01 ~]# vi /tmp/1/1.txt [root@test-01 ~]# mv /tmp/1/1.txt /tmp/1/2.txt mv: 沒法將"/tmp/1/1.txt" 移動至"/tmp/1/2.txt": 不容許的操做 [root@test-01 ~]#
該命令用於讀取文件或目錄的特殊權限,格式爲:lsattr 參數 文件或目錄 a:相似於ls -a 選項,連同隱藏的文件一同列出 R:連同子目錄的數據一同列出
[root@test-01 ~]# lsattr -R /tmp ---------------- /tmp/yum.log ---------------- /tmp/1 /tmp/1: ---------------- /tmp/1/2 /tmp/1/2: ----i----------- /tmp/1/1.txt ---------------- /tmp/1/1_txt.swp ---------------- /tmp/1/1_txt.swo ---------------- /tmp/1/1_txt.swn [root@test-01 ~]# lsattr -a /tmp ---------------- /tmp/. ---------------- /tmp/.. ---------------- /tmp/yum.log ---------------- /tmp/.X11-unix ---------------- /tmp/.ICE-unix ---------------- /tmp/.XIM-unix ---------------- /tmp/.font-unix ---------------- /tmp/.Test-unix ---------------- /tmp/1 [root@test-01 ~]#