用戶組和權限管理2

十七、linux文件系統上的特殊權限

    suid:s表示,sgid:s表示,sticky:t表示

    

    安全上下文

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

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

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

        進程訪問文件是的權限,取決于進程的發起者

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

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

            應用文件其他權限

    可執行文件上suid權限

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

        啟動為進程之后,其進程的屬主為原程序文件的屬主

        suid只對二進制可執行程序有效

        suid設置在目錄上無意義

            chmod u+s file    

            chmod u-s file

[root@localhost testdir]# ll
total 0
-rwxrwxrwx 1 root wang 0 Aug  4 03:33 file1
-rwxrwxrwx 1 root wang 0 Aug  4 03:35 file2
[root@localhost testdir]# chmod u+s file1
[root@localhost testdir]# ll
total 0
-rwsrwxrwx 1 root wang 0 Aug  4 03:33 file1
-rwxrwxrwx 1 root wang 0 Aug  4 03:35 file2
[root@localhost testdir]# chmod u-s file1
[root@localhost testdir]# ll
total 0
-rwxrwxrwx 1 root wang 0 Aug  4 03:33 file1
-rwxrwxrwx 1 root wang 0 Aug  4 03:35 file2

    可執行文件上sgid權限

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

        啟動文件為進程之后,其進程的屬主為源程序文件的屬組

            chmod g+s file

            chmod g-s file

[root@localhost testdir]# chmod g+s file1
[root@localhost testdir]# ll
total 0
-rwxrwsrwx 1 root wang 0 Aug  4 03:33 file1
-rwxrwxrwx 1 root wang 0 Aug  4 03:35 file2
[root@localhost testdir]# chmod g-s file1
[root@localhost testdir]# ll
total 0
-rwxrwxrwx 1 root wang 0 Aug  4 03:33 file1
-rwxrwxrwx 1 root wang 0 Aug  4 03:35 file2

    目錄上的sgid權限

        默認情況下,用戶創建文件是,其屬組為此用戶所屬的主組

        一旦某目錄被設定了sgid,則對此目錄有寫權限的和用戶在此目錄中創建的文件所屬的組為此目錄的屬組

        通常用于創建一個協作目錄

                chmod g+s file

            chmod g-s file

[root@localhost testdir]# mkdir 1234
[root@localhost testdir]# ll -a
total 8
drwxr-xr-x   3 1000 user1   41 Aug  4 06:56 .
dr-xr-xr-x. 18 root root  4096 Aug  4 00:17 ..
drwxr-xr-x   2 root root     6 Aug  4 06:56 1234
-rw-r--r--+  1 root root     0 Aug  4 04:49 file1
-rwxrwxrwx   1 root wang     4 Aug  4 04:32 file2
[root@localhost testdir]# chmod o+w 1234
[root@localhost testdir]# useradd 1
[root@localhost testdir]# su - 1
[1@localhost ~]$ cd /testdir/1234
[1@localhost 1234]$ touch file1
[1@localhost 1234]$ ll file1
-rw-rw-r-- 1 1 1 0 Aug  4 06:58 file1
[1@localhost 1234]$ exit
logout
[root@localhost testdir]# chmod g+s 1234
[root@localhost testdir]# su - 1
Last login: Thu Aug  4 06:57:23 CST 2016 on pts/1
[1@localhost ~]$ cd /testdir/1234
[1@localhost 1234]$ touch file2
[1@localhost 1234]$ ll
total 0
-rw-rw-r-- 1 1 1    0 Aug  4 06:58 file1
-rw-rw-r-- 1 1 root 0 Aug  4 06:58 file2

    suicky位

        具有寫權限的用戶通常用戶可以刪除該目錄中的任何文件,無論該文件的權限或擁有權

        在目錄設置sticky位,只有文件的所有者或root可以刪除該文件

        sticky設置在文件上無意義

            chmod o+s file

            chmod o-s file

    特殊權限數字法

        suid:chmod 4777 file

        sgid:chmod 2777 file

        sticky:chmod 1777 dir

    權限為映射

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

            s:屬主擁有x權限

            S:屬主沒有x權限

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

            s:group屬組擁有x權限

            S:group沒有x權限

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

            t:other擁有x權限

            T:other沒有x權限

    設置文件特定屬性

        chattr option file

            +A:鎖定訪問時間,訪問不刷新時間

            +i:鎖定文件不能刪除,改名,更改

            +a:鎖定文件,能看,能修改,不能改名,不能刪除

            lsattr 顯示特定屬性

        作用:避免誤操作,誤刪除。

[root@localhost testdir]# chattr +i file2
[root@localhost testdir]# rm -rf file2
rm: cannot remove ‘file2’: Operation not permitted
[root@localhost testdir]# echo "xxx" > file2
-bash: file2: Permission denied
[root@localhost testdir]# lsattr file2
----i----------- file2

十八、訪問控制列表

    acl:access control list。實現靈活的權限管理

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

        Centos7默認創建的xfs和ext4文件系統有acl功能

        Centos7之前版本,默認手工創建的ext4文件系統無acl功能

    acl生效順序:所有者,自定義用戶,自定義組,其他人

        setfacl option (d)(ug):username:權限 file

                u:用戶

                g:組

                d:在這個目錄下新建的文件有acl權限

            -m:修改acl權限

            -x:刪除acl權限

            -M:調用腳本修改acl權限

            -X:調用腳本刪除acl權限

