用戶組管理系列(二):權限設置

1、權限簡介

   操作系統中每個文件都擁有特定的權限、所屬用戶和所屬組。權限是操作系統用來限制資源訪問的機制,在Linux中權限一般分為讀(readable)、寫(writable)和執行(excutable),分為三組。分別對應文件的屬主(owner),屬組(group)和其他用戶(other),通過這樣的機制來限制哪些用戶、哪些組可以對特定的文件進行什么樣的操作。

用戶組管理系列(二):權限設置

2、文件和目錄權限的區別

   對文件和目錄而言,讀寫執行表示不同的意義

    對文件

r     可以使用cat查看文件的內容
w     可以修改文件的內容
x     可以將其運行為二進制文件

    對目錄

r     可以查看目錄下列表
w     可以創建和刪除目錄下文件
x     可以使用cd進入目錄

    注:文件的x權限一般關閉,防止可執行的二進制病毒文件自動運行

        目錄的x權限一般開啟,否則r和w權限將失去意義

3、權限的控制

    <1>修改文件的屬主屬組

        chown [OPTION]… FILE…       

          -R:遞歸修改文件的屬主屬組

        chgrp [OPTION]… FILE… 

[root@centos7 testdir]# touch f1 f2
[root@centos7 testdir]# ll
total 0
-rw-r--r--. 1 root root 0 Aug  4 13:22 f1
-rw-r--r--. 1 root root 0 Aug  4 13:22 f2
[root@centos7 testdir]# chown zhao:zhao f1
[root@centos7 testdir]# chown :zhao f2
[root@centos7 testdir]# touch f3
[root@centos7 testdir]# chgrp zhao f3
[root@centos7 testdir]# ll
total 0
-rw-r--r--. 1 zhao zhao 0 Aug  4 13:22 f1
-rw-r--r--. 1 root zhao 0 Aug  4 13:22 f2
-rw-r--r--. 1 root zhao 0 Aug  4 13:23 f3

<2>修改文件的權限屬性

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

         -R:遞歸修改文件的權限

         下面是三種修改權限的方法:

         賦權表示法:操作一類用戶的所有權限位

         授權表示法:操作一類用戶的一個權限位

        數字表示法:用數字的形式表示權限

[root@centos7 testdir]# touch f1
[root@centos7 testdir]# ll f1
-rw-r--r--. 1 root root 0 Aug  4 14:13 f1
[root@centos7 testdir]# chmod o=rw f1
[root@centos7 testdir]# ll f1
-rw-r--rw-. 1 root root 0 Aug  4 14:13 f1
[root@centos7 testdir]# chmod o+x f1
[root@centos7 testdir]# ll f1
-rw-r--rwx. 1 root root 0 Aug  4 14:13 f1
[root@centos7 testdir]# chmod 644 f1
[root@centos7 testdir]# ll f1
-rw-r--r--. 1 root root 0 Aug  4 14:13 f1

      chmod [OPTION]… –reference=RFILE FILE…

[root@centos7 testdir]# ll
total 0
-rw-r--r--. 1 root root 0 Aug  4 14:13 f1
-rw-rw-r--. 1 root root 0 Aug  4 14:18 f2
[root@centos7 testdir]# chmod --reference=f1 f2
[root@centos7 testdir]# ll
total 0
-rw-r--r--. 1 root root 0 Aug  4 14:13 f1
-rw-r--r--. 1 root root 0 Aug  4 14:18 f2
[root@centos7 testdir]#

     chgrp [GROUPNAME]…

           修改文件的數組

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

           修改文件的屬主和屬組

       chattr [FILE]

           設置文件的特定屬性

           -i:不能刪除,改名,修改文件

           -a:只能增加

         lsattr [FILE]

           顯示特定屬性

[root@centos7 testdir]# chattr +i f1
[root@centos7 testdir]# rm -f f1
rm: cannot remove ‘f1’: Operation not permitted
[root@centos7 testdir]# echo f1 > f1
-bash: f1: Permission denied
[root@centos7 testdir]# mv f1 f2
mv: cannot move ‘f1’ to ‘f2’: Operation not permitted
[root@centos7 testdir]# chattr -i f1
[root@centos7 testdir]# chattr +a f1
[root@centos7 testdir]# lsattr f1
-----a---------- f1
[root@centos7 testdir]# echo f1 > f1
-bash: f1: Operation not permitted
[root@centos7 testdir]# echo f1 >> f1
[root@centos7 testdir]# cat f1
f1

3、三種特殊權限及其意義

   在談特殊權限之前,我們先需要了解一下安全上下文

    安全上下文:計算機上的二進制文件一旦運行為程序,必定是以某個用戶的身份在運行。如果此身份與某個被訪問的資源屬主一致,則應用資源屬主權限;與屬組一致,應用屬組權限;與其他用戶一致,應用其他用戶權限。換言之,用戶的訪問權限,取決于發起此進程的用戶。但是SUID、SGID這倆類特殊權限將與上述觀點不一致。用戶運行某程序時,如果程序擁有SUID權限,那么,此進程運行為程序時,用戶將應用程序的屬主權限去訪問文件。SGID同理。

