系統權限及用戶受權命令(11)

命令(6個)node

chmod,chown,chgrp,setfacl,getfacl,umask
vim


11.1.chmodbash

功能:改變文件權限ide

這一命令有符號模式和絕對模式測試


符號模式:ui

語法:chmod [who] operator [permission] filenamespa


who的含義是:orm

u 文件屬主權限。server

g 屬組用戶權限。繼承

o 其餘用戶權限。

a 全部用戶(文件屬主、屬組用戶及其餘用戶)。


operator的含義:

+ 增長權限。

- 取消權限。

= 設定權限。


permission的含義:

r 讀權限。

w 寫權限。

x 執行權限。

s 文件屬主和組set-ID。

t 粘性位*。

l 給文件加鎖,使其餘用戶沒法訪問。

u,g,o 針對文件屬主、屬組用戶及其餘用戶的操做。


示例:

[root@localhost scripts]# chmod a+x test.txt   #添加全部的執行權限
[root@localhost scripts]# ll test.txt
-rwxr-xr-x 1 root root 276 Jun 26 18:39 test.txt
[root@localhost scripts]# chmod a-x test.txt   #收回全部命令的執行權限
[root@localhost scripts]# ll test.txt
-rw-r--r-- 1 root root 276 Jun 26 18:39 test.txt
[root@localhost scripts]# chmod og-w test.txt  #收回屬組用戶和其餘用戶的寫權限
[root@localhost scripts]# ll test.txt
-rw-r--r-- 1 root root 276 Jun 26 18:39 test.txt
[root@localhost scripts]# chmod g+w test.txt #賦予屬組用戶寫權限
[root@localhost scripts]# chmod u+x test.txt  #賦予文件屬主執行權限
[root@localhost scripts]# chmod go+x test.txt  #賦予屬組用戶和其餘用戶執行權限


11.2.chown

功能:變動文件或目錄的擁有者或所屬羣組。

語法:chomd -R -h owner file

經常使用選項:

 -c或--changes           效果相似"-v"參數,但僅回報更改的部分。

 -f或--quite或--silent  不顯示錯誤信息。

 -h或--no-dereference  之對符號鏈接的文件做修改,而不更動其餘任何相關文件。

 -R或--recursive  遞歸處理,將指定目錄下的全部文件及子目錄一併處理。

 -v或--version  顯示指令執行過程。

 --dereference  效果和"-h"參數相同。

 --help  在線幫助。

 --reference=<參考文件或目錄>  把指定文件或目錄的擁有者與所屬羣組所有設成和參考文件或目 錄的擁有者與所屬羣組相同。

示例:

[root@localhost scripts]# ll test.txt
-rwxrwxr-x 1 root root 276 Jun 26 18:39 test.txt
[root@localhost scripts]# chown user001 test.txt
[root@localhost scripts]# ll test.txt
-rwxrwxr-x 1 user001 root 276 Jun 26 18:39 test.txt


11.3chgrp

功能:變動文件或目錄的所屬羣組

語法:chgrp [-cfhRv][--help][--version][所屬羣組][文件或目錄...]

經常使用選項:

 -c或--changes  效果相似"-v"參數,但僅回報更改的部分。

 -f或--quiet或--silent  不顯示錯誤信息。

 -h或--no-dereference  只對符號鏈接的文件做修改,而不更動其餘任何相關文件。

 -R或--recursive  遞歸處理,將指定目錄下的全部文件及子目錄一併處理。

  --reference=<參考文件或目錄>

示例:

[root@localhost scripts]# ll test.txt
-rwxrwxr-x 1 user001 root 276 Jun 26 18:39 test.txt
[root@localhost scripts]# chgrp group1 test.txt
[root@localhost scripts]# ll test.txt
-rwxrwxr-x 1 user001 group1 276 Jun 26 18:39 test.txt


11.4.umask

功能:遮罩權限


root用戶:

默認建立目錄的權限:755

默認建立文件的權限:644

普通用戶:

默認建立目錄的權限:775

默認建立文件的權限:664

 

umask 查看當前用戶的默認權限

root:

umask

0022

普通用戶:

uamsk

0002

 

目錄的最大權限:777

文件的最大權限:666

root:

目錄的默認權限=0777-umask=0777-0022=0755

文件的默認權限=0666-umask=0666-0022=0644

