美文网首页
四、文件权限(一)

四、文件权限(一)

作者: 胖虎喜欢小红 | 来源:发表于2020-01-24 14:53 被阅读0次

    1.基本权限 UGO

    文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件

    image-20191101152351943.png
    权限对象:
    属主------->u
    属组------->g
    其他人------>o
    
    基本权限类型:
    读(read):r   ---->4
    写(write):w  ---->2
    执行: x(exec) ---->1
    

    案例:

    r w x        rw-        r--       alice   hr    file1.txt
    属主权限    属组权限   其他人权限      属主   属组      文件
    
    前提条件:jack属于hr组
    一  alice对file1.txt文件有什么权限?
    二  jack对file1.txt文件有什么权限?
              a. jack是所有者吗?
              b. jack属于hr组吗?
    三 tom对file1.txt文件有什么权限? 
              a. tom是所有者吗?
              b. tom属于hr组吗?
              c. tom为其他人吗?
    

    1.1.设置权限
    chown : Linux中用来改变某个文件的属主和属组的命令,如漫画中所示,将某个“资源”(门)的访问权限给予别人。
    chmod : Linux中用来改变某个文件的访问模式的命令,如漫画中所示,chmod 777会将“大门”敞开,谁都可以进出了。

    image-20191101170920817.png
    更改文件的属主(所有者)、属组 (所属组)
    chown:
    [root@biudefor ~]# chown alice.hr file1.txt  //修改属主、属组
    [root@biudefor ~]# chown tom file1.txt      //修改属主
    [root@biudefor ~]# chown .it file1.txt       //只改属组
    [root@biudefor ~]# chown -R alice.hr dir1    //递归修改
    
                         # chown 拥有者.所属组 目标   //修改属主、属组
                         #       -R                  //递归
    

    1.2.更改权限

    系统默认目录755 文件644

    a. 使用符号

    image-20191101170709941.png
    [root@biudefor ~]# chmod u+x file1.txt     //属主增加执行
    [root@biudefor ~]# chmod a=rwx file1.txt   //所有人等于读写执行
    [root@biudefor ~]# chmod a=- file1.txt     //所有人都没有权限
    [root@biudefor ~]# chmod ug=rw,o=r file1.txt  //属主属组等于读写,其他人只读
                         #       -R                   //递归修改
    [root@biudefor ~]# ll
    -rw-rw-r--. 1 tom   it      0 Nov  1 15:30 file1.txt
    

    b.使用数字

    [root@biudefor~]# chmod 644 file1.txt 
    [root@biudefor ~]# ll file1.txt 
    -rw-r--r--. 1 tom it 0 Nov  1 15:30 file1.txt
    

    2 .权限案例 UGO

    2.1.设置权限案例

    针对hr部门的访问目录/home/hr设置权限,要求如下:

    1. root用户和hr组的员工可以读、写、执行
    2. 其他用户没有任何权限
    [root@biudefor ~]# groupadd hr   //创建一个用户组
    [root@biudefor ~]# useradd hr01 -G hr   //创建hr01用户添加到hr组里
    [root@biudefor ~]# useradd hr02 -G hr   //创建hr02用户添加到hr组里
    [root@biudefor ~]# mkdir /home/hr       //在/home创建一个hr目录
    [root@biudefor ~]# chown .hr /home/hr   //将/home/hr目录的所属组设置为hr
    [root@biudefor ~]# chmod 770 /home/hr   //将/home/hr目录的权限设置为770
    [root@biudefor ~]# ll -d /home/hr       //查看/home/hr目录本身的权限
    drwxrwx---. 2 root hr 6 Nov  1 17:11 /home/hr
    

    r、w、x权限对文件和目录的意义

    image-20191103151911043.png
    对文件:                        对目录:
    r----cat                        r----ls
    w----vi, vim                    w----touch ,rm
    x----bash /dir/file             x----cd
    

    2.2.rwx对文件的影响

    实战案例1:rwx对文件的影响

    [root@biudefor ~]# vim /home/file1
    date
    [root@biudefor ~]# ll /home/file1 
    -rw-r--r--. 1 root root 5 Nov  3 15:19 /home/file1
    
    [root@biudefor ~]# su - alice  #切换普通用户
    [alice@biudefor ~]$ cat /home/file1 
    date
    [alice@biudefor ~]$ /home/file1   #执行文件
    -bash: /home/file1: Permission denied
    [alice@biudefor ~]$ exit
    logout
    [root@biudefor ~]# chmod o+x /home/file1
    [alice@biudefor ~]$ /home/file1 
    Sun Nov  3 15:26:21 CST 2019
    
    [root@biudefor ~]# chmod o+w /home/file1
    [alice@biudefor ~]$ vim /home/file1
    date
    123
    ls
    

    2.3.rwx对目录的影响

    实战案例2:对目录没有w,对文件有rwx

    [root@biudefor ~]# mkdir /dir10
    [root@biudefor ~]# touch /dir10/file1
    [root@biudefor ~]# chmod 777 /dir10/file1 
    [root@biudefor ~]# ll -d /dir10/
    drwxr-xr-x. 2 root root 19 Nov  3 15:37 /dir10/
    [root@biudefor~]# ll /dir10/file1 
    -rwxrwxrwx. 1 root root 0 Nov  3 15:37 /dir10/file1
    [root@biudefor ~]# vim /dir10/file1
    jack
    [root@biudefor ~]# su - alice
    Last login: Sun Nov  3 15:28:06 CST 2019 on pts/0
    [alice@biudefor ~]$ cat /dir10/file1 
    jack
    [alice@biudefor ~]$ rm -rf /dir10/file1   #权限不够
    rm: cannot remove ‘/dir10/file1’: Permission denied
    [alice@biudefor ~]$ touch /dir10/file2   #权限不够
    touch: cannot touch ‘/dir10/file2’: Permission denied
    

    实战案例3:对目录有w,对文件没有任何权限

    [root@biudefor ~]# chmod 777 /dir10/
    [root@biudefor ~]# chmod 000 /dir10/file1 
    [root@biudefor ~]# ll -d /dir10/
    drwxrwxrwx. 2 root root 19 Nov  3 15:38 /dir10/
    [root@biudefor ~]# ll /dir10/file1 
    ----------. 1 root root 5 Nov  3 15:38 /dir10/file1
    [root@biudefor ~]# su - alice   #切换普通用户
    Last login: Sun Nov  3 15:38:53 CST 2019 on pts/0
    [alice@biudefor ~]$ cat /dir10/file1 
    cat: /dir10/file1: Permission denied    #没有权限
    [alice@biudefor ~]$ rm -rf /dir10/file1 
    [alice@biudefor ~]$ touch /dir10/file2
    

    小结
    对目录有w权限,可以在目录中创建新文件,可以删除目录中的文件(跟文件权限无关)
    注意事项
    文件: x 权限小心给予
    目录: w 权限小心给予

    3. 基本权限ACL

    文件权限管理之: ACL设置基本权限(r、w、x) 。
    UGO设置基本权限: 只能针对一个用户,一个组和其他人 。
    设置方法:

    [root@biudefor ~]# touch /home/test.txt
    [root@biudefor ~]# ll /home/test.txt 
    -rw-r--r--. 1 root root 0 Nov  3 15:53 /home/test.txt 
    [root@biudefor ~]# getfacl /home/test.txt 
    getfacl: Removing leading '/' from absolute path names
    # file: home/test.txt
    # owner: root
    # group: root
    user::rw-     #哪个用户
    group::r--    #哪个组
    other::r--    #其他人
    
    [root@biudefor ~]# useradd jack
    [root@biudefor ~]# setfacl -m u:alice:rw /home/test.txt   //增加用户alice权限
    [root@biudefor ~]# setfacl -m u:jack:- /home/test.txt     //增加用户jack权限
    [root@biudefor ~]# setfacl -m g:hr:r /home/test.txt       //增加组的权限
    [root@biudefor ~]# setfacl -m o::rw /home/test.txt
    
    -m, --modify  modify the current ACL(s) of file(s)
    

    3.2.查看/删除:

    [root@biudefor ~]# ll /home/test.txt 
    -rw-rw-rw-+ 1 root root 4 Nov  3 15:58 /home/test.txt
    [root@biudefor ~]# getfacl /home/test.txt 
    getfacl: Removing leading '/' from absolute path names
    # file: home/test.txt
    # owner: root
    # group: root
    user::rw-
    user:alice:rw- 
    user:jack:---
    group::r--
    group:hr:r--
    mask::rw-
    other::rw-
    [root@biudefor ~]# setfacl -x u:alice /home/test.txt
    [root@biudefor ~]# setfacl -x g:hr /home/test.txt
     -x:--remove
    [root@biudefor ~]# getfacl /home/test.txt 
    getfacl: Removing leading '/' from absolute path names
    # file: home/test.txt
    # owner: root
    # group: root
    user::rw-
    user:jack:---
    group::r--
    mask::r--
    other::rw-
    [root@biudefor ~]# setfacl -b /home/test.txt  #取消所有的facl权限
    -b, --remove-all
    

    例子

    1.创建用户tom,jack,zhuzhuxia;zhuzhuxia用户的附加组为tom
    # useradd tom
    # useradd jack
    # useradd zhuzhuxia -G tom
    2.切换到用户tom,在/tmp目录下创建文件tom.txt (观察文件的归属和权限)
    # su - tom 
    $ touch /tmp/tom.txt
    3.将文件权限修改为rw-r-----
    # chmod 640 /tmp/tom.txt
    4.向文件中写入内容"Tihs is tom file ,dont touch"
    # vim /tmp/tom.txt
    # i  this is tom file, don't touch
    5.请问: jack,zhuzhuxia能不能查看文件的内容?如果不能,通过设置facl实现jack访问?
    # jack不行 zhuzhuxia可以
    # setfacl -m u:jack:r /tmp/tom.txt
    6.jack,zhuzhuxia能不能修改文件的内容?如果不能,需要怎么操作才能实现?
    # 不能   chmod 666 /tmp/tom.txt
    7.使用root用户创建目录/prov,修改属主为tom,属组为tom,权限为750
    # mkdir /prov
    # chown tom.tom /prov
    # chmod 750 /prov
    8.将/tmp下的tom.txt移动到/prov下,tom和zhuzhuxia还能看到此文件内容吗? 
    # 可以
    9.root用户将/prov权限为751
    # chmod 751 /prov
    10.jack和zhuzhuxia用户能不能删除tom.txt文件?为什么
    # 不能,没有w权限
    11.列出/home/下面的所有文件包括隐藏文件?
    # ls -a
    12.查看自己的ip地址
    # ip a
    13.chown是用来做什么的?chmod是用来做什么的?
    # 更改目录或文件的拥有者和所属组,更改文件或目录的rwx权限
    14.将/var/log/secure 拷贝到/opt/目录下
    # cp /var/log/secure /opt
    

    相关文章

      网友评论

          本文标题:四、文件权限(一)

          本文链接:https://www.haomeiwen.com/subject/robcthtx.html