Linux文件权限及ACL

1、文件权限:

 

       文件的权限主要针对三类对象进行定义:

 

             owner:属主,u表示

             group :属组,g表示

             Other:其他, o表示

   

       每个文件或目录针对每类访问者都定义了三种权限:

 

         文件:

             r:可以使用文件查看类工具查看文件内容;

             w:可以修改其内容;

             x:执行该文件(二进制文件);

 

          目录:

              r:可以查看目录内容(显示子目录、文件列表);

              W:修改文件内容(在目录中新建、移动、删除文件或子目录);

              X:可以使用ls  -l 查看此目录中的文件列表,可以cd进入此目录;

 

           注:x只给目录x权限,不给文件x权限;

 

       文件权限对应表:

        blob.png

 

2、chmod命令:修改文件权限

 

          chmod [OPTION]… MODE[,MODE]… FILE…

 

                  -R:递归修改权限;

 

          修改一类用户的所有权限:

   

            u=  g=  o=  ug=  a=  u=  g=

 

          修改一类用户某位或某些位权限:

 

            u+  u-  g+  g-   o+  o-   a+  a-  + –

 

            例:

    

              chmod  u+wx,g-r,o=rx /testdir/file2

 

            文件权限对应的八进制数字:

 

            r=4  w=2  0=1

 

          例:

             chmod    755  /bin/cat

 

             chmod  a=rwX  /testdir

          

                   rwX

                       对于文件不会增加x执行权限(前提是文件本身没有x执行权限);

                       对于目录会增加x执行权限;

 

           参考其他文件来设置权限:

           chmod [OPTION]… –reference=RFILE FILE…

 

                    -R:递归创建属主属组

           例:

              chmod –reference  /etc/passwd  /testdir

 

     3chown命令:用来设置文件的属主和属组:

 

               chown [OPTION]… [OWNER][:[GROUP]] FILE…

 

               例:

                 chown  rootsysadmins  /testdir/file1

 

                 chown   -R   root  /testdir/

 

     4chgrp命令:修改文件的属组

 

                 chgrp [OPTION]… GROUP FILE…

 

                        -R:递归修改文件的属组;

 

                例:

                  chgrp     user1    /testdir/file

 

                  chgrp  -R  user1   /testdir  

      

                 chgrp [OPTION]… –reference=RFILE FILE…

 

                 例:

                   chgrp    –reference  /etc/passwd  /testdir

      

         5umask值:

       

                 umask值可以用来保留创建文件默认权限;

 

                 新建文件权限:666–umask,如果所得结果某位存在执行(奇数)权限,则将其权限+1

 

             例:

                666-127=54-1+1=640

 

                 新建目录权限:777-umask

 

             例:

                777-127=650

 

                 umask :查看系统默认umask码;

                 umask :为系统临时设置umask码;

                 umask  -s :模仿的rwx的形式显示出umask码;

                 umask  -p :输出的umask码可以被调用;

                 umask  -p  >  /etc/bashrc    

                 umask  -p  >  ~/.bashrc

 

      6Linux文件系统上的特殊权限:

 

           任何一个可执行程序文件能不能启动为进程:取决于发起者对程序文件是否拥有执行权限;

 

               suid sgid所对应的八进制数字:

 

                   suid=4   sgid=2

 

               suid :只对二进制程序文件有效;设置在目录上无效;

  

              对二进制文件设定suid

                 

                   chmod  u+s  /etc/cat

                           或

                   chmod  4755  /etc/cat

      

              取消对二进制文件的suid

 

                    chmod  u-s   /etc/cat

 

 

            sgid:通常用于创建一个协作目录,一旦某目录被设定了sgid,则对此目录有写权限的用户在此目录中创建的文件所属的组为此目录的属组;

 

            对目录设定sgid

 

                  chmod   g+s   dir

 

             对目录取消sgid

 

                  chmod   g-s   dir

 

          例:

 

            [root@centos6 testdir]# chmod g+s /testdir/

            [root@centos6 testdir]# chown :it  /testdir/

            [root@centos6 testdir]# ls -ld /testdir/

            drwxrwsrwx+ 2 root it 4096 Aug  4 14:37 /testdir/

            [root@centos6 testdir]# touch aa bb cc

            [root@centos6 testdir]# ll

            total 12

            -rw-rw-r–+ 1 root it 0 Aug  4 14:43 aa

            -rw-rw-r–+ 1 root it 0 Aug  4 14:43 bb

            -rw-rw-r–+ 1 root it 0 Aug  4 14:43 cc

            [root@centos6 testdir]#

 

        suid权限映射位:

 

              s:属主拥有x权限;

              S:属主没有x权限;

 

         guid权限映射位:

 

               sgroup拥有x权限;

               Sgroup没有x权限;

 

          sticky权限位映射

 

               tother拥有x权限;

               Tother没有x权限;

 

   7sticky 粘滞位:

 

       Sticky粘滞位:只对目录有效;设置在文件上无意义;

 

       在目录中设置sticky位以后,只有文件的所有者或root可以删除文件;其他用户只能查看文件内容;

 

      注:具有写权限的目录通常用户可以删除该目录中的任何文件,无论该文件的权限或拥有者;

 

       Sticky位对应的八进制数字:

 

        添加sticky=1  取消sticky=0

 

       为目录设置sticky位:

            

             chmod  o+t  dir     或   chmod  1777  dir

 

       例:

      

         [root@centos6 testdir]# chmod o+t /testdir/ chmod 1777 /testdir

         [root@centos6 testdir]# ll -d /testdir/

         drwxrwsrwt+ 2 root it 4096 Aug  4 14:51 /testdir/

 

        为目录取消sticky位权限:

 

             chmod  o-t  dir     或   chmod  0777  dir

 

          例:

   

            [root@centos6 testdir]# chmod o-t /testdir/

            [root@centos6 testdir]# ls -ld /testdir/

            drwxrwsrwx+ 2 root it 4096 Aug  4 14:51 /testdir/

            [root@centos6 testdir]#

 

