chmod [選項] … MODE[,MODE] … FILE …
chmod [選項] … OCTAL-MODE FILE …
chmod [選項] … --reference=RFILE FILE …
chmod命令用來更改指定文件訪問權限。將改變每個FILE的模式為MODE。使用—reference時,將每個FILE的模式更改為RFILE的模式。
MODE有兩種更改方式:一種是用符號標記進行更改,另一種是采用八進制數指定新的訪問權限。
符號模式的格式是[ugoa…][[+ - =][perms…]…],其中perms可以來自集合{rwxXst}中的零個或多個,也可以來自集合{ugo}中的一個。可以給出多個符號模式,以逗號分隔。
字母ugoa的組合控制哪些用戶對文件的訪問權限將被更改:文件的所有者(u),在文件組中的用戶(g),其他的用戶(o),所有用戶(a)。如果沒有給出,則默認使用a,但是在umask中設置的位不會受到影響。注意:這里所有的文件組都指代的是文件所有者所在的組
操作符’+’給指定文件添加用戶所選的權限;操作符’-’使得指定文件撤消用戶所選的權限;’=’使得指定文件只有用戶所選的權限(少增多刪)。
集合{rwxXst}和集合{ugo}給用戶選擇文件新的屬性:讀取(r)、寫入(w)、執行(或搜索目錄)(x)、精當文件是目錄或已經具有某些執行權限時才執行/搜索用戶(X)、在執行時設置用戶或組ID(s)、限制刪除標志或粘滯位(t)、授予目標文件的所有者權限(u)、授予目標文件的文件組成員權限(g)、授予目標文件的其他用戶權限(o)。
數字模式是從1到 4個八進制數,數值從0到7,每個數由0與位權為4,2,1的3個數疊加得出。被省略的數字缺省值為0。第一個八進制數設置用戶ID(4)、組ID(2)、限制刪除和粘滯位(1);第二個八進制數設置擁有該文件的用戶權限:讀取(4)、寫入(2)和執行(1)。第三個八進制數設置文件組中其他用戶的權限,值與第二個八進制數相同;第四個八進制數設置其他用戶的權限,值與第二個八進制數相同。注意:這里的省略值是在前面,例如444,完整就是0444。
chmod永遠不會改變符號鏈接的權限;chmod系統調用無法更改其權限。但這不是問題,因為從不使用符號鏈接的權限。對於命令行中列出的每個符號鏈接,chmod會更改指向文件的權限,但忽略遞歸目錄遍歷時遇到的符號鏈接。
(1).選項
-c,--change 類型verbose,但僅在進行更改時報告。 -f,--silent,--quiet 不列出大多數錯誤信息。 -v,--verbose 為每個處理為文件輸出診信息。 --no-preserve-root 要特別對待’/’(根目錄?)(默認) --perserve-root 無法以’/’(根目錄?)遞歸操作 --reference=RFILE 使用RFILE的MODE,而不是指定的MODE值 -R,--recurive 以遞歸方式操作文件和目錄。 --help 打印幫助並退出 --version 打印版本信息並退出
(2).實例
符號模式
[root@xuexi xf]# ls -l 1.txt -rw-rw-r--. 1 xf xf 0 11月 7 13:28 1.txt [root@xuexi xf]# chmod o+w 1.txt //增加權限 [root@xuexi xf]# ls -l 1.txt -rw-rw-rw-. 1 xf xf 0 11月 7 13:28 1.txt [root@xuexi xf]# chmod a-r 1.txt //撤銷權限 [root@xuexi xf]# ls -l 1.txt --w--w--w-. 1 xf xf 0 11月 7 13:28 1.txt [root@xuexi xf]# chmod a=rw 1.txt //完全匹配權限,多刪少加 [root@xuexi xf]# ls -l 1.txt -rw-rw-rw-. 1 xf xf 0 11月 7 13:28 1.txt [root@xuexi xf]# chmod u=rwx,g=r,o=--- 1.txt //不同用戶不同要求 [root@xuexi xf]# ls -l 1.txt -rwxr-----. 1 xf xf 0 11月 7 13:28 1.txt
數字模式
[root@xuexi xf]# chmod 4 1.txt [root@xuexi xf]# ls -l 1.txt -------r--. 1 xf xf 0 11月 7 13:28 1.txt [root@xuexi xf]# chmod o=r 1.txt //可以看出這兩個是等價的 [root@xuexi xf]# ls -l 1.txt -------r--. 1 xf xf 0 11月 7 13:28 1.txt
遞歸目錄,設置權限
[root@xuexi xf]# ls -l 總用量 0 drwxrwxr-x. 2 xf xf 32 11月 7 13:29 newDir drwxr-xr-x. 2 xf xf 6 9月 6 11:43 公共 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 模板 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 視頻 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 圖片 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 文檔 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 下載 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 音樂 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 桌面 [root@xuexi xf]# ls -l newDir/ 總用量 0 -rw-rw-r--. 1 xf xf 0 11月 7 13:29 2.txt -rw-rw-r--. 1 xf xf 0 11月 7 13:29 3.txt [root@xuexi xf]# chmod -R o=--- newDir [root@xuexi xf]# ls -l 總用量 0 drwxrwx---. 2 xf xf 32 11月 7 13:29 newDir drwxr-xr-x. 2 xf xf 6 9月 6 11:43 公共 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 模板 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 視頻 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 圖片 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 文檔 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 下載 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 音樂 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 桌面 [root@xuexi xf]# ls -l newDir/ 總用量 0 -rw-rw----. 1 xf xf 0 11月 7 13:29 2.txt -rw-rw----. 1 xf xf 0 11月 7 13:29 3.txt
注意:只要是文件所有者就一定可以寫文件,哪怕文件權限是000。vim進入文件,wq!強制保存文件。
(3).擴展
1)補碼(了解)
創建文件和目錄時,默認權限是根據/etc/profile(全局永久應用)和當前用戶主目錄(也叫家目錄)的.bash_profile或.profile(個人永久應用)中設置的umask值決定。
umask命令允許創建文件時使用缺省值,對應每一類用戶(所屬主、所屬組、其他用戶)。
文件默認值666(rw-rw-rw-),目錄默認值777(rwxrwxrwx)。
例如/etc/profile中判斷標准如下:
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then umask 002 else umask 022 fi
說明:如果UID大於199,並且用戶名和組名相同umask值為002(-------w-),否則為022(----w--w-)。
注:-gt在shell中表示大於;id-g顯示組ID,id-gn顯示組名;id-u顯示用戶ID,id-un顯示用戶名。
如果root用戶創建,那么計算方法:
創建文件時默認權限=文件初始默認666(rw-rw-rw-)去除022(----w--w-)=664(rw-r--r--)
創建目錄時默認權限=目錄初始默認777(rwxrwxrwx)去除022(----w--w-)=755(rwxr-xr-x)
另外還可以用umask命令臨時修改,實例如下:
[root@xuexi ~]# umask 033 [root@xuexi ~]# touch 1 [root@xuexi ~]# ls -l 1 -rw-r--r--. 1 root root 0 1月 8 14:28 1
就算過程:創建文件時默認權限=文件初始默認666(rw-rw-rw-)去除033(----wx-wx)=664(rw-r--r--)。文件默認初始沒有x權限,所以也沒法去除x權限,可以無視。
2)文件的特殊權限suid、sgid、sticky(了解)
suid:程序運行時會臨時擁有該文件的所有者權限。
限定,只能設置在二進制可執行程序上,對目錄無效。
sgid:在設置了sgid權限的目錄下新建文件,該文件的所屬組集成上級目錄的所屬組。
限定,既可以給二進制可執行程序設置,也可以給目錄設置。
sticky:目錄下創建的文件只有root、文件創建者、目錄所有者才能刪除。
限定,粘滯位權限是針對目錄的,對文件無效,也叫防刪除位。
特殊權限 |
實現方式(數字模式可以混合使用) |
suid |
u+s或4XXX(后面是正常權限) |
sgid |
g+s或2XXX(后面是正常權限) |
sticky |
o+t或1XXX(后面是正常權限) |
suid實例:設置suid權限
[root@xuexi ~]# ll /usr/bin/passwd -rwsr-xr-x. 1 root root 27832 6月 10 2014 /usr/bin/passwd [root@xuexi ~]# ll /usr/bin/less -rwxr-xr-x. 1 root root 158240 7月 31 2015 /usr/bin/less Try 'chmod --help' for more information. [root@xuexi ~]# chmod 4755 /usr/bin/less [root@xuexi ~]# ll /usr/bin/less -rwsr-xr-x. 1 root root 158240 7月 31 2015 /usr/bin/less
sgid實例:設置sgid權限
[root@xuexi ~]# mkdir test [root@xuexi ~]# ll -d test drwxr-xr-x. 2 root root 6 1月 8 16:29 test [root@xuexi ~]# chmod g+s test [root@xuexi ~]# ll -d test drwxr-sr-x. 2 root root 6 1月 8 16:29 test [root@xuexi ~]# chown :bin test/ [root@xuexi ~]# ll -d test drwxr-sr-x. 2 root bin 6 1月 8 16:30 test [root@xuexi ~]# touch test/1 [root@xuexi ~]# ll test/1 -rw-r--r--. 1 root bin 0 1月 8 16:31 test/1
sticky實例:設置sticky權限
[root@xuexi ~]# chmod g-s test [root@xuexi ~]# ll -d test drwxr-xr-x. 2 root bin 15 1月 8 16:31 test [root@xuexi ~]# chmod o+t test [root@xuexi ~]# ll -d test drwxr-xr-t. 2 root bin 15 1月 8 16:31 test
3)文件擴展權限ACL(了解)
setfacl [選項] file
某用戶不是文件所屬主,也不在所屬組,但又筆other所要的權限多時使用此命令
(1).常用選項
-b,--remove-all 刪除所有擴展的ACL項
-d,--default 設置默認ACL,可對目錄使用此選項,目錄下創建新的文件或目錄將繼承指定的ACL權限
-m,--modify=acl 修改當前ACL項
-R,--recursive 遞歸到子目錄
-x,--remove=acl 從文件的ACL中刪除條目
ACL格式:
[d[efault]:] [u[ser]:]uid [:perms] 指定用戶的權限,文件所有者的權限(如果uid沒有指定)。 [d[efault]:] g[roup]:gid [:perms] 指定群組的權限,文件所有群組的權限(如果gid未指定) [d[efault]:] m[ask][:] [:perms] 有效權限掩碼 [d[efault]:] o[ther] [:perms] 其他的權限
(2).實例
給文件添加某個用戶的擴展權限
[root@xuexi ~]# touch 1 [root@xuexi ~]# ll 1 -rw-r--r--. 1 root root 0 1月 9 10:33 1 [root@xuexi ~]# setfacl -m u:xf:rwx 1 //u代表用戶 [root@xuexi ~]# ll 1 -rw-rwxr--+ 1 root root 0 1月 9 10:33 1 [root@xuexi ~]# getfacl 1 # file: 1 # owner: root # group: root user::rw- user:xf:rwx group::r-- mask::rwx other::r--
給文件夾添加某個用戶擴展權限
[root@xuexi ~]# mkdir test [root@xuexi ~]# ll -d test drwxr-xr-x. 2 root root 6 1月 9 10:38 test [root@xuexi ~]# setfacl -m d:u:xf:rwx test //d設置默認ACL,新建的文件或目錄繼承指定ACL權限 [root@xuexi ~]# ll -d test drwxr-xr-x+ 2 root root 6 1月 9 10:38 test [root@xuexi ~]# getfacl test # file: test # owner: root # group: root user::rwx group::r-x other::r-x default:user::rwx default:user:xf:rwx default:group::r-x default:mask::rwx default:other::r-x
對目錄遞歸添加某個用戶的擴展權限
[root@xuexi ~]# mkdir t1 [root@xuexi ~]# touch t1/{a,b,c} [root@xuexi ~]# ll -d t1 drwxr-xr-x. 2 root root 33 1月 9 10:43 t1 [root@xuexi ~]# ll t1/ 總用量 0 -rw-r--r--. 1 root root 0 1月 9 10:43 a -rw-r--r--. 1 root root 0 1月 9 10:43 b -rw-r--r--. 1 root root 0 1月 9 10:43 c [root@xuexi ~]# setfacl -R -m u:xf:rwx t1/ //-R要在-m前,因為-m要加參數 [root@xuexi ~]# ll -d t1 drwxrwxr-x+ 2 root root 33 1月 9 10:43 t1 [root@xuexi ~]# ll t1/ 總用量 0 -rw-rwxr--+ 1 root root 0 1月 9 10:43 a -rw-rwxr--+ 1 root root 0 1月 9 10:43 b -rw-rwxr--+ 1 root root 0 1月 9 10:43 c [root@xuexi ~]# getfacl t1 # file: t1 # owner: root # group: root user::rwx user:xf:rwx group::r-x mask::rwx other::r-x [root@xuexi ~]# getfacl t1/a # file: t1/a # owner: root # group: root user::rw- user:xf:rwx group::r-- mask::rwx other::r--
給文件或文件夾去掉某個用戶的擴展權限
[root@xuexi ~]# setfacl -x u:xf t1/a [root@xuexi ~]# ll t1/a -rw-r--r--+ 1 root root 0 1月 9 10:43 t1/a [root@xuexi ~]# getfacl t1/a # file: t1/a # owner: root # group: root user::rw- group::r-- mask::r-- other::r--
給文件或文件夾去掉所有擴展權限
[root@xuexi ~]# setfacl -b t1/b [root@xuexi ~]# ll t1/b -rw-r--r--. 1 root root 0 1月 9 10:43 t1/b [root@xuexi ~]# getfacl t1/b # file: t1/b # owner: root # group: root user::rw- group::r-- other::r--