普通用戶:

目錄的默認權限=0777-umask=0777-0002=0775

文件的默認權限=0666-umask=0666-0002=0664

 

更改用戶的默認權限:
臨時更改:
umask 0002 只在當前終端生效
永久生效:
vim ~/.bashrc  當前用戶家目錄裏的文件(局部變量)
..
umask 0002
 
# source ~/.bashrc  馬上立刻讀取文件內容(讓其立刻生效)
# . ~/.bashrc
 
vim /etc/bashrc 針對全部用戶(全局)
...
umask 0066
 
. ~/.bashrc
說明:
當局部變量和全局變量衝突,以局部爲準

練習:

一、建立一個文件test,要求它實現user001只讀,user002,user003可讀可寫,user004,user005可讀可執行

[root@localhost scripts]# usermod -g group1  user002
[root@localhost scripts]# usermod -g group1  user003
[root@localhost scripts]# id user002
uid=601(user002) gid=10015(group1) groups=10015(group1)
[root@localhost scripts]# id user003
uid=602(user003) gid=10015(group1) groups=10015(group1)
[root@localhost scripts]# touch test
[root@localhost scripts]# ll test
-rw-r--r-- 1 root root 78184 Jun 27 14:06 test
[root@localhost scripts]# chown user001:group1 test
[root@localhost scripts]# ll test
-rw-r--r-- 1 user001 group1 0 Jun 27 14:28 test
[root@localhost scripts]# chmod u-w,g+w,o+x test
[root@localhost scripts]# ll test
-r--rw-r-x 1 user001 group1 0 Jun 27 14:28 test

2.什麼狀況下才能刪除一個文件或目錄?

答:跟這個文件或目錄自己的權限無關,跟你對它們的上一級目錄是否有寫權限和執行全有關。你對一個目錄有寫權限和執行權限,你就能夠刪除這個目錄裏任何文件


例子

[root@localhost ~]# ll -d test  #可讀可執行
drwxr-xr-x 2 root root 4096 Dec 21 18:54 test
[root@localhost ~]# chown .group1 test
[root@localhost ~]# id user002
uid=601(user002) gid=10015(group1) groups=10015(group1)
[user002@localhost home]$ ll test
total 0
-rw-r--r-- 1 root group1 0 Dec 21 18:58 123
user002@localhost test]$ rm -f 123  
rm: cannot remove `123': Permission denied
說明:可讀可執行是沒法刪除

[root@localhost ~]# ll -d /home/test  #增長w,去掉x權限
drwxrw-r-x 3 root group1 4096 Dec 21 18:58 /home/test
[user002@localhost home]$ cd test #無執行權限,是進不了文件,進不了文件夾,怎麼刪除文件
-bash: cd: test: Permission denied
[user002@localhost home]$ rm -f test/123
rm: cannot remove `test/123': Permission denied

[root@localhost ~]# ll -d /home/test  #去掉r,保留wx
drwx-wxr-x 3 root group1 4096 Dec 21 19:17 /home/test
[user002@localhost test]$ ll
ls: cannot open directory .: Permission denied
[user002@localhost test]$ rm -f 123
說明:沒有隻讀權限,可是用戶有wx,就能進入文件夾,刪除文件


3.請問下面的狀況,我一個普通用戶可否刪除/test/abc這個子目錄

# ll -d /test
drwxr-xrwx. 3 root root 12288 Jul 16 17:42 /test
# ll /test
drwxr-xr-x. 2 root root 4096 Jul 16 17:40 abc
[user001@localhost ~]$ rm -rf /home/test/abc
答案:能夠刪除,由於普通如今不屬於屬主,也不屬於屬組,那就只能屬於其餘人了;而test權限的o位是7的權限,可寫可執行,就能刪掉abc

 

四、什麼狀況下修改了一個文件才須要強制保存,什麼狀況下可以成功強制保存?

解答:

狀況1:目錄有可讀可寫權限,文件只有只讀權限(普通用戶)

[root@localhost ~]# ll -d /home/test

drwxrw--wx 3 root group1 4096 Dec 22 16:20 /home/test

[root@localhost ~]# ll  /home/test

total 0

-rw-rw-r-- 1 root group1 0 Dec 21 19:24 123

[user002@localhost ~]$ vim /home/test/123

