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

      

         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

 

      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 ~]#

 

   9、ACL訪問控制列表:

 

          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 10:17
下一篇 2016-08-05 10:17

相關推薦

  • NFS

    NFS初探 NFS概述 NFS工作流程 NFS Server端設置 NFS工具與命令 NFS配置實例 NFS概述 NFS是什么? NFS,network file system(網絡文件系統)的縮寫,是一種實現文件共享的實現方式。 NFS的功能是什么 NFS基于RPC協議,實現遠程系統調用,共享文件資源。 NFS工作流程 RPC,Remote Pr…

    Linux干貨 2016-04-28
  • linux常見文件管理類命令

    1、文件管理類命令主要有mkdir、rmdir、cp、mv、rm等 mkdir:make directory創建文件夾 -p:自動按需創建父目錄 -v:顯示創建過程 -m:創建時給定文件夾權限 創建層級文件夾時,需保證上一級目錄存在,否則會報錯,例如 ? 創建層級文件夾時,可以加上-p選項,遞歸創建不存在的上級目錄,例如 ? rmdir:刪除文件夾 -p:刪…

    2017-12-10
  • http和apache服務器

    超文本傳輸協議(HTTP,HyperText Transfer Protocol)是互聯網上應用最為廣泛的一種網絡協議。所有的WWW文件都必須遵守這個標準。設計HTTP最初的目的是為了提供一種發布和接收HTML頁面的方法。 http/1.1 :1997年1月 引入了持久連接(persistent connection) , tcp連接默認不關閉,可以被多個請…

    2017-12-05
  • Liunx課前準備

    ? ? ? ? 經過與家人的溝通終于來到了北京,開始了期待已久的Linux學習之路。 ? ?今天是講課前第一天,和上學時代一樣,各位同學做了自我介紹,仿佛又置身于10年前的課堂,同學們有序的介紹著自己,今天我們坐到了一起就為了同一個夢想。之前還有很大的顧慮:學不會怎么辦?出來找不到工作怎么辦?……但聽了大家的介紹后發現很多同學與我一樣,所有的顧慮瞬間消失,?!?/p>

    2018-03-26
  • FTP基于PAM和MySQL/MariaDB實現虛擬用戶訪問控制

    前言 vsftpd是一款在Linux發行版中最受推崇的FTP服務器程序,特點是小巧輕快,安全易用,目前在開源操作系統中常用的FTP套件主要有proftpd、pureftp、ServU和wu-ftpd等。本文將講解vsftpd的基本功能和如何基于PAM和MySQL/MariaDB實現虛擬用戶訪問控制。 基礎配置介紹 工作原理 狀態響應碼 1xx:信息碼 2xx…

    2015-04-20
  • linux文件系統創建

    件系統管理 格式化:低級格式化(分區之前,劃分磁道)         高級格式化:在分區之后進行,創建文件系統         元數據(也是放在塊上(block)):inode  &nbsp…

    Linux干貨 2016-08-29

評論列表(1條)

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

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

欧美性久久久久