shell腳本--權限分配

由於shell腳本內部是不少命令的集合,這些命令也許會涉及到操做某一個文件,並且shell腳本的運行,也是須要當前用戶對腳本具備運行的權限,不然,會由於權限不夠而失敗。shell

首先最重要的一點:修改權限,只是修改用戶對文件內容,文件內容,文件內容的權限,而不是修改用戶對文件的權限。只有文件的擁有者才能夠對文件的權限進行更改,即便其餘用戶對文件擁有rwx權限,也是不能更改文件權限的,而且只有文件的全部者能夠對文件進行更名、複製、移動、刪除。ubuntu

Linux中涉及權限的命令有:chmod、acl、sudo,下面一一講解他們各自的用法。bash

chmod:用於分配權限,使用的頻率很高。指針

分配權限時,經常使用的有兩種形式,一種是直接使用八進制的三個數字指定文件的全部權限(Owner,group,other),一種是使用某類用戶的簡寫,追加一個+/-,而後加上要分配或者收回的權限。code

root@ubuntu:/# echo 'echo "hello world"' > test.sh
root@ubuntu:/# ls -l test.sh
-rw-r--r-- 1 root root 19 1月  14 11:10 test.sh
root@ubuntu:/# ./test.sh
bash: ./test.sh: Permission denied
root@ubuntu:/# chmod 744 test.sh
root@ubuntu:/# ./test.sh
hello world
root@ubuntu:/# su ubuntu
ubuntu@ubuntu:/$ ls -l test.sh
-rwxr--r-- 1 root root 19 1月  14 11:10 test.sh
ubuntu@ubuntu:/$ echo "cover it" > test.sh
bash: test.sh: Permission denied
ubuntu@ubuntu:/$ chmod 746 test.sh
chmod: changing permissions of 'test.sh': Operation not permitted
ubuntu@ubuntu:/$ su 
Password: 
root@ubuntu:/# chmod 746 test.sh
root@ubuntu:/# su ubuntu
ubuntu@ubuntu:/$ echo "cover it" > test.sh
ubuntu@ubuntu:/$

  

另一種形式:blog

root@ubuntu:/# echo 'echo "hello world"' > test.sh
root@ubuntu:/# ls -l test.sh
-rw-r--r-- 1 root root 19 1月  14 11:20 test.sh
root@ubuntu:/# chmod o+x test.sh #給other分配執行權限
root@ubuntu:/# su ubuntu
ubuntu@ubuntu:/$ ./test.sh
hello world
ubuntu@ubuntu:/$

  上面的一種形式,分配權限比較直觀,由於給什麼樣的用戶分配什麼權限,一目瞭然,不須要計算。其中擁有者使用u,組用戶使用g,其餘用戶使用o,a表示全部用戶。繼承

  對於權限分配,比較穩妥的方式是:給某個文件的group用戶分配讀寫執行的權限,而後將某個other的用戶添加到group中去。不然若是other分配權限是不能細分的,好比我只想對other中的6個用戶分配寫權限,那麼就不能對other分配w權限了,由於一旦分配w,則全部的other就有了w權限。遞歸

  若是想對權限細分,也就是單獨的對某個用戶分配對某個文件的權限的話,可使用acl權限分配,acl(access control list,訪問控制列表)。acl權限分配,有兩個命令,getfacl用來獲取某個文件的acl權限,setfacl用來設置文件的acl權限。get

下面是使用getfacl來查看test.sh文件的訪問控制列表it

ubuntu@ubuntu:/$ ls -l test.sh
-rw-r--r-- 1 root root 19 1月  14 11:20 test.sh
ubuntu@ubuntu:/$ getfacl test.sh
# file: test.sh
# owner: root
# group: root
user::rw-
group::r--
other::r--

  注意user::rw中間是兩個冒號,這個說明兩個冒號中間是能夠添加用戶的,若是省略中間的用戶時,表示這一類的全部用戶,好比other的全部用戶有r-x權限。

  登陸root用戶,給ubuntu用戶分配rw權限(收回x權限),使用setfacl。修改某個用戶的權限使用-m參數

  注意格式:對於用戶的話,格式爲setfacl -m user:username:rwx  filename 。對於組,格式爲:setfacl -m group:groupName:rwx filename。還要注意的是,rwx儘可能全寫,沒有的權限使用-代替,若是隻寫rw,那麼他的x默認不分配。

例子:

root@ubuntu:/# echo 'echo "hello world"' >test.sh
root@ubuntu:/# ls -l test.sh
-rw-r--r-- 1 root root 19 1月  14 14:29 test.sh
root@ubuntu:/# setfacl -m u:ubuntu:rw- test.sh #給ubuntu用戶分配rw權限,不給x權限
root@ubuntu:/# su
root@ubuntu:/# su ubuntu
ubuntu@ubuntu:/$ ./test.sh  #執行失敗
bash: ./test.sh: Permission denied
ubuntu@ubuntu:/$ echo "echo 'cover it'" > test.sh   #能夠寫
ubuntu@ubuntu:/$ cat test.sh    #能夠讀
echo 'cover it'
ubuntu@ubuntu:/$

  

  刪除用戶的全部權限,可使用setfacl -m user:username:--- filename。簡潔的作法是:使用-x參數

root@ubuntu:/# setfacl -m user:ubuntu:--- test.sh
root@ubuntu:/# getfacl test.sh
# file: test.sh
# owner: root
# group: root
user::rw-
user:ubuntu:---
group::r--
mask::r--
other::r--

root@ubuntu:/# setfacl -x user:ubuntu test.sh
root@ubuntu:/# getfacl test.sh
# file: test.sh
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--

  

  注意:目錄的x執行權限是指一些命令(限制cd等命令),而r讀權限是指針對一些命令如ls,tree等命令。

root@ubuntu:/# mkdir abc
root@ubuntu:/# getfacl abc
# file: abc
# owner: root
# group: root
user::rwx
group::r-x
other::r-x

root@ubuntu:/# setfacl -m user:ubuntu:r-- abc  #撤銷ubuntu用戶對目錄abc的x權限
root@ubuntu:/# su ubuntu
ubuntu@ubuntu:/$ cd abc
bash: cd: abc: Permission denied

  要想分配給某個用戶某個文件及其子目錄的某個權限時,這須要遞歸,使用-R參數,可是這樣是不方便的,若是在子目錄又建立一個目錄,目錄下再建立一個文件,這個文件的權限不會繼承當前對某個用戶分配的對該文件的權限,這時能夠添加default(d)來達到某個用戶在某個目錄建立的文件。

root@ubuntu:/# setfacl -m default:user:ubuntu:rwx /abc/
相關文章
相關標籤/搜索