"/home/test/123" E212: Can't open file for writing  #不能寫也不能強制保存


狀況2:目錄有可讀可寫權限,文件可讀可寫(普通用戶)

[root@localhost ~]# ll -d /home/test

drwxrw--wx 3 root group1 4096 Dec 22 16:20 /home/test

[root@localhost ~]# ll  /home/test

total 0

-rw-rw-r-- 1 root group1 0 Dec 21 19:24 123

[user002@localhost ~]$ vim /home/test/123  #仍是不能強制保存


狀況3:目錄有可讀可寫權限,文件可讀可寫可執行(普通用戶)

[root@localhost ~]# ll -d /home/test

drwxrw--wx 3 root group1 4096 Dec 22 16:20 /home/test

[root@localhost ~]# ll  /home/test

total 4

-rwxrw-rwx 1 root group1 4 Dec 22 17:04 123

[user002@localhost ~]$ cat /home/test/123  

my

說明:這種狀況不須要強制就能保存


狀況4:目錄有可讀可寫權限,文件可寫可執行(普通用戶)

[root@localhost ~]# ll  /home/test

total 4

-rwxrw--wx 1 root group1 3 Dec 22 17:10 123

說明:不須要強制就能保存


狀況3:目錄有可讀可寫可執行權限,文件可讀(普通用戶)

[root@localhost ~]# ll -d /home/test

drwxrwx-wx 3 root group1 4096 Dec 22 16:20 /home/test

[root@localhost ~]# ll  /home/test

total 0

-rw-r--r-- 1 root group1 0 Dec 21 19:24 123

[user002@localhost ~]$ vim /home/test/123 

[user002@localhost ~]$ cat /home/test/123

yyl

說明:對一個文件有隻讀權限,對目錄有讀寫執行權限,強制保存沒問題,而且保存後,文件屬主屬組都變了當前用戶


狀況4:



答案:若是你對一個文件沒有寫權限,那麼修改後,要保存就會出現強制保存的選項;

     雖然你對這個文件沒有寫權限,但若是你對文件所在的上一級目錄有寫權限,則能夠強制保存成功,而且保存後,把文件的owner和group屬性改爲了你本身

     若是對上一級目錄沒有寫權限,則不能夠強制保存;但也有一個例外,就是若是這個文件的owner就是你,那麼你對它沒有寫權限,對它上級目錄也沒寫權限,也能夠強制保存成功


11.5.特殊權限(s,t)

s位叫特權位,英文的叫法有setuid,setgid,suid,sgid等


1)冒險位(setuid):4000 u+s 臨時擁有文件擁有者的權限,做用在屬主身上,通常針對命令。皇帝臨時給一個用戶一個尚方寶劍.只針對前三位

對目錄能夠加,可是無效,由於目錄不是命令,根本不能執行它

 

demo:

# chmod u+s /usr/bin/vim

# ll /usr/bin/vim

-rwsr-xr-x. 1 root root 1967072 Feb 17  2012 /usr/bin/vim

# chmod 0766 /usr/bin/vim

[root@node1 Desktop]# ll /usr/bin/vim

-rwxrw-rw-. 1 root root 1967072 Feb 17  2012 /usr/bin/vim

 

小實驗:

1,測試passwd命令的s位,去掉其s位,普通用戶就不能本身改本身密碼了,加上s位就能夠了

 

2,/bin/touch這個命令是沒有s位的,你能夠嘗試加上s位,而後用普通用戶touch文件,觀察效果。作完測試後,記得改回來也就是把s位去掉

 

3,普通用戶能關機嗎?怎麼讓普通用戶關機

 

能夠對關機命令加s位來讓普通用戶能關機,在rhel6下測試reboot,shutdown能夠這樣作,init不能這樣作

這只是一個理論討論,實際狀況不會這麼作的


 

2)強制位(setgid):2000 g+s 針對目錄,任何人在該目錄下建立的文件或目錄強制繼承父目錄的屬組。強制繼承家族企業。中間三位

demo:

chmod g+s dir1

chmod 2755 dir1

測試驗證

# ll -d

drwxr-sr-x. 5 u01 sysadmin 4096 Apr  6 11:24 .


3)粘制位 :1000 o+t 針對公共目錄,在該目錄下的文件只有root和文件的擁有者能夠刪除,其餘人不能刪除。本身管理本身

