文件權限管理

文件權限

QQ截圖20160803144418.png

 

            [root@CentOS6 ~]# ll 3.txt

            -rwxrw-rw-. 3 root root 16 Jul 27 10:15 3.txt

            前3位:定義屬主(user)的權限

            中3位:屬組的(group)權限

            后3位:不是屬主也不是屬組的其他人(other)的權限

文件權限

            文件:

                        r: 可使用文件查看類工具獲取其內容

                        w: 可修改其內容

                        x: 可以把此文件提請內核啟動為一個進程

            目錄:

                        r: 可以使用ls 查看此目錄中文件列表

                        w: 可在此目錄中創建文件,也可刪除此目錄中的文件

                        x: 可以使用ls -l 查看此目錄中文件列表,可以cd進入此目錄

                        X:只給目錄x權限,不給文件x

                                    常用于-R遞歸給目錄及其子目錄時防止給予文件執行權限

權限管理命令

            chmod        chown        chgrp

chmod:改變文件的mode權限 change file mode bits

            mode權限的4類用戶:u(uesr)    g(group)    o(other)    a(all)(默認可不寫)

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

                        MODE表示法:

                                    1.賦權表示法:直接操作一類用戶的所有權限rwx:

                                    u=rw  給user用戶賦予讀寫權限

                                    o=rx  給other用戶賦予讀執行權限

                                    g= 取消group組的用戶所有權限

                                    2.授權表示法:直接操作一類用戶的一種權限,但也可以操作多種權限

                                    u+x  給user用戶賦予執行權限

                                    g+rw  給user用戶賦予讀寫權限

                                    o-x 給other用戶取消讀執行權限

                                            [root@CentOS6 testdir]# touch txt

                                            [root@CentOS6 testdir]# ll txt

                                            -rw-r–r–. 1 root root 0 Aug  3 09:24 txt

                                            [root@CentOS6 testdir]# chmod g= txt

                                            [root@CentOS6 testdir]# ll txt

                                            -rw—-r–. 1 root root 0 Aug  3 09:24 txt

                                            [root@CentOS6 testdir]# chmod g=rw txt

                                            [root@CentOS6 testdir]# ll txt

                                            -rw-rw-r–. 1 root root 0 Aug  3 09:24 txt

                                            [root@CentOS6 testdir]# chmod g-w txt

                                            [root@CentOS6 testdir]# ll txt

                                            -rw-r–r–. 1 root root 0 Aug  3 09:24 txt

        

                                            區別:a+w , +w , a-w , -w 的區別

                                            [root@CentOS6 testdir]# touch f1 f2

                                            [root@CentOS6 testdir]# ll f1 f2

                                            -rw-r–r–. 1 root root 0 Aug  3 09:29 f1

                                            -rw-r–r–. 1 root root 0 Aug  3 09:29 f2

                                            [root@CentOS6 testdir]# chmod a+w f1

                                            [root@CentOS6 testdir]# chmod +w f2

                                            [root@CentOS6 testdir]# ll f1 f2

                                            -rw-rw-rw-. 1 root root 0 Aug  3 09:29 f1

                                            -rw-r–r–. 1 root root 0 Aug  3 09:29 f2

                                            [root@CentOS6 testdir]# chmod a+w f2  給f2都賦予w權限以便觀察

                                            [root@CentOS6 testdir]# ll f1 f2

                                            -rw-rw-rw-. 1 root root 0 Aug  3 09:29 f1

                                            -rw-rw-rw-. 1 root root 0 Aug  3 09:29 f2

                                            [root@CentOS6 testdir]# chmod a-w f1

                                            [root@CentOS6 testdir]# chmod -w f2

                                            chmod: f2: new permissions are r–rw-rw-, not r–r–r–  重要的提示信息

                                            [root@CentOS6 testdir]# ll f1 f2

                                            -r–r–r–. 1 root root 0 Aug  3 09:29 f1

                                            -r–rw-rw-. 1 root root 0 Aug  3 09:29 f2

                        注意:對w權限,a+w給所有人賦予權限,+w只會給user用戶w權限,而rx權限無此影響,a可寫,可不寫

            chmod [OPTION]… OCTAL-MODE FILE…

                        八進制表示法:

                                    —    000 0

                                    –x    001 1

                                    -w-    010 2

                                    -wx    011 3

                                    r–    100 4

                                    r-x    101 5

                                    rw-    110 6

                                    rwx    111 7

                        例如:

                                    rw-r—–:640

                                    rwxr-xr-x: 755

                        注意:其中第一數字代表user的權限,第二個數字代表group組的權限,第三個數字代表other的權限

                        [root@CentOS6 testdir]# ll f1

                        -r–r–r–. 1 root root 0 Aug  3 09:29 f1

                        [root@CentOS6 testdir]# chmod 755 f1

                        [root@CentOS6 testdir]# ll f1

                        -rwxr-xr-x. 1 root root 0 Aug  3 09:29 f1

            chmod [OPTION]… –reference=RFILE FILE…

                        參考RFILE文件的權限,將FILE文件的權限修改為同RFILE文件

                        [root@CentOS6 testdir]# ll /var/mail/gao f1

                        -rwxr-xr-x. 1 root root    0 Aug  3 09:29 f1

                        -rw-rw—-. 1 gao  mail 5304 Jul 29 17:58 /var/mail/gao

                        [root@CentOS6 testdir]# chmod –reference=/var/mail/gao f1

                        [root@CentOS6 testdir]# ll /var/mail/gao f1

                        -rw-rw—-. 1 root root    0 Aug  3 09:29 f1

                        -rw-rw—-. 1 gao  mail 5304 Jul 29 17:58 /var/mail/gao

            R,–recursive:遞歸的修改目錄及其目錄下文件的權限

                        -R一般和MODE表示法結合使用

            注意:用戶只能修改屬主為自己的那些文件的權限


