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

  

  

shell脚本--权限分配的更多相关文章

  1. Shell文件权限和脚本执行

    一.预备知识 1.shell的作用   2.常识 (1)Tab键自动补全   使用Terminal时,输入命令的前几个字母,敲tab会自动补全命令或文件名.目录等. 好处:操作速度更快:不容易出错: ...

  2. Linux终端执行shell脚本,提示权限不够的解决办法

    原文:http://blog.csdn.net/this_capslock/article/details/17415409 今天在Linux尝试搭建dynamips的工作环境,在执行shell脚本时 ...

  3. php利用root权限执行shell脚本 (转)

    转一篇博客,之前搞这个东西搞了好久,结果今天晚上看到了一篇救命博客,瞬间开心了...转载转载 利用sudo来赋予Apache的用户root的执行权限,下面记录一下: 利用PHP利用root权限执行sh ...

  4. Linux终端执行shell脚本,提示权限不够

    在学习dubbo过程中,上传自己写的脚本,执行的时候提示“权限不够”,从网上了解到是因为没有为脚本赋权限 解决方法是使用chmod命令为shell脚本赋予权限 chmod 777 ./service- ...

  5. centos分配IP脚本--写的第一个shell脚本

    IDC小菜鸟一枚,非科班出身.常常有客户的centos服务器需要分配15个IP甚至30个IP.每次需要手动分配十分麻烦,于是花了一天时间学了shell脚本,写了这个脚本. #!/bin/bash re ...

  6. shell脚本每天自动备份mysql数据库

    一.mysql提供了一个mysqldump的工具可以方便的导出导入数据库信息: 二.使用命令行shell测试执行mysqldump,理解必备的参数,查看生成的sql备份文件是否符合需求: /usr/b ...

  7. 《Linux命令行与shell脚本编程大全》 第二十三章 学习笔记

    第二十三章:使用数据库 MySQL数据库 MySQL客户端界面 mysql命令行参数 参数 描述 -A 禁用自动重新生成哈希表 -b 禁用 出错后的beep声 -B 不使用历史文件 -C 压缩客户端和 ...

  8. 《Linux命令行与shell脚本编程大全》 第二十二章 学习笔记

    第二十二章:使用其他shell 什么是dash shell Debian的dash shell是ash shell的直系后代,ash shell是Unix系统上原来地Bourne shell的简化版本 ...

  9. 《Linux命令行与shell脚本编程大全》 第十四章 学习笔记

    第十四章:呈现数据 理解输入与输出 标准文件描述符 文件描述符 缩写 描述 0 STDIN 标准输入 1 STDOUT 标准输出 2 STDERR 标准错误 1.STDIN 代表标准输入.对于终端界面 ...

随机推荐

  1. UVA12171-Sculpture(离散化+floodfill)

    Problem UVA12171-Sculpture Accept: 196  Submit: 1152 Time Limit: 3000 mSec Problem Description Imagi ...

  2. $Matrix-Tree$定理-理论

    $Matrix-Tree$ 矩阵的行列式 这个东西看了好久才明白 _ (:з」∠)_ 时间不够可以直接跳到第六段. 看到这种新定义,第一反应还是去翻百度百科: 但是这个讲解真的让人很迷惑...关键就是 ...

  3. 转载 信号量 <第六篇>

    一.ManualResetEvent 该对象有两种信号量状态True和False.构造函数设置初始状态.简单来说, 如果构造函数由true创建,则第一次WaitOne()不会阻止线程的执行,而是等待R ...

  4. HTTPS协议,SSL协议及完整交互过程

    文章转自 https://blog.csdn.net/dfsaggsd/article/details/50910999 SSL 1.        安全套接字(Secure Socket Layer ...

  5. mongodb数据库中插入数据

    mongodb数据库中插入数据 一:connection 访问集合: 在mongodb数据库中,数据是存储在许多数据集合中,可以使用数据库对象的collection方法访问一个集合.该方法使用如下: ...

  6. Vue 改变数组中对象的属性不重新渲染View的解决方案

    Vue 改变数组中对象的属性不重新渲染View的解决方案 在解决问题之前,我们先来了解下 vue响应性原理: Vue最显著的一个功能是响应系统-- 模型只是一个普通对象,修改对象则会更新视图.受到ja ...

  7. jmeter(十一)JDBC Request之Query Type

    工作中遇到这样一个问题: 需要准备10W条测试数据,利用jmeter中的JDBC Request向数据库中批量插入这些数据(只要主键不重复就可以,利用函数助手中的Random将主键的ID末尾五位数随机 ...

  8. RBAC 基于权限的访问控制 serviceaccount -- clusterRole clusterRoleBinding

    1.Role , RoleBinding 的作用对象都是namespace. 2.通过RoleRef,可以看到,RoleBinding对象通过名字,直接引用前面定义的Role,实现subject(us ...

  9. Shiro的认证授权

    shiro安全框架入门整理 package com.shiro.test; import org.apache.shiro.SecurityUtils; import org.apache.shiro ...

  10. C# 判断一个文本文件的编码格式(转载)

    文件的字符集在Windows下有两种,一种是ANSI,一种Unicode.对于Unicode,Windows支持了它的三种编码方式,一种是小尾编码(Unicode),一种是大尾编码(BigEndian ...