用户组管理系列(二):权限设置

1、权限简介

   操作系统中每个文件都拥有特定的权限、所属用户和所属组。权限是操作系统用来限制资源访问的机制,在Linux中权限一般分为读(readable)、写(writable)和执行(excutable),分为三组。分别对应文件的属主(owner),属组(group)和其他用户(other),通过这样的机制来限制哪些用户、哪些组可以对特定的文件进行什么样的操作。

用户组管理系列(二):权限设置

2、文件和目录权限的区别

   对文件和目录而言,读写执行表示不同的意义

    对文件

r     可以使用cat查看文件的内容
w     可以修改文件的内容
x     可以将其运行为二进制文件

    对目录

r     可以查看目录下列表
w     可以创建和删除目录下文件
x     可以使用cd进入目录

    注:文件的x权限一般关闭,防止可执行的二进制病毒文件自动运行

        目录的x权限一般开启,否则r和w权限将失去意义

3、权限的控制

    <1>修改文件的属主属组

        chown [OPTION]… FILE…       

          -R:递归修改文件的属主属组

        chgrp [OPTION]… FILE… 

[root@centos7 testdir]# touch f1 f2
[root@centos7 testdir]# ll
total 0
-rw-r--r--. 1 root root 0 Aug  4 13:22 f1
-rw-r--r--. 1 root root 0 Aug  4 13:22 f2
[root@centos7 testdir]# chown zhao:zhao f1
[root@centos7 testdir]# chown :zhao f2
[root@centos7 testdir]# touch f3
[root@centos7 testdir]# chgrp zhao f3
[root@centos7 testdir]# ll
total 0
-rw-r--r--. 1 zhao zhao 0 Aug  4 13:22 f1
-rw-r--r--. 1 root zhao 0 Aug  4 13:22 f2
-rw-r--r--. 1 root zhao 0 Aug  4 13:23 f3

<2>修改文件的权限属性

     chmod [OPTION]… MODE[,MODE]… FILE…

         -R:递归修改文件的权限

         下面是三种修改权限的方法:

         赋权表示法:操作一类用户的所有权限位

         授权表示法:操作一类用户的一个权限位

        数字表示法:用数字的形式表示权限

[root@centos7 testdir]# touch f1
[root@centos7 testdir]# ll f1
-rw-r--r--. 1 root root 0 Aug  4 14:13 f1
[root@centos7 testdir]# chmod o=rw f1
[root@centos7 testdir]# ll f1
-rw-r--rw-. 1 root root 0 Aug  4 14:13 f1
[root@centos7 testdir]# chmod o+x f1
[root@centos7 testdir]# ll f1
-rw-r--rwx. 1 root root 0 Aug  4 14:13 f1
[root@centos7 testdir]# chmod 644 f1
[root@centos7 testdir]# ll f1
-rw-r--r--. 1 root root 0 Aug  4 14:13 f1

      chmod [OPTION]… –reference=RFILE FILE…

[root@centos7 testdir]# ll
total 0
-rw-r--r--. 1 root root 0 Aug  4 14:13 f1
-rw-rw-r--. 1 root root 0 Aug  4 14:18 f2
[root@centos7 testdir]# chmod --reference=f1 f2
[root@centos7 testdir]# ll
total 0
-rw-r--r--. 1 root root 0 Aug  4 14:13 f1
-rw-r--r--. 1 root root 0 Aug  4 14:18 f2
[root@centos7 testdir]#

     chgrp [GROUPNAME]…

           修改文件的数组

       chown [OWNER][:[GROUP]] FILE…

           修改文件的属主和属组

       chattr [FILE]

           设置文件的特定属性

           -i:不能删除,改名,修改文件

           -a:只能增加

         lsattr [FILE]

           显示特定属性

[root@centos7 testdir]# chattr +i f1
[root@centos7 testdir]# rm -f f1
rm: cannot remove ‘f1’: Operation not permitted
[root@centos7 testdir]# echo f1 > f1
-bash: f1: Permission denied
[root@centos7 testdir]# mv f1 f2
mv: cannot move ‘f1’ to ‘f2’: Operation not permitted
[root@centos7 testdir]# chattr -i f1
[root@centos7 testdir]# chattr +a f1
[root@centos7 testdir]# lsattr f1
-----a---------- f1
[root@centos7 testdir]# echo f1 > f1
-bash: f1: Operation not permitted
[root@centos7 testdir]# echo f1 >> f1
[root@centos7 testdir]# cat f1
f1