useradd wangwenjie
[root@localhost testdir]# touch file1
[root@localhost testdir]# setfacl -m g:wangwenjie:rwx file1
[root@localhost testdir]# getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r--
group:wangwenjie:rwx
mask::rwx
other::r--

[root@localhost testdir]# setfacl -x g:wangwenjie file1
[root@localhost testdir]# getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--

        setfacl -k dir:刪除默認acl權限

        setfacl -b file:清除所有acl權限

[root@localhost testdir]# ll
total 0
-rw-r--r--+ 1 root root 0 Aug  4 07:10 file1
[root@localhost testdir]# setfacl -b file1
[root@localhost testdir]# ll
total 0
-rw-r--r-- 1 root root 0 Aug  4 07:10 file1

        getfacl:查看特殊權限

        getfacl file1 | setfacl –set-file=-file2  復制file的acl權限給file2

[root@localhost testdir]# setfacl -m g:wangwenjie:rwx file1
[root@localhost testdir]# getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r--
group:wangwenjie:rwx
mask::rwx
other::r--

[root@localhost testdir]# touch file2
[root@localhost testdir]# getfacl file1 | setfacl --set-file=- file2
[root@localhost testdir]# getfacl file2
# file: file2
# owner: root
# group: root
user::rw-
group::r--
group:wangwenjie:rwx
mask::rwx
other::r--

    

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

        默認acl權限給x,文件也不會x權限

        bash cal 不能刪除

    mask只影響除所有者和other的之外的人和組的最大權限

    nask需要與用戶的權限進行邏輯與運算后,才能變成有限的權限

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

            setfacl -m mask::權限 file

        –set選項會把所有的acl想都刪除,用新的代替,需要注意的是一定要包含ugo的設置,不能像-m一樣只是添加acl就可以

            setfacl –set u::權限,g::權限,o::權限 file

    備份和恢復acl

        主要的文件操作命令cp和mv都支持acl,只是cp命令需要加上-p參數,但是tar等常見的備份工具是不會保留目錄和文件的acl信息。

            cp -p 保留權限

            getfacl -R 目錄 > file1     把目錄里的acl權限備份到file1文件里

            setfacl -R -b 目錄     刪除目錄的所有acl權限

            setfacl -R –set-file=file1 目錄     通過file1文件把目錄的acl權限還原

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

(0)
DYWDYW
上一篇 2016-08-04 21:45
下一篇 2016-08-04 22:34

相關推薦

  • N25-第二周博客作業

    第二周博客作業 1.Linux上的文件管理類命令都有哪些,其常用的使用方法和演示 2.bash的工作特性之命令執行狀態返回值和命令行展開所涉及內容演示及其實例 3.請使用命令行展開功能完成以下練習;    (1)創建/tmp的目錄下a_c,a_d,b_c,b_d     (2) 創建/tmp/mylinux目錄下的 4…

    Linux干貨 2016-12-11
  • shell腳本語言中的選擇執行

    shell腳本語言中的選擇執行 概述 程序執行過程分為順序執行、選擇執行和循環執行。順序執行是指程序按照步驟一步一步地運行。選擇執行是指程序根據特定條件選擇兩項或者多項中的一項運行。循環執行是指程序根據特定條件重復執行直到某個節點結束,繼續運行其他步驟。本篇文章從判斷條件和條件判斷式簡要說明shell腳本語言中程序選擇執行的用法。 shell腳本中的判斷條件…

    Linux干貨 2017-04-17
  • N25_第五周

    1、顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行;grep -E "^[[:space:]]+" /boot/grub2/grub.cfg 2、顯示/etc/rc.d/rc.sysinit文件中以#開頭,后面跟至少一個空白字符,而后又有至少一個非空白字符的行;grep -E "^#[[:space:]…

    Linux干貨 2017-01-08
  • 數組

    一.數組 變量:存儲單個元素的內存空間 數組:存儲多個元素的連續的內存空間,相當于多個變量的集合。 索引:編號從0 開始,屬于數值索引  注:索引可支持使用自定義的格式,而不僅是數值格式,即為關聯索引,bash4.0 版本之后開始支持,bash 的數組支持稀疏格式(索引不連續) 二.數組的聲明 declare -a ARRAY_NAME decla…

    Linux干貨 2016-11-23
  • sed命令初識

    ? ? ? ?sed是一種流編輯器,它是文本處理中非常常用的工具,能夠配合正則表達式使用。處理時,把當前處理的行存儲在臨時緩沖區中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區中的內容,處理完成后,把緩沖區的內容送往屏幕。接著處理下一行,這樣不斷重復,直到文件末尾。文件內容并沒有 改變,除非你使用重定向存儲輸出。Sed主要用來自…

    Linux干貨 2017-12-03
  • LAMP+logzilla+sphinx+syslog-ng實現集中日志管理(第一版)[原創]

    一、前言        目前查看系統日志比較被動,遇到系統不正常或故障時才會主動去檢查服務器系統日志,這樣一來不能及時了解系統的運行情況,因此部署Logzilla+sphine+syslog-ng來彌補這不足。以下為安裝、部署平臺詳細步驟。(Logzilla是什么新東西?其實前身就是php-syslog-ng,引用作者…

    Linux干貨 2015-03-27

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-08-07 23:53

    文章的層次結構清晰明了,內容豐滿,有理論有實踐。

欧美性久久久久