三種權限的應用范圍:

SUID 作用在二進制文件上
SGID 作用在二進制文件上
SGID 也可以作用在目錄上
SBIT 作用在目錄上

下面詳細分析三種權限的應用

    <1>SUID作用:應用擁有SUID權限的二進制的屬主權限

[root@centos7 testdir]# ll /etc/shadow
-r--------. 1 root root 3933 Aug  4 18:34 /etc/shadow
[zhao@centos7 testdir]$ ll /bin/nano
-rwxr-xr-x. 1 root root 205904 Jun 10  2014 /bin/nano
[root@centos7 testdir]# ll /bin/nano
-rwsr-xr-x. 1 root root 205904 Jun 10  2014 /bin/nano
[root@centos7 testdir]# su zhao
[zhao@centos7 testdir]$ nano /etc/shadow
qi:!!:17017:0:99999:7::: ---->將倆個感嘆號去掉,取消鎖定--->qi:!!:17017:0:99999:7:::
[zhao@centos7 testdir]$ su qi
[qi@centos7 testdir]$

 注:危險操作,切勿模仿   

    <2>SGID作用在二進制文件上,作用:應用擁有SGID權限的二進制的屬主權限,同SUID,不舉例。

    <3>SBIT作用在目錄上時:如果用戶對目錄有寫權限,對目錄下的文件有讀權限,雖然不能修改別人的文件,卻可以創建自己的文件和刪除別人的文件。此時SBIT可以避免刪除別人的文件

       示例:在/testdir下,要求自己可以創建和查看自己的文件,能查看別人的文件,但不能刪除和修改別人的文件

[root@centos7 testdir]# su user1
[user1@centos7 /testdir]$ touch f1
[user1@centos7 /testdir]$ chmod g-w f1
[user1@centos7 /testdir]$ su 
Password: 
[root@centos7 testdir]# su user2
[user2@centos7 /testdir]$ touch f2
[user2@centos7 /testdir]$ chmod g-w f2
[user2@centos7 /testdir]$ ll f1 f2
-rw-r--r--. 1 user1 user1 0 Aug  4 20:08 f1
-rw-r--r--. 1 user2 user2 0 Aug  4 20:09 f2
[user2@centos7 /testdir]$ cat f1
[user2@centos7 /testdir]$ su
Password: 
[root@centos7 testdir]# chmod o+t .
[root@centos7 testdir]# ll -d .
drwxrwxrwt. 2 root it 24 Aug  4 20:09 .
[root@centos7 testdir]# su user1
[user1@centos7 /testdir]$ echo content > f2
f2: Permission denied.
[user1@centos7 /testdir]$ rm -f f2
rm: cannot remove ‘f2’: Operation not permitted

     *SGID作用在目錄上時,在此目錄下創建文件時,屬組將自動屬于目錄的屬組

     示例:在/testdir下,創建一個組,要求組內的某些用戶自己可以創建查看修改自己的文件,也能查看修改組內其他人的文件,不屬于此組的用戶對文件無任何權限。

    步驟:root添加用戶和組g1

          用戶創建自己的文件并且把屬組改為g1

          root修改目錄的屬組和對其他用戶的訪問權限并且將屬組置為SGID權限

[root@centos7 testdir]# useradd user1
[root@centos7 testdir]# useradd user2
[root@centos7 testdir]# groupadd g1
[root@centos7 testdir]# gpasswd -a user1 g1
Adding user user1 to group g1
[root@centos7 testdir]# gpasswd -a user2 g1
Adding user user2 to group g1
[root@centos7 testdir]# su user1
[user1@centos7 testdir]$ touch f1
[user1@centos7 testdir]$ chgrp g1 f1
[user1@centos7 testdir]$ su
Password: 
[root@centos7 testdir]# su user2
[user2@centos7 testdir]$ touch f2
[user2@centos7 testdir]$ chgrp g1 f2
[user2@centos7 testdir]$ su
Password: 
[root@centos7 testdir]# ll f1 f2
-rw-rw-r--. 1 user1 g1 0 Aug  4 20:44 f1
-rw-rw-r--. 1 user2 g1 0 Aug  4 20:45 f2
[root@centos7 testdir]# chgrp g1 .
[root@centos7 testdir]# chmod o= .
[root@centos7 testdir]# chmod g+s .
[root@centos7 testdir]# ll -d .
drwxrws---. 2 root g1 24 Aug  4 20:45 .

