由于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/