8、设定文件特定属性:

 

            chattr命令:用于改变文件的属性

 

             chattr  [options]   file

        

                        +i:锁定文件或目录,不能删除更名修改文件;

                        -i :取消对文件的锁定;可以删除更名修改文件;

                        +A:锁定文件的访问时间改变;

                        -A:取消对文件访问时间的锁定;

                        +a:只能向文件中添加数据,而不能删除,多用于服务器日志文件,/var/log/messages

 

               例:

 

                  chattr  +i -ifile 示例:

 

                  [root@localhost ~]# chattr +i user

                  [root@localhost ~]# lsattr user

                  —-i———– user

                  [root@localhost ~]#

                  [root@localhost ~]# rm -rf user

                  rm: 无法删除"user": 不允许的操作

                  [root@localhost ~]# mv user aa

                  mv: 无法将"user" 移动至"aa": 不允许的操作

                  [root@localhost ~]# echo aaaa  >> user

                  bash: user: 权限不够

                  [root@localhost ~]#chattr  -i user

 

                  

               chattr  +a-afile 示例:

 

                  [root@localhost ~]# chattr +a user

                  [root@localhost ~]# lsattr user

                  —–a———- user

                 [root@localhost ~]# rm -rf user

                 rm: 无法删除"user": 不允许的操作

                 [root@localhost ~]# mv user  bb

                 mv: 无法将"user" 移动至"bb": 不允许的操作

                 [root@localhost ~]# echo nihao  >> user

                 [root@localhost ~]# cat user

                 nihao

                 [root@localhost ~]# chattr  -a user

 

           lsattr命令:查看文件特定的属性

 

                lsattr  file

 

                例:

                  [root@localhost ~]# lsattr user

                  —–a———- user

                  [root@localhost ~]#

 

   9ACL访问控制列表:

 

          ACL:实现灵活的权限管理,除了文件的所有者,所属组和其他人,可以对更多的用户设置权限;

 

         centos7,默认创建的xfsext4文件系统有ACL功能;

 

         Centos7,之前的版本,默认手工创建的ext4文件系统无ACL功能,需手动创建:

 

         tune2fs  -o acl  /dev/sdb1

      

         mount  -o  acl /dev/sdb1   /mnt

 

        ACL生效顺序:所有者、自定义用户、自定义组,其他人

 

         设置setfacl访问控制:

 

             setfacl  -m  uzhengrw  user (设置单个用户setfacl

 

             setfacl  -m  gitrw  user (设置组setfacl

 

                     -m:创建setfacl;   

                     -x:删除setfacl

                     -b:清空setfacl控制;

                     -M:批量为文件设置setfacl访问控制权限;

                     -X:批量删除文件的setfacl访问控制权限;

                     -Rm:对目录递归设置setfacl访问控制;

                     -k:删除默认的acl权限;

 

         查看setfacl访问控制属性:

 

             getfacl      file | dir

 

       给单个用户设置setfacl访问控制:

 

         [root@localhost testdir]# setfacl  -m u:zheng:rw user

         [root@localhost testdir]# getfacl user

         # file: user

         # owner: root

         # group: root

         user::rw-

         user:zheng:rw-

         group::r–

         mask::rw-

         other::r–

         [root@localhost testdir]#

 

   给组设置setfacl访问控制:

 

         [root@localhost testdir]# setfacl -m g:it:rwx user

         [root@localhost testdir]# getfacl user

         # file: user

         # owner: root

         # group: it

         user::rw-

         user:zheng:rw-

         group::r–

         group:it:rwx

         mask::rwx

         other::r–

         [root@localhost testdir]#

 

  删除单个用户的setfacl访问控制权限:

 

        [root@localhost testdir]# setfacl -x u:zheng user

        [root@localhost testdir]# getfacl user

        # file: user

        # owner: root

        # group: it

        user::rw-

        group::r–

        group:it:rwx

        mask::rwx

        other::r–

        [root@localhost testdir]#

 

   删除组的setfacl访问控制权限:

 

       root@localhost testdir]#setfacl  -x  git  user

 

   彻底清空文件的setfacl访问控制:

 

        root@localhost testdir]# setfacl  -b  user  

        [root@localhost testdir]# getfacl user

        # file: user

        # owner: root

        # group: it

        user::rw-

        group::r–

        other::r–

        [root@localhost testdir]# ll

        总用量 4

        -rw-r–r–. 1 root it 3 8月   4 19:37 user

        [root@localhost testdir]#

 

   批量设置文件的setfacl访问控制:

 

       vim编辑acl.txt文件

 

       [root@localhost testdir]# vim acl.txt

       uzhengrwx

       gitrw   

       保存此文件;

       [root@localhost testdir]# setfacl -M acl.txt user

       [root@localhost testdir]# getfacl user

       # file: user

       # owner: root

       # group: it

       user::rw-

       user:zheng:rwx

       group::r–

       group:it:rw-

       mask::rwx

       other::r–

       [root@localhost testdir]#   

 