4、ACL應用

    ACL權限控制主要目的是提供傳統的owner,group,other的read,wirte,execute權限之外的具體權限設置,可以針對單一用戶或組來設置特定的權限。

        <1>查看acl權限

            getfacl FILENAME

        <2>設置acl權限

            語法格式

                setfacl [OPTION] [FILENAME|DIRECTORY]

            常用選項

-m     添加acl規則
-x     刪除acl規則
-M      從文件中讀取acl規則
-X     從文件中刪除acl規則
-m d     設置默認acl規則,執針對目錄有效
-k     清除默認acl規則
-b     清除所有的acl規則
-R     遞歸設置acl規則

      <3>備份和恢復acl

         第一步:設置目錄及目錄下文件的acl

[root@centos7 dir]# setfacl -m u:user1:r f1
[root@centos7 dir]# setfacl -m g:user2:r-x .
[root@centos7 dir]# getfacl -R .
# file: .
# owner: root
# group: g1
# flags: -s-
user::rwx
group::r-x
group:user2:r-x
mask::r-x
other::r-x

# file: f1
# owner: root
# group: g1
user::rw-
user:user1:r--
group::r--
mask::r--
other::r--

         第二步:備份這些acl至特定文件

[root@centos7 dir]# getfacl -R . > /tmp/acl.txt

         第三步:清除原有的目錄及文件的acl

[root@centos7 dir]# setfacl -b -R .
[root@centos7 dir]# getfacl -R .
# file: .
# owner: root
# group: g1
# flags: -s-
user::rwx
group::r-x
other::r-x

# file: f1
# owner: root
# group: g1
user::rw-
group::r--
other::r--

         第四步:還原acl

[root@centos7 dir]# setfacl -R --set-file=/tmp/acl.txt .
[root@centos7 dir]# getfacl -R .
# file: .
# owner: root
# group: g1
# flags: -s-
user::rw-
user:user1:r--
group::r--
group:user2:r-x			#effective:r--
mask::r--
other::r--

# file: f1
# owner: root
# group: g1
user::rw-
user:user1:r--
group::r--
group:user2:r-x			#effective:r--
mask::r--
other::r--

         

      

                 

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

(0)
mfwingmfwing
上一篇 2016-08-05 10:18
下一篇 2016-08-05 10:18

相關推薦

  • 推薦-使用iptables作為網絡防火墻構建安全的網絡環境

    使用iptables作為網絡防火墻構建安全的網絡環境 使用iptables作為網絡防火墻構建安全的網絡環境 前言 網絡防火墻的優勢 實驗拓撲圖 實驗環境 實驗步驟 防火墻未設置前對所有服務器的測試 針對不同服務器進行”非法”訪問 定義網絡防火墻規則 再次針對不同服務器進行”非法”訪問 測試服務器是否可訪問 總結 前言 一般情況下iptables只作為主機防火…

    Linux干貨 2016-03-31
  • Nginx相關實戰案例

    Nginx相關實戰案例: Nginx在實際生產中極為重要,先來看一下Nginx配置文件nginx.conf中文詳解 #定義Nginx運行的用戶和用戶組 user www www; #nginx進程數,建議設置為等于CPU總核心數。 worker_processes 8; #全局錯誤日志定義類型,[ debug | info | notice | warn |…

    Linux干貨 2017-06-19
  • 網絡yum源的配置

    1.準備: 光盤 包 元數據 2.網絡服務http(s)  ftp 查看是否安裝  which  vsftp;  ls  /misc/cd/Packages | grep vsftpd 安裝  rpm  -ivh  /misc/cd/Packages/vsftpd… …

    2017-06-13
  • 第二周

    3、(1)、  :  ~]# mkdir -p /tmp/{a_c,a_d,b_c,b_d}                  or     ~]# …

    Linux干貨 2016-08-22
  • 排錯

    把/etc/inittab  模式改為6模式 怎么修復 1 先把 vim /etc/inittab 打開 2 把/etc/inittab 模式改為6 3 reboot 4 在倒計時完之前按任意鍵 5按A進入 6 在quiet  命令后面寫入 3  模式 重啟 7把 vim /etc/inittab 打開 8 把/etc…

    Linux干貨 2017-05-15
  • 登錄后經常出現You have new mail in /var/spool/mail/root的提示

    安裝完LINUX后經常使用終端遠程登錄,登錄后經常出現You have new mail in /var/spool/mail/root的提示,很是煩人。 這東西到底是做什么用的呢?經過查詢才知道這是LINUX的郵年提示功能。LINUX會定時查看LINUX各種狀態做匯總,每經過一段時間會把匯總的信息發送的root的郵箱里,以供有需之時查看。 那要怎么去掉這麻…

    系統運維 2017-08-05

評論列表(1條)

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

    文章的層次結構清晰明了,如果能多一些自己對操做部分的理解與總結就更好了。

欧美性久久久久