3、三种特殊权限及其意义

   在谈特殊权限之前,我们先需要了解一下安全上下文

    安全上下文:计算机上的二进制文件一旦运行为程序,必定是以某个用户的身份在运行。如果此身份与某个被访问的资源属主一致,则应用资源属主权限;与属组一致,应用属组权限;与其他用户一致,应用其他用户权限。换言之,用户的访问权限,取决于发起此进程的用户。但是SUID、SGID这俩类特殊权限将与上述观点不一致。用户运行某程序时,如果程序拥有SUID权限,那么,此进程运行为程序时,用户将应用程序的属主权限去访问文件。SGID同理。

三种权限的应用范围:

SUID 作用在二进制文件上
SGID 作用在二进制文件上
SGID 也可以作用在目录上
SBIT 作用在目录上

下面详细分析三种权限的应用

    <1>SUID作用:应用拥有SUID权限的二进制的属主权限

[root@centos7 testdir]# ll /etc/shadow
-r--------. 1 root root 3933 Aug  4 18:34 /etc/shadow
[zhao@centos7 testdir]$ ll /bin/nano
-rwxr-xr-x. 1 root root 205904 Jun 10  2014 /bin/nano
[root@centos7 testdir]# ll /bin/nano
-rwsr-xr-x. 1 root root 205904 Jun 10  2014 /bin/nano
[root@centos7 testdir]# su zhao
[zhao@centos7 testdir]$ nano /etc/shadow
qi:!!:17017:0:99999:7::: ---->将俩个感叹号去掉,取消锁定--->qi:!!:17017:0:99999:7:::
[zhao@centos7 testdir]$ su qi
[qi@centos7 testdir]$

 注:危险操作,切勿模仿   

    <2>SGID作用在二进制文件上,作用:应用拥有SGID权限的二进制的属主权限,同SUID,不举例。

    <3>SBIT作用在目录上时:如果用户对目录有写权限,对目录下的文件有读权限,虽然不能修改别人的文件,却可以创建自己的文件和删除别人的文件。此时SBIT可以避免删除别人的文件

       示例:在/testdir下,要求自己可以创建和查看自己的文件,能查看别人的文件,但不能删除和修改别人的文件

[root@centos7 testdir]# su user1
[user1@centos7 /testdir]$ touch f1
[user1@centos7 /testdir]$ chmod g-w f1
[user1@centos7 /testdir]$ su 
Password: 
[root@centos7 testdir]# su user2
[user2@centos7 /testdir]$ touch f2
[user2@centos7 /testdir]$ chmod g-w f2
[user2@centos7 /testdir]$ ll f1 f2
-rw-r--r--. 1 user1 user1 0 Aug  4 20:08 f1
-rw-r--r--. 1 user2 user2 0 Aug  4 20:09 f2
[user2@centos7 /testdir]$ cat f1
[user2@centos7 /testdir]$ su
Password: 
[root@centos7 testdir]# chmod o+t .
[root@centos7 testdir]# ll -d .
drwxrwxrwt. 2 root it 24 Aug  4 20:09 .
[root@centos7 testdir]# su user1
[user1@centos7 /testdir]$ echo content > f2
f2: Permission denied.
[user1@centos7 /testdir]$ rm -f f2
rm: cannot remove ‘f2’: Operation not permitted

     *SGID作用在目录上时,在此目录下创建文件时,属组将自动属于目录的属组

     示例:在/testdir下,创建一个组,要求组内的某些用户自己可以创建查看修改自己的文件,也能查看修改组内其他人的文件,不属于此组的用户对文件无任何权限。

    步骤:root添加用户和组g1

          用户创建自己的文件并且把属组改为g1

          root修改目录的属组和对其他用户的访问权限并且将属组置为SGID权限

[root@centos7 testdir]# useradd user1
[root@centos7 testdir]# useradd user2
[root@centos7 testdir]# groupadd g1
[root@centos7 testdir]# gpasswd -a user1 g1
Adding user user1 to group g1
[root@centos7 testdir]# gpasswd -a user2 g1
Adding user user2 to group g1
[root@centos7 testdir]# su user1
[user1@centos7 testdir]$ touch f1
[user1@centos7 testdir]$ chgrp g1 f1
[user1@centos7 testdir]$ su
Password: 
[root@centos7 testdir]# su user2
[user2@centos7 testdir]$ touch f2
[user2@centos7 testdir]$ chgrp g1 f2
[user2@centos7 testdir]$ su
Password: 
[root@centos7 testdir]# ll f1 f2
-rw-rw-r--. 1 user1 g1 0 Aug  4 20:44 f1
-rw-rw-r--. 1 user2 g1 0 Aug  4 20:45 f2
[root@centos7 testdir]# chgrp g1 .
[root@centos7 testdir]# chmod o= .
[root@centos7 testdir]# chmod g+s .
[root@centos7 testdir]# ll -d .
drwxrws---. 2 root g1 24 Aug  4 20:45 .