批量删除文件的setfacl访问控制权限:

 

    vim编辑acl2.txt文件

    [root@localhost testdir]# vim acl2.txt

    [root@localhost testdir]# setfacl -X acl2.txt user

        [root@localhost testdir]# getfacl user

        # file: user

        # owner: root

        # group: it

        user::rw-

        group::r–

        group:it:rw-

        mask::rw-

        other::r–

        [root@localhost testdir]#

 

  setfacl访问控制列表中,用户或组的设置必须存在于mask权限设定范围内才会生效;

  mask只影响除所有者和other以外的人和组的最大权限;只能等于或小于mask的权限,                         不能超过mask的权限;

 

       设定mask权限:

 

          [root@localhost testdir]# setfacl -m mask::rwx user

          [root@localhost testdir]# getfacl user

          # file: user

          # owner: root

          # group: it

          user::rw-

          group::r–

          group:it:rw-

          mask::rwx

          other::r–

          [root@localhost testdir]#

 

设置目录默认setfacl访问控制权限:

 

         [root@localhost testdir]# setfacl -m d:u:zheng:rwx dir

                                      d:(default)表示对目录设置递归的setfacl访问控制权限;在该目录下新创建的文件也默认继承在目录中设置的setfacl访问控制权限;

 

        删除目录默认的setfacl访问控制权限;

 

             [root@localhost testdir]# setfacl -k dir

 

复制file1文件的setfacl权限给分file2

  

     getfacl  file1 | setfacl  –set-file=-  file2

 

    [root@bogon testdir]# getfacl user | setfacl –set-file=- user1

    [root@bogon testdir]# getfacl user1

    # file: user1

    # owner: root

    # group: root

    user::rw-

    user:zheng:rw-

    group::r–

    mask::rw-

    other::r–

    [root@bogon testdir]#

 

