shell脚本--权限分配

因为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/

  

  

原文地址:https://www.cnblogs.com/-beyond/p/8282623.html