chown:改變文件的屬主和屬組    change file owner and group

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

            chown [OPTION]… –reference=RFILE FILE…

                        參考RFILE文件的屬主屬組,將FILE文件的屬主屬組修改為同RFILE文件

                        [root@CentOS6 testdir]# ll /var/mail/gao f1

                        -rw-rw—-. 1 root root    0 Aug  3 09:29 f1

                        -rw-rw—-. 1 gao  mail 5304 Jul 29 17:58 /var/mail/gao

                        [root@CentOS6 testdir]# chown –reference=/var/mail/gao f1

                        [root@CentOS6 testdir]# !ll

                        ll /var/mail/gao f1

                        -rw-rw—-. 1 gao mail    0 Aug  3 09:29 f1

                        -rw-rw—-. 1 gao mail 5304 Jul 29 17:58 /var/mail/gao

            -R,–recursive:遞歸的修改目錄及其目錄下文件的屬主屬組

                        [root@CentOS6 testdir]# ll /testdir/gm -d

                        drwxr-xr-x. 4 root root 4096 Aug  3 10:19 /testdir/gm

                        [root@CentOS6 testdir]# ll /testdir/gm/* -d

                        drwxr-xr-x. 2 root root 4096 Aug  3 10:19 /testdir/gm/gm1

                        drwxr-xr-x. 3 root root 4096 Aug  3 10:18 /testdir/gm/hlr

                        [root@CentOS6 testdir]# chown -R gao:gentoo /testdir/gm

                        [root@CentOS6 testdir]# ll /testdir/gm -d

                        drwxr-xr-x. 4 gao gentoo 4096 Aug  3 10:19 /testdir/gm

                        [root@CentOS6 testdir]# ll /testdir/gm/* -d

                        drwxr-xr-x. 2 gao gentoo 4096 Aug  3 10:19 /testdir/gm/gm1

                        drwxr-xr-x. 3 gao gentoo 4096 Aug  3 10:18 /testdir/gm/hlr

chgrp:change group ownership

            chgrp [OPTION]… GROUP FILE…

            chgrp [OPTION]… –reference=RFILE FILE..