ACL备份和恢复:

 

    主要的文件操作命令cpmv都支持ACL,只是cp命令需要加上 -p选项;但是tar等常见的备份压缩工具是不会保留目录和文件的ACL信息。

 

    备份/testdir目录下的ACL访问控制属性到/root/acl.bak

 

       [root@bogon testdir]# getfacl -R * >> /root/acl.bak

 

     清空/testdir目录下的所有ACL访问控制列表:

 

       [root@bogon testdir]# setfacl -R -b *

           

        使用ll命令查看/testdir/目录下的子目录和文件,在权限位最后已没有了“+”号;

 

           [root@bogon testdir]# ll

           总用量 0

           drwxr-xr-x. 2 root root 26 8月   4 21:16 dir

           -rw-r–r–. 1 root root  0 8月   4 22:57 user

           -rw-r–r–. 1 root root  0 8月   4 22:58 user1

           [root@bogon testdir]#

 

       恢复/testdir/目录下ACL访问控制:

 

           [root@bogon testdir]# setfacl -R –set-file=/root/acl.bak ./

 

        使用ll命令查看/testdir/目录下的子目录和文件,在权限位最后已经有了“+”号;

 

           [root@bogon testdir]# ll

           总用量 12

           drw-rw-r–+ 2 root root 26 8月   4 21:16 dir

           -rw-rw-r–+ 1 root root  0 8月   4 22:57 user

           -rw-rw-r–+ 1 root root  0 8月   4 22:58 user1

           [root@bogon testdir]#

 

    

        

       

 

 

 

 

 

 

 

原创文章,作者:zhengyibo,如若转载,请注明出处:http://www.178linux.com/29340

(0)
zhengyibozhengyibo
上一篇 2016-08-05 10:17
下一篇 2016-08-05 10:17

相关推荐

  • 马哥教育网络班22期+第三周课程练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。     [root@zabbix ~]# who|awk '{print $1}'|sort| uniq 2、取出最后登录到当前系统的用户的相关信息。 &nb…

    Linux干货 2016-08-23
  • 内部命令与外部命令——Linux基本命令(3)

    1.     内部命令与外部命令 内部命令:由shell自带的,而且通过某命令形式提供 内部命令在系统启动时就调入内存,是常驻内存的,所以执行效率高。 外部命令:在文件系统路径下有对应的可执行程序文件 外部命令是系统的软件功能,用户需要时才从硬盘中读入内存。 2.     命令类…

    2017-07-13
  • LVM——如何让你的磁盘空间可大可小

    逻辑卷管理器(LVM) 允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小 允许在多个物理设备间重新组织文件系统          将设备指定为物理卷          用一个或者多个物理卷来创…

    Linux干货 2016-08-29
  • 09yum的使用以及简单配置

    YUM: yellowdog update modifier ,rpm的前端程序,用来解决软件包相关依赖性,可以在多个库之间定位软件包。 yum repository:yum repo,存储了众多RPM包,以及包相关的元数据文件,放置于特定目录repodata下。 yum 访问的文件服务器主要有三种,ftp,http,file。 yum客户端配置文件: 【/…

    Linux干货 2016-11-04
  • 系统基础之文件管理工具

    系统基础之文件管理工具   linux的重要哲学思想之一,一切皆文件.那作为系统管理员,就要求对文件的操作管理特别熟悉.那么下面介绍的一个工具可以帮助到大家,更有效,快捷的完成对文件的处理.下面让我们来认识以下的工具. 文本工具: 文件内容:   cat: 复制标准输入到标准输出     选项:   &nbs…

    Linux干货 2016-08-07
  • 文件系统挂载

    文件系统挂载 文件系统管理: 将额外文件系统与根文件系统某现存的目录建立起关联关系,进而使得此目录做为其它文件访问入口的行为称之为挂载;  解除此关联关系的过程称之为卸载;  把设备关联挂载点:Mount Point mount  卸载时:可使用设备,也可以使用挂载点 umount  注意:挂载点下原有文件在挂载完成后…

    Linux干货 2015-12-16

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-05 11:34

    写的很简洁清晰,总体来说还可以。