chmod - change file mode bits
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...
三类用户:
u: 属主
g: 属组
o: 其他
a: 所有
chmod [OPTION]... MODE[,MODE]... FILE...
(1)MODE 表示法:
A, 赋权表示法: 直接赋予用户对应权限, 可以操作一类用户的所有权限位,
u=
g=
o=
a=
两种用户可以合并书写: ug=,uo=,go=,
权限可合并书写, r,w,x,rw,rwx,rx,wx,
B, 授权表示法: 直接增加或删除一类用户的现有权限 r,w,x;
u+,u-
g+,g-
o+,o-
a+,a- (有时候可省略 a, 直接写成 +,-)
两种用户可以合并书写: ug=,uo=,go=,
权限也可合并书写, r,w,x,rw,rwx,rx,wx,
尽量符合逻辑书写, 不符合逻辑也不会报错 (例如其他用户没有 w 权限用 o-x 也不会报错)
分别加不同权限时可以用逗号隔开, 如: chmod u+w,g+r FILE
ps: 全局 + w 操作只会匹配到 u(属主用户)
(2) 八进制表示法
chmod [OPTION]... OCTAL-MODE FILE...
example:
chmod 660 /etc/shadow
(3) 引用权限命令
chmod [OPTION]... --reference=RFILE FILE...
修改文件 FILE 的权限与被引用的文件 RFILE 的权限相同
选项 :
-R,--recurisive: 递归修改, 会将目录以及目录下的子目录和所有文件一起修改 (此选项只建议授权表示法使用, 避免影响目录内部的文件拥有其他不需要的权限, 例如错误的将 x 权限给不可执行的文件)
注意: 用户仅能修改属主为自己的那些文件的权限
exmaple. 赋权表示
[[email protected] ~]# ls
00-02-17 a_c a_d anaconda-ks.cfg b_c b_d Desktop Documents Downloads Music Pictures Public Templates Videos 桌面
[[email protected] ~]# ll anaconda-ks.cfg
-rw-------. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg
[[email protected] ~]# chmod g=rw anaconda-ks.cfg
[[email protected] ~]# ll anaconda-ks.cfg
-rw-rw----. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg
[[email protected] ~]# chmod ug=r anaconda-ks.cfg
[[email protected] ~]# ll anaconda-ks.cfg
-r--r-----. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg
[[email protected] ~]# chmod a=rwx anaconda-ks.cfg
[[email protected] ~]# ll anaconda-ks.cfg
-rwxrwxrwx. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg
[[email protected] ~]# chmod u=rwx,g=rw,o= anaconda-ks.cfg
[[email protected] ~]# ll anaconda-ks.cfg
-rwxrw----. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg
授权表示
[[email protected] ~]# ll anaconda-ks.cfg
----------. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg
[[email protected] ~]# chmod u+w anaconda-ks.cfg
[[email protected] ~]# ll anaconda-ks.cfg
--w-------. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg
[[email protected] ~]# chmod ug+r anaconda-ks.cfg
[[email protected] ~]# ll anaconda-ks.cfg
-rw-r-----. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg
[[email protected] ~]# chmod a+x anaconda-ks.cfg
[[email protected] ~]# ll anaconda-ks.cfg
-rwxr-x--x. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg
[[email protected] ~]# chmod -x anaconda-ks.cfg
[[email protected] ~]# ll anaconda-ks.cfg
-rw-r-----. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg
[[email protected] ~]# chmod u-w anaconda-ks.cfg
[[email protected] ~]# ll anaconda-ks.cfg
-r--r-----. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg
[[email protected] ~]# chmod +w anaconda-ks.cfg
[[email protected] ~]# ll anaconda-ks.cfg
-rw-r-----. 1 root root 1257 3 月 19 19:05 anaconda-ks.cfg // 全局 + w 操作只会匹配到 u(属主用户), 因为全局 + w 很危险
权限引用
[[email protected] tmp]# ll | head -3
总用量 276
-rw-r--r--. 1 root root 0 4 月 19 19:16 a
-rw-r--r--. 1 root root 0 4 月 19 19:16 b
[[email protected] tmp]# chmod 766 a
[[email protected] tmp]# ll | head -3
总用量 276
-rwxrw-rw-. 1 root root 0 4 月 19 19:16 a
-rw-r--r--. 1 root root 0 4 月 19 19:16 b
[[email protected] tmp]# chmod --reference=a b
[[email protected] tmp]# ll | head -3
总用量 276
-rwxrw-rw-. 1 root root 0 4 月 19 19:16 a
-rwxrw-rw-. 1 root root 0 4 月 19 19:16 b
-R 选项
[email protected] tmp]# ls -la skel
总用量 16
drwxr-xr-x. 3 root root 78 4 月 19 19:43 .
drwxrwxrwt. 24 root root 4096 4 月 19 19:43 ..
-rw-r--r--. 1 root root 18 4 月 19 19:43 .bash_logout
-rw-r--r--. 1 root root 193 4 月 19 19:43 .bash_profile
-rw-r--r--. 1 root root 231 4 月 19 19:43 .bashrc
drwxr-xr-x. 4 root root 39 4 月 19 19:43 .mozilla
[[email protected] tmp]# chmod -R go= skel
[[email protected] tmp]# ls -la skel
总用量 16
drwx------. 3 root root 78 4 月 19 19:43 .
drwxrwxrwt. 24 root root 4096 4 月 19 19:43 ..
-rw-------. 1 root root 18 4 月 19 19:43 .bash_logout
-rw-------. 1 root root 193 4 月 19 19:43 .bash_profile
-rw-------. 1 root root 231 4 月 19 19:43 .bashrc
drwx------. 4 root root 39 4 月 19 19:43 .mozilla
[[email protected] tmp]# chmod -R g+r skel/
[[email protected] tmp]# ls -la skel
总用量 16
drwxr-----. 3 root root 78 4 月 19 19:43 .
drwxrwxrwt. 24 root root 4096 4 月 19 19:45 ..
-rw-r-----. 1 root root 18 4 月 19 19:43 .bash_logout
-rw-r-----. 1 root root 193 4 月 19 19:43 .bash_profile
-rw-r-----. 1 root root 231 4 月 19 19:43 .bashrc
drwxr-----. 4 root root 39 4 月 19 19:43 .mozilla
来源: http://www.bubuko.com/infodetail-3033199.html