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

 

     3、chown命令:用來設置文件的屬主和屬組:

 

               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

      

         5、umask值:

       

                 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

 

      6、Linux文件系統上的特殊權限:

 

           任何一個可執行程序文件能不能啟動為進程:取決于發起者對程序文件是否擁有執行權限;

 

               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權限;

 

   7、sticky 粘滯位:

 

       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.www58058.com/29340

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

相關推薦

  • Haproxy+Keepalived+Varnish+LAMP+Memcacked+NFS 實現WordPress站點的動靜分離

    詳細說明,請點擊CSDN博客的連接地址:http://blog.csdn.net/yhy1271927580/article/details/70195158

    2017-04-16
  • 作業-第二周

    1、linux常用文件管理命令 ls rm chmod touch mv cp 2、echo $? 如果返回0則成功,其他失敗 3、 mkdir /tmp/{a,b}_{c,d} mkdir -p /tmp/mylinux/bin /tmp/mylinux/boot/ /tmp/mylinux/boot/grup /tmp/mylinux/dev \ /tm…

    Linux干貨 2016-06-23
  • N22-第十一周作業

    1、詳細描述一次加密通訊的過程,結合圖示最佳。 (1)數字簽名 A與B通信,B發給A一段數據,為了證明數據確實是B發送過來的,B首先會用單向加密算法從數據中提取一段特征碼,然后用自己的私鑰加密這段特征碼和原始數據后,發送給A;A接受到數據,首先用B的公鑰解密,獲取到特征碼和原始數據;然后用同樣的單向加密算法從原始數據中提取一段特征碼,與之前用公鑰解密得到的特…

    Linux干貨 2016-11-01
  • N22-第七周作業

    1、創建一個10G分區,并格式為ext4文件系統;    (1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;    (2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳; [root@localhost ~]# fd…

    Linux干貨 2016-10-07
  • Tomcat集群之會話保持

    一:環境介紹:      OS:CentOS7.3      Ngninx:1.10.2     Tomcat:7     注:便于實驗,此處關閉全部服務器的防火墻,selinux(iptables -F  于  setenforce 0)…

    2017-05-25
  • 運維面試題, 不知是否正確的答案

    1、簡述TCP三次握手四次揮手過程及各過程中客戶端和服務器端的狀態。 握手: client 發送請求SYN到 server; 狀態:server;初始狀態為LISTEN,client 發送SYN后變為SYN_SENT server 發送ACK回應,并發送SYN請求到 client;狀態:服務器收到SYN后,變為SYN_RCVD,發送ACK+SYN后,變為ES…

    Linux干貨 2016-06-23

評論列表(1條)

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

    寫的很簡潔清晰,總體來說還可以。

欧美性久久久久