新建文件和目錄的默認權限

            umask:文件權限的遮罩碼,反掩碼

                        用戶新建文件或目錄的默認權限有umask決定

                        用戶新建文件的默認權限:

                                    文件權限=666-umask

                                    注意:對文件默認不能擁有執行權限,若計算出的默認權限有奇數則+1處理,偶數不處理

                                    其原理如下:

                                                                    umask=135

                                                                    666-135=531,但其運算過程如下

                                                                    666對應的二進制:110110110

                                                                    135對應的二進制:001011101

                                                    下面的0對上面無作用,    ———————

                                                    下面的1對上面的1有置0作用:110100010  ==>642

                        用戶新建目錄的默認權限:

                                    目錄權限=777-umask

                        # umask   查看當前系統用戶的umask

                        -p:顯示結果為umask number 作用:umask -p >> .bashrc 在不打開文本的前提下修改用戶的默認umask

Linux文件系統上的特殊權限

            SUID, SGID, Sticky

            三種常用權限:r, w, x user, group, other

            安全上下文

            前提:進程有屬主和屬組;文件有屬主和屬組

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

                        (2)啟動為進程之后,其進程的屬主為發起者;進程的屬組為發起者所屬的組

                        (3)進程訪問文件時的權限,取決于進程的發起者

                                    (a)進程的發起者,同文件的屬主:則應用文件屬主權限

                                    (b)進程的發起者,屬于文件屬組;則應用文件屬組權限

                                    (c)應用文件“其它”權限

SUID權限:

            SUID只作用與可執行的二進制文件上

            用戶運行該程序時,自動繼承該程序所屬主的權限

            SUID 設置在目錄上無意義

            權限設定:

                        chmod u+s FILE…

                        chmod u-s FILE…

SGID權限:

            作用在二進制的程序上:

                        用戶運行該程序時,自動繼承該程序所屬組的權限

            作用在目錄上:

                        該目錄內新建的文件或目錄的所屬組自動繼承與該文件的所屬組

            通常用于創建一個協作目錄,用于同組內的用戶,可互相修改對方文件,協同工作

            權限設定:

                        chmod g+s DIR…

                        chmod g-s DIR…

                        [root@CentOS6 testdir]# ll -d gm

                        drwxr-xr-x. 4 gao gentoo 4096 Aug  3 10:19 gm

                        [root@CentOS6 testdir]# chmod g+s gm

                        [root@CentOS6 testdir]# ll -d gm

                        drwxr-sr-x. 4 gao gentoo 4096 Aug  3 10:55 gm

                        [root@CentOS6 testdir]# cd gm

                        [root@CentOS6 gm]# mkdir fit

                        [root@CentOS6 gm]# touch r2t

                        [root@CentOS6 gm]# ll -d fit r2t

                        drwxr-sr-x. 2 root gentoo 4096 Aug  3 10:57 fit

                        -rw-r–r–. 1 root gentoo    0 Aug  3 10:57 r2t

Sticky權限:

            防止某些用戶對該目錄有權限,則胡亂刪除該目錄下的(屬主是自己或屬主不是自己)文件

            在目錄設置Sticky位,只有文件的所有者或root可以刪除該文件,即除root用戶外,其他用戶只能刪除屬主是自己的文件

            sticky  設置在文件上無意義

            權限設定:

                        chmod o+t DIR…

                        chmod o-t DIR…

特殊權限數字法

            SUID SGID STICKY

            000 0

            001 1

            010 2

            011 3

            100 4

            101 5

            110 6

            111 7

            例如:chmod 3777 /testdir/gm

            對/testdir/gm目錄給予777權限,并且對目錄設置了SGID和Sticky權限

權限位映射

            SUID: user, 占據屬主的執行權限位

                        s:  屬主擁有x 權限

                        S :屬主沒有x 權限

            SGID: group, 占據屬組的執行權限位

                        s: group 擁有x 權限

                        S :group 沒有x 權限

            Sticky: other, 占據other 的執行權限位

                        t: other 擁有x 權限

                        T :other 沒有x

設定文件特定屬性

            chattr +i file: 不能刪除,改名,更改

            chattr +a  file: 只能增加