demo:

# chmod 1777 dir2

drwxrwxrwt. 2 root root     4096 Apr  6 10:33 dir2

 

# ll -d /tmp

drwxrwxrwt. 24 root root 4096 Apr  6 11:16 /tmp

 

s位和t位注意的地方:

 

用數字表示

 

前三位上加s    4

中間三位加s    2

後三位加t        1

 

 

chmod 1777 /tmp

 

s位和t位大小寫的區別 

        小寫的話就表示 有x執行權限

        大寫的話就表示 沒有x執行權限 


設置文件的特殊屬性

chattr +i 不能刪除,更名,修改 chattr -i

chattr +a 只能追加內容

lsattr 顯示特定屬性



11.6.acl訪問控制策略

ACL:Access Control List ,實現靈活的權限管理

除了文件的全部者,所屬組和其它人,能夠對更多的用戶設置權限

CentOS7 默認建立的xfs和ext4文件系統具備ACL功能

CentOS7 以前版本,默認手工建立的ext4文件系統無ACL功能,需手動增長

tune2fs –o acl /dev/sdb1

mount –o acl /dev/sdb1 /mnt/test

ACL生效順序:全部者,自定義用戶,自定義組,其餘人


setfacl:

-R:遞歸受權,對目錄下已經存在的文件有acl策略,可是新建的文件沒有acl策略

-d:遞歸受權,對目錄下新建的文件有acl策略,老文件沒有

-m:修改acl策略

-x:刪除某個用戶的權限

-b:刪除全部的acl策略

-mask:設置除了其餘人和擁有者之外的最大權限

 

下面咱們再來繼續看一個例子

假如如今咱們設置test.sh的mask爲read only,那麼family組的用戶還會有write 權限嗎?

 

經過實例來了解 爲多用戶或者組的文件和目錄賦予訪問權限rwx

示例:

 

// 查看文件或者目錄的ACL權限 使用 getfacl FILE|DIR
[root@localhost scripts]# getfacl  100.sh
# file: 100.sh
# owner: root
# group: root
user::rw-
group::r--
other::r--
//給文件100.sh加上mage的運行權限
[root@localhost scripts]# setfacl -m u:user_00:w 100.sh
//能夠發現,在權限位的後面加上了一個加號,這就說明該文件被賦予了ACL權限
[root@localhost scripts]# ll 100.sh
-rw-rw-r--+ 1 root root 660 Sep 21 14:09 100.sh
// 查看100.sh的ACL權限
[root@localhost scripts]# getfacl 100.sh
# file: 100.sh
# owner: root
# group: root
user::rw-
user:user_00:-w-
group::r--
mask::rw-
other::r--
//若是此時user_00用戶去讀100.sh文件,他是不能讀取的。
//指定admins組裏的成員對100.sh文件有讀寫權限
[root@localhost scripts]# setfacl -m g:admin:rw 1001.sh
[root@localhost scripts]# getfacl 1001.sh
# file: 1001.sh
# owner: root
# group: root
user::rw-
group::r--
group:admin:rw-
mask::rw-
other::r--
//此時user_00用戶能夠對1001.sh文件機型寫入嗎
//切換到user_00用戶
  [user_00@localhost ~]$ echo helloworld >> /server/scripts/1001.sh
//下面這些命令能夠本身嘗試一下
setfacl -m u:wang:rwx file|directory
setfacl -Rm g:sales:rwX directory //遞歸
setfacl -M file.acl file|directory //增長權限
setfacl -m g:salesgroup:rw file| directory
setfacl -m d:u:wang:rx directory //設置目錄的默認權限
setfacl -x u:wang file |directory
setfacl -X file.acl directory 刪除權限
//增長權限和刪除權限的文件格式是不同的。
//定義一個文件用來增長權限
[root@localhost scripts]# nano file.acles
//先看一下f1的ACl
[root@localhost scripts]# getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
group::r--
other::r--
//給file.acles增長權限
u:mage:rwx
g:admins:rw
[root@localhost scripts]# setfacl  -m u:user_00:rwx file.acles
[root@localhost scripts]# setfacl -m g:admin:rw file.acles
//執行修改權限命令
[root@localhost scripts]# setfacl -M file.acles f1
setfacl: Invalid argument in line 1 of file file.acles   #這裏報錯,須要在研究下
[root@localhost scripts]# setfacl -x u:user_00 file.acles
[root@localhost scripts]# getfacl file.acles
# file: file.acles
# owner: root
# group: root
user::rw-
group::r--
group:admin:rw-
mask::rw-
other::r--