4、ACL应用

    ACL权限控制主要目的是提供传统的owner,group,other的read,wirte,execute权限之外的具体权限设置,可以针对单一用户或组来设置特定的权限。

        <1>查看acl权限

            getfacl FILENAME

        <2>设置acl权限

            语法格式

                setfacl [OPTION] [FILENAME|DIRECTORY]

            常用选项

-m     添加acl规则
-x     删除acl规则
-M      从文件中读取acl规则
-X     从文件中删除acl规则
-m d     设置默认acl规则,执针对目录有效
-k     清除默认acl规则
-b     清除所有的acl规则
-R     递归设置acl规则

      <3>备份和恢复acl

         第一步:设置目录及目录下文件的acl

[root@centos7 dir]# setfacl -m u:user1:r f1
[root@centos7 dir]# setfacl -m g:user2:r-x .
[root@centos7 dir]# getfacl -R .
# file: .
# owner: root
# group: g1
# flags: -s-
user::rwx
group::r-x
group:user2:r-x
mask::r-x
other::r-x

# file: f1
# owner: root
# group: g1
user::rw-
user:user1:r--
group::r--
mask::r--
other::r--

         第二步:备份这些acl至特定文件

[root@centos7 dir]# getfacl -R . > /tmp/acl.txt

         第三步:清除原有的目录及文件的acl

[root@centos7 dir]# setfacl -b -R .
[root@centos7 dir]# getfacl -R .
# file: .
# owner: root
# group: g1
# flags: -s-
user::rwx
group::r-x
other::r-x

# file: f1
# owner: root
# group: g1
user::rw-
group::r--
other::r--

         第四步:还原acl

[root@centos7 dir]# setfacl -R --set-file=/tmp/acl.txt .
[root@centos7 dir]# getfacl -R .
# file: .
# owner: root
# group: g1
# flags: -s-
user::rw-
user:user1:r--
group::r--
group:user2:r-x			#effective:r--
mask::r--
other::r--

# file: f1
# owner: root
# group: g1
user::rw-
user:user1:r--
group::r--
group:user2:r-x			#effective:r--
mask::r--
other::r--

         

      

                 

原创文章,作者:mfwing,如若转载,请注明出处:http://www.178linux.com/29231

(0)
mfwingmfwing
上一篇 2016-08-05 10:18
下一篇 2016-08-05 10:18

相关推荐

  • Linux中实现java和c语言打印Hello world小程序

    1、C语言实现 先确认linux系统中是否gcc编译器,可以通过rpm -q gcc查询系统已经安装gcc软件包,如果没有安装可以到系统自带光盘里面安装相应的安装包软件。 使用rpm -ivh gcc-4.8.5-4.el7.x86_64.rpm安装编译器,安装后再查询确认已经成功 环境准备好了,我们就可以编写C语言的代码了,我们可以使用vim编辑器编写第一…

    Linux干货 2016-08-26
  • Linux进程管理

    一、程序与进程           1、程序         程序是为了达到特定的目的,可以被计算机运行并且由命令代码组成的语句序列。       &…

    Linux干货 2015-05-14
  • 马哥教育网络班21期+第15周课程练习

    1、总结sed和awk的详细用法; sed 流编辑器,处理一行数据到模式空间(p),不匹配条件话就输出源行,匹配条件且有处理话,就输出处理过后的行和源行, 匹配条件且没有处理动作的话,只输出p空间的行;保持空间(h)用来存放模式空间的临时处理结果 [root@centos ~]# sed ''&nb…

    Linux干货 2016-08-30
  • linux一周学习总结

          对于linux,之前也完全没有接触过,完全零基础小白。来到马哥以后,进入学习也有一周时间 ,一周里老师带我们学习了很多指令,下面,我就自己的理解和老师讲授的内容对linux中的一些指令做一个简单的小总结。 一、关于SHELL Shell     查询当前系统使用了哪个shell   &n…

    2017-07-15
  • Shell脚本之流程控制语句

    Shell脚本之流程控制语句 1、 if语句 (1)if 条件;then        action1 else        action2 fi  注意:shell里没有缩进要求。 (2)if 条件1;then   …

    Linux干货 2017-04-16
  • LVS介绍及工作原理图解

    一、LVS简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器,是一个由章文嵩博士发起的自由软件项目,官方站点是:http://www.linuxvirtualserver.org。现在LVS已经是Linux标准内核的一部分,在Linux2.4内核以前,使用LVS时必须重新编译内核以支持LVS功能模块,但是从Linux2.…

    2017-12-06

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-07 23:45

    文章的层次结构清晰明了,如果能多一些自己对操做部分的理解与总结就更好了。