ACL:訪問控制列表  Access Control List

            除了文件的所有者,所屬組和其它人,可以對更多的用戶設置權限

                        比如設置某一用戶能以rwx權限訪問一指定文件,而其他用戶按照u,g,o權限對文件進行訪問

            getfacl file |directory

                        查看文件或目錄的訪問控制列表

            setfacl -m u:gao:rx file|directory

                        設置gao用戶,對目錄或文件,不以u,g,o所對應的權限訪問文件,而是以rx權限訪問文件

            setfacl -M file.acl file|directory

                        以文本的形式,,對多個用戶設置某一目錄或文件的ACL

            setfacl -m d:u:gao:rx directory

                        清除指定用戶的ACL

            setfacl -b file1

                        清除文件所有ACL權限

            ACL 文件上的group 權限是mask 值(自定義用戶,自定義組,擁有組的最大權限), 而非傳統的組權限

            mask只影響除所有者和other的之外的人和組的最大權限Mask需要與用戶的權限進行邏輯與運算后,才能變成有限的權限(Effective Permission)

            用戶或組的設置必須存在于mask權限設定范圍內才會生效。

                        setfacl -m mask::rx file

            [root@CentOS6 testdir]# ll f1

            -rw-rw—-. 1 root root 19 Aug  3 14:38 f1

            [root@CentOS6 testdir]# getfacl f1

            # file: f1

            # owner: root

            # group: root

            user::rw-

            group::rw-

            other::—

            [root@CentOS6 testdir]# setfacl -m u:gao:rwx f1

            [root@CentOS6 testdir]# getfacl f1

            # file: f1

            # owner: root

            # group: root

            user::rw-

            user:gao:rwx

            group::rw-

            mask::rwx

            other::—

            [root@CentOS6 testdir]# cat f1

            lkjsdfl

            klsfj;lfjs

            [root@CentOS6 testdir]# setfacl -b f1

            [root@CentOS6 testdir]# getfacl f1

            # file: f1

            # owner: root

            # group: root

            user::rw-

            group::rw-

            other::—

原創文章,作者:megedugao,如若轉載,請注明出處:http://www.www58058.com/27830

(0)
megedugaomegedugao
上一篇 2016-08-04
下一篇 2016-08-04

相關推薦

  • linux入門第二課

    **linux入門基礎** linux 命令類別 內部命令和外部命令 首先我們可以用echo $SHELL 來查詢shell 里面會顯示我們當前在用的shell 上圖是echo $SHELL 也可以用cat /etc/shells來查詢系統里面的shell bash其實是一個文件是一個程序 存在于 /bin/bash 目錄,這是真是存在的文件,是一個可以執行…

    Linux干貨 2017-05-20
  • N25期—第四周作業

    1、 復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 cp –rf /etc/skel /home/tuser1 chmod -R go= /home/tuser1 2、 編輯/etc/group文件,添加組hado…

    Linux干貨 2016-12-26
  • centos系列初步搭建LAMP

    centos6搭建LAMP 系統環境 ip=192.168.0.105 selinux為:setenforce 0 iptables 為stop 客戶機需要修改hosts文件 1.192.168.2.105 www1.stuX.com2.192.168.2.105 www2.stuX.com 安裝LAMP組件 1.yum install …

    Linux干貨 2016-12-11
  • N25-第七周作業

    1、創建一個10G分區,并格式為ext4文件系統;~]#fdisk -l #查看已有分區 設備 Boot Start End Blocks Id System/dev/sda1 * 2048 8194047 4096000 83 Linux/dev/sda2 8194048 24578047 8192000 82 Linux swap / Solaris~]…

    Linux干貨 2017-02-24
  • 磁盤管理

    linux的哲學思想:一切皆文件; 設備類型: 塊(block):隨機訪問,交換數據單位是“塊”; 字符(character):線性訪問,數據交換單位是“字符”; 設備文件:FHS /dev     設備文件:關聯至設備驅動程序;設備的訪問入口;       &nbsp…

    Linux干貨 2016-08-30
  • 馬哥教育第20期一班第一周博客

    Linux入門 1、Linux的哲學思想 一切皆文件(硬件設備亦表現為文件) 由眾多目的單一的小程序組成;一個程序只做一件事,并且要做好 組合小程序完成復雜任務 盡可能避免跟用戶交互 使用文本文件保存配置信息 提供機制,而非策略 2、常用命令 id命令 簡介:   id – print real a…

    Linux干貨 2016-07-26
欧美性久久久久