zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Linux命令之chmod

Linux命令 chmod
2023-09-11 14:17:22 时间

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--