因為shell腳本內部是很多命令的集合,這些命令也許會涉及到操作某一個文件,而且shell腳本的運行,也是需要當前用戶對腳本具有運行的權限,否則,會因為權限不夠而失敗。
首先最重要的一點:修改權限,只是修改用戶對文件內容,文件內容,文件內容的權限,而不是修改用戶對文件的權限。只有文件的擁有者才可以對文件的權限進行更改,即使其他用戶對文件擁有rwx權限,也是不能更改文件權限的,並且只有文件的所有者可以對文件進行改名、復制、移動、刪除。
Linux中涉及權限的命令有:chmod、acl、sudo,下面一一講解他們各自的用法。
chmod:用於分配權限,使用的頻率很高。
分配權限時,常用的有兩種形式,一種是直接使用八進制的三個數字指定文件的所有權限(Owner,group,other),一種是使用某類用戶的簡寫,追加一個+/-,然后加上要分配或者收回的權限。
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:/$
另外一種形式:
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權限。
下面是使用getfacl來查看test.sh文件的訪問控制列表
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/