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

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

相关推荐

  • redis + keepalived 双主模型

    redis + keepalived 双主模型 架构图:    1.vip默认绑定在redis主上,由redis主提供服务,redis从为备用节点。(实际上提供服务的只是vip) 2.当redis主挂掉,vip会默认漂移至redis从。由redis从提供服务,redis主已经挂掉。 3.当redis主已经恢复,redis从继续提供服务和挂…

    Linux干货 2016-06-23
  • 马哥教育网络班第19期+第7周课程练习

    1、创建一个10G分区,并格式为ext4文件系统; (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; 2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之; 3、写一个脚本  …

    Linux干货 2016-08-08
  •   在服务器托管的过程中出现故障是不能完全避免的问题,可能是服务器硬盘出现硬件故障或者是防火墙遭到攻击。那么服务器托管出现故障怎么办?

      在服务器托管的过程中出现故障是不能完全避免的问题,可能是服务器硬盘出现硬件故障或者是防火墙遭到攻击。那么服务器托管出现故障怎么办?   服务器硬盘出现硬件故障时,大家应注意以下几点:   1、硬盘出现异声,此时磁头已不正常寻址,为防止打伤盘体,需立即断电不可拆解硬盘,需交有超净间的专业公司拆开检查。   2、BIOS不认盘,找不到硬盘,先请计算机工程师仔…

    Linux干货 2016-04-05
  • 文本处理之sed

     sed:是一种行编辑器,它在处理行时会把要处理的行读入模式空间中,处理的是模式空间的内容,一行一行的处理,然后把处理结果显示在屏幕中,不对原文做修改,除非强制重定向。   好处:可同时编辑一个或多个文件,简化了对文件的反复操作。 sed用法:   格式: sed [options ]…'script&#0…

    Linux干货 2016-08-15
  • 雷人的程序注释

    使用Google code search可以搜索到一些比较有趣的代码注释,呵呵。下面的这些程序注释有搞笑的,也有粗口,看来写程序本来也不是一件很枯燥的事,关键看你的心态如何了。读到这些注释的时候,只能想到一个词,那就是“疯狂的程序员”,哈哈。Have a Fun  ;-) 写个程序时不忘表达自己的感情,以免以后忘了。 呵呵,看来自己也不是很自信。 …

    Linux干货 2016-05-10
  • 关于tar命令的一些方法

    tar 解压缩文件时只有当文件格式中有tar才能使用,否则使用其他单独的特定解压缩工具,例如uncompress,gzip ,bzip2.xz等等 tar压缩解压缩命令详解 tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命…

    Linux干货 2016-08-29

评论列表(1条)

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

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