zoukankan      html  css  js  c++  java
  • linux中权限管理命令(chmod/chown/chgrp/unmask)

    目录

    • chmod
    • chown
    • chgrp
    • umask

    chmod

    解释

    命令名称:chmod
    命令英文原意:change the permissions mode of a file
    命令所在路径:/bin/chmod
    执行权限:所有用户
    功能描述:改变文件或目录权限
    

    语法

    chmod [{ugoa}{+-=}{rwx}] [文件或目录] 
    chmod [mode=421] [文件或目录]
        -R 递归修改
        
    # 第一种修改方式   chmod [{ugoa}{+-=}{rwx}] [文件或目录]
    ugoa:
        u:所有者
        g:所属组
        o:其他人
        a:所有人
    +-=:
        +:针对文件或目录增加某个权限
        -:针对文件或目录减少某个权限
        =:赋予文件或目录全新的权限,以此刻的权限为准
        
    # 第二种修改方式  chmod [mode=421] [文件或目录]
    rwx:
        r:4
        w:2
        x:1
    rwxrw-r--
        权限:764(4+2+1=7/4+2=6/4)
    

    示例

    # 第一种增加权限
     chmod g+x test.txt
     
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
    -rw-r--r-- 1 root root 11 Nov 28 15:39 test.txt
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod g+x test.txt
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
    -rw-r-xr-- 1 root root 11 Nov 28 15:39 test.txt
    
    
    # 第二种增加权限
    chmod 777 test.txt
    
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
    -rw-r-xr-- 1 root root 11 Nov 28 15:39 test.txt
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 777 test.txt
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
    -rwxrwxrwx 1 root root 11 Nov 28 15:39 test.txt
    
    

    权限特别注意

    root用户进入的时候显示#,普通用户显示$

    # 在/tmp下新建文件夹test
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# mkdir test
    
    # 在/tmp/test文件夹下新建test.txt
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# touch test/test.txt
    
    # 查看test文件下的文件
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test
    total 0
    -rw-r--r-- 1 root root 0 Nov 28 17:54 test.txt
    
    # 查看/tmp/test文件夹的权限
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -ld test
    drwxr-xr-x 2 root root 4096 Nov 28 17:54 test
    
    # 赋予/tmp/test文件夹全部的权限
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 777 test
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -ld test
    drwxrwxrwx 2 root root 4096 Nov 28 17:54 test
    
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test/test.txt
    -rw-r--r-- 1 root root 0 Nov 28 17:54 test/test.txt
    
    # 新增加一个普通用户并修改密码
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# useradd eternity
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# passwd eternity
    
    
    
    
    # 使用eternity帐号,密码123456,登录服务器
    # 查看当前目录
    [eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ pwd
    /home/eternity
    
    # 进入/tmp目录
    [eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ cd /tmp
    
    # 查看/tmp/test目录的权限,拥有全部权限
    [eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -ld test
    drwxrwxrwx 2 root root 4096 Nov 28 17:54 test
    
    # /tmp/test目录下存在test.txt,拥有读权限
    [eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -l test/test.txt
    -rw-r--r-- 1 root root 0 Nov 28 17:54 test/test.txt
    
    # 删除/tmp/test下的test.txt文件
    [eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ rm test/test.txt
    rm: remove write-protected regular empty file ‘test/test.txt’? y
    
    # 删除成功,此时/tmp/test目录下test.txt已经没有了
    [eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -l test/test.txt
    ls: cannot access test/test.txt: No such file or directory
    

    只有管理员拥有rw读写权限,所属组和其他人只有读权限,但是此时普通用户却删除了只有r读权限的文件,为什么????

    文件目录权限总结

    代表字符 权限 对文件的含义 对目录的含义
    r 读权限 可以查看文件内容 可以列出目录中的内容
    w 写权限 可以修改文件内容 可以在目录中创建和删除文件
    x 执行权限 可以执行文件 可以进入目录

    分析

    对于文件有写权限,仅仅代表可以修改文件的内容,而没有删除文件的权限
    
    对于目录有写权限,可以在目录中创建和删除文件
    
    因为上面的/tmp/test目录的权限为777
    所以普通用户对于/tmp/test目录也具有创建文件和删除文件的权限
    所以,普通用户也能删除/tmp/test/test.txt文件
    但是普通用户无法编辑/tmp/test/test.txt文件,使用vim编辑文件的时候,会提示Waring: Changing a readonly file
    

    chown

    解释

    命令名称:chown
    命令英文原意:change file ownership
    命令所在路径:/bin/chown
    执行权限:所有用户
    功能描述:改变文件或目录的所有者
    

    语法

    chown [用户] [文件或目录]
    

    在linux中只有root能改变文件所有者,即便是创建者都不可以

    示例

    # 改变文件所有者(将test.txt的所有者由eternity更改为root)
    chown root /tmp/test/test.txt
    
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# pwd
    /root
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
    -rw-r--r-- 1 eternity eternity 7 Nov 28 18:15 /tmp/test/test.txt
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# chown root /tmp/test/test.txt
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
    -rw-r--r-- 1 root eternity 7 Nov 28 18:15 /tmp/test/test.txt
    

    chgrp

    解释

    命令名称:chgrp
    命令英文原意:change file group ownership
    命令所在路径:/bin/chgrp
    执行权限:所有用户
    功能描述:改变文件或目录的所属组
    

    语法

    chgrp [用户组] [文件或目录]
    

    示例

    # 改变文件所属组(将test.txt的所属组由eternity更改为eternityz)
    chgrp eternityz /tmp/test/test.txt
    
    # 当前目录
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# pwd
    /root
    # 查看详细信息
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
    -rw-r--r-- 1 root eternity 7 Nov 28 18:15 /tmp/test/test.txt
    # 增加eternityz组
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# groupadd eternityz
    # 改变所属组
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# chgrp eternityz /tmp/test/test.txt
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
    -rw-r--r-- 1 root eternityz 7 Nov 28 18:15 /tmp/test/test.txt
    

    umask

    解释

    命令名称:umask
    命令英文原意the user file-creation mask
    命令所在路径:shell内置命令
    执行权限:所有用户
    功能描述:显示/设置文件的缺省权限
    

    语法

    umask [-S]
        -S 以rwx形式显示新建文件缺省权限(大写的S)
    

    示例

    # 查看文件的缺省权限
    umask -S
    
    # 查看umask
    umask
    
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# umask
    0022
    
    0022中
    0   特殊权限
    022 ----w--w-
    
    # 通过所有权限777和022权限进行异或操作,得到缺省权限
    777  rwx rwx rwx
    022  --- -w- -w-
    ================
    目录 rwx r-x r-x
    文件 rwx r-- r--
    
    
    # 更改umask值,进而改变缺省权限
    umask 077
    
    # 更改umask值之后,缺省权限变为
    777  rwx rwx rwx
    077  --- rwx rwx
    ================
    目录 rwx --- ---
    文件 rw- --- ---
    
    # 以下实验符合更改缺省权限的设置
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# umask 077
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# mkdir /tmp/lyf
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -ld /tmp/lyf
    drwx------ 2 root root 4096 Nov 29 10:55 /tmp/lyf
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# touch /tmp/lyf/lyf
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/lyf/lyf
    -rw------- 1 root root 0 Nov 29 10:56 /tmp/lyf/lyf
    
    

    在linux中只有root能改变文件所有者,即便是创建者都不可以

    文件的创建者为默认的所有者,此时默认的所属组也是文件创建者

    linux中文件夹的缺省权限时rwxr-xr-x,文件的缺省权限是rw-r--r--,新建文件不具备可执行权限

  • 相关阅读:
    CSS开发中常用技巧总结
    Linq的分组功能
    深入理解 C# 协变和逆变
    js数组删除数组元素!
    关于 Photoshop 蒙版和 Alpha 通道
    jQuery数组处理详解(含实例演示)
    多媒体指令(灰度像素最大值)
    多媒体指令(图像均值模糊)
    matlab练习程序(立体相关块匹配)
    matlab练习程序(steerable filters)
  • 原文地址:https://www.cnblogs.com/eternityz/p/12377490.html
Copyright © 2011-2022 走看看