ACL mask 值

一個文件的權限一般涉及到哪幾類用戶。 屬主,屬組,自定義用戶,自定義組,Other,一共五類使用者。 而ACL就是對這五類用戶的權限進行控制 。

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

 

[root@localhost scripts]# getfacl  f1
# file: f1
# owner: root
# group: root
user::rw-
user:user_00:rwx
group::r--
group:admin:rw-
mask::rwx
other::r--
//設置文件的mask值位 r--
[root@localhost scripts]# setfacl -m mask::r-- f1
[root@localhost scripts]# getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
user:user_00:rwx        #effective:r--
group::r--
group:admin:rw-            #effective:r--
mask::r--
other::r--
設置mask值以後咱們會發現,自定義用戶,自定義組,以及默認的屬組,都被mask值限定住了。
能夠將mask值理解爲最高權限,除了全部者,Other以外,其餘的都在mask 的管轄以內。
getfacl 可看到特殊權限:flags
經過ACL賦予目錄默認x權限,目錄內文件也不會繼承x權限
base ACL 不能刪除
setfacl -k dir 刪除默認ACL權限
setfacl –b file1清除全部ACL權限
getfacl file1 | setfacl –set-file=- file2 複製file1的acl權限給file2
//getfacl 可看到特殊權限:flags
 //若是一個文件或目錄設置特殊權限,SGID,SUID,Sticky
    # file: dir
    # owner: root
    # group: root
    # flags: -s-
    user::rwx
    group::r-x
    other::r-x
mask隻影響除全部者和other的以外的人和組的最大權限Mask須要與用戶的權限進行邏輯與運算後,才能變成有限的權限(Effective Permission)
用戶或組的設置必須存在於mask權限設定範圍內纔會生效
setfacl -m mask::rx file
–set選項會把原有的ACL項都刪除,用新的替代,須要注意的是必定要包含UGO的設置,不能象-m同樣只是添加ACL就能夠
示例: setfacl –set u::rw,u:wang:rw,g::r,o::- file1


練習一:

一、root用戶新建目錄/tmp/common,要求目錄的所屬組爲tom

 

# mkdir /tmp/common
前提是有tom組
# chgrp tom /tmp/common/
或者
# chown .tom /tmp/common/

二、是否容許tom用戶在/tmp/common下建立文件tomfile?若是不容許,怎麼才能讓它建立文件?

不容許,給屬組一個w權限

 

chmod g+w /tmp/common

三、用戶jack若是想修改tomfile文件,怎麼作?

 

chmod o+w tomfile
或者
usermod -G tom jack
或者
gpasswd -a jack tom(組)
或者
setfacl -m u:jack:rw tomfile

 

練習二:

一、以普通用戶user01身份登陸,新建目錄/shared/sysadmin,而且屬於sysadmin組

 

# su - user01
$ mkdir /shared/sysadmin -p
# setfacl -m u:user01:rwx /
# chgrp sysadmin /shared/sysadmin

 

二、要求組成員有讀寫訪問的權限,其餘成員沒有任何權限

 

[root@node1 home]# ll -d /shared/sysadmin/-x
drwxrwxr-x. 2 user01 sysadmin 4096 Apr  6 14:44 /shared/sysadmin/
[root@node1 home]# chmod o=--- /shared/sysadmin/
[root@node1 home]# ll -d /shared/sysadmin/

 

三、要求組成員不能夠刪除屬於其餘成員的文件

 

# ll -d /shared/sysadmin/
drwxrwx--T. 2 user01 sysadmin 4096 Apr  6 14:44 /shared/sysadmin/

 

四、要求在/shared/sysadmin下面建立的文件所有屬於sysadmin組

 

# ll -d /shared/sysadmin/
drwxrws--T. 2 user01 sysadmin 4096 Apr  6 14:44 /shared/sysadmin/

五、要求harry可寫,natash不能讀寫

 

# setfacl -m u:harry:rwx /shared/sysadmin/
# setfacl -m u:natash:--- /shared/sysadmin/
相關文章
相關標籤/搜索