zoukankan      html  css  js  c++  java
  • Linux命令总结--chmod命令

    1.chmod命令介绍

         Linux/Unix 的文件调用权限分为三级 : 文件拥有者、群组、其他。

         chmod命令用于改变linux系统文件或目录的访问权限。用它控制文件或目录的访问权限。该命令有两种用法。一种是包含字母和操作符表达式的文字设定法;另一种是包含数字的数字设定法。

    使用权限 : 所有使用者

    2.语法

    chmod [-cfvR] [--help] [--version] mode file...

    3.参数说明

    mode : 权限设定字串,格式如下 :

    [ugoa...][[+-=][rwxX]...][,...]

    3. 命令参数:

    必要参数:

    -c 当发生改变时,报告处理信息
    -f 错误信息不输出
    -R 处理指定目录以及其子目录下的所有文件
    -v 运行时显示详细处理信息

    选择参数:

    --reference=<目录或者文件> 设置成具有指定目录或者文件具有相同的权限
    --version 显示版本信息
    <权限范围>+<权限设置> 使权限范围内的目录或者文件具有指定的权限
    <权限范围>-<权限设置> 删除权限范围的目录或者文件的指定权限
    <权限范围>=<权限设置> 设置权限范围内的目录或者文件的权限为指定的值

    权限范围:

    u :目录或者文件的当前的用户
    g :目录或者文件的当前的群组
    o :除了目录或者文件的当前用户或群组之外的用户或者群组
    a :所有的用户及群组

    权限代号:

    r :读权限,用数字4表示
    w :写权限,用数字2表示
    x :执行权限,用数字1表示
    - :删除权限,用数字0表示
    s :特殊权限 

    该命令有两种用法。一种是包含字母和操作符表达式的文字设定法;另一种是包含数字的数字设定法。

    1). 文字设定法:

    chmod [who] [+ | - | =] [mode] 文件名

    2). 数字设定法

    我们必须首先了解用数字表示的属性的含义:0表示没有权限,1表示可执行权限,2表示可写权限,4表示可读权限,然后将其相加。所以数字属性的格式应为3个从0到7的八进制数,其顺序是(u)(g)(o)。

    例如,如果想让某个文件的属主有“读/写”二种权限,需要把4(可读)+2(可写)=6(读/写)。

    数字设定法的一般形式为:

    chmod [mode] 文件名

    数字与字符对应关系如下:

    r=4,w=2,x=1
    若要rwx属性则4+2+1=7
    若要rw-属性则4+2=6;
    若要r-x属性则4+1=7。 

    4.实例

    实例1:增加文件所有用户组可执行权限

    命令:

    chmod a+x log2012.log

    输出:

    [root@localhost test]# ls -al log2012.log 

    -rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

    [root@localhost test]# chmod a+x log2012.log 

    [root@localhost test]# ls -al log2012.log 

    -rwxr-xr-x 1 root root 302108 11-13 06:03 log2012.log

    [root@localhost test]#

    说明:

    即设定文件log2012.log的属性为:文件属主(u) 增加执行权限;与文件属主同组用户(g) 增加执行权限;其他用户(o) 增加执行权限。

    实例2:同时修改不同用户权限

    命令:

    chmod ug+w,o-x log2012.log

    输出:

    [root@localhost test]# ls -al log2012.log 

    -rwxr-xr-x 1 root root 302108 11-13 06:03 log2012.log

    [root@localhost test]# chmod ug+w,o-x log2012.log 

    [root@localhost test]# ls -al log2012.log 

    -rwxrwxr-- 1 root root 302108 11-13 06:03 log2012.log


    说明:

    即设定文件text的属性为:文件属主(u) 增加写权限;与文件属主同组用户(g) 增加写权限;其他用户(o) 删除执行权限

    实例3:删除文件权限

    命令:

    chmod a-x log2012.log

    输出:

    [root@localhost test]# ls -al log2012.log 

    -rwxrwxr-- 1 root root 302108 11-13 06:03 log2012.log

    [root@localhost test]# chmod a-x log2012.log 

    [root@localhost test]# ls -al log2012.log 

    -rw-rw-r-- 1 root root 302108 11-13 06:03 log2012.log

    说明:

    删除所有用户的可执行权限 

    实例4:使用“=”设置权限 

    命令:

    chmod u=x log2012.log

    输出:

    [root@localhost test]# ls -al log2012.log 

    -rw-rw-r-- 1 root root 302108 11-13 06:03 log2012.log

    [root@localhost test]# chmod u=x log2012.log 

    [root@localhost test]# ls -al log2012.log 

    ---xrw-r-- 1 root root 302108 11-13 06:03 log2012.log

    说明:

    撤销原来所有的权限,然后使拥有者具有可读权限 

    实例5:对一个目录及其子目录所有文件添加权限 

    命令:

    chmod -R u+x test4

    输出:

    [root@localhost test]# cd test4

    [root@localhost test4]# ls -al

    总计 312drwxrwxr-x 2 root root   4096 11-13 05:50 .

    drwxr-xr-x 5 root root   4096 11-22 06:58 ..

    -rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

    -rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

    -rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

    [root@localhost test4]# cd ..

    [root@localhost test]# chmod -R u+x test4

    [root@localhost test]# cd test4

    [root@localhost test4]# ls -al

    总计 312drwxrwxr-x 2 root root   4096 11-13 05:50 .

    drwxr-xr-x 5 root root   4096 11-22 06:58 ..

    -rwxr--r-- 1 root root 302108 11-12 22:54 log2012.log

    -rwxr--r-- 1 root root     61 11-12 22:54 log2013.log

    -rwxr--r-- 1 root root      0 11-12 22:54 log2014.log

    说明:

    递归地给test4目录下所有文件和子目录的属主分配权限 

    其他一些实例:

    将文件 file1.txt 设为所有人皆可读取 :

    chmod ugo+r file1.txt

    将文件 file1.txt 设为所有人皆可读取 :

    chmod a+r file1.txt

    将文件 file1.txt 与 file2.txt 设为该文件拥有者,与其所属同一个群体者可写入,但其他以外的人则不可写入 :

    chmod ug+w,o-w file1.txt file2.txt

    将 ex1.py 设定为只有该文件拥有者可以执行 :

    chmod u+x ex1.py

    将目前目录下的所有文件与子目录皆设为任何人可读取 :

    chmod -R a+r *

    此外chmod也可以用数字来表示权限如 :

    chmod 777 file

    语法为:

    chmod abc file

    其中a,b,c各为一个数字,分别表示User、Group、及Other的权限。

    r=4,w=2,x=1

    • 若要rwx属性则4+2+1=7;
    • 若要rw-属性则4+2=6;
    • 若要r-x属性则4+1=5。
    chmod a=rwx file

    chmod 777 file

    效果相同

    chmod ug=rwx,o=x file

    chmod 771 file

    效果相同

    若用chmod 4755 filename可使此程序具有root的权限

  • 相关阅读:
    每日日报1
    shazidouhui的使用体验
    水滴的使用体验
    麻雀记的使用体验
    钢镚儿的使用体验
    TD课程通的使用体验
    01 fs模块
    0 mysql 安装
    slot
    vue引入 lodash
  • 原文地址:https://www.cnblogs.com/hanjiali/p/11582351.html
Copyright © 2011-2022 走看看