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

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
下一篇 2016-08-05

相关推荐

  • 第一篇博客 简单说下最近的学习心得吧

        今天是个特殊的日子, 来到马哥教育已经一周时间了,刚来的时候满环信心,感觉人生充满了希望,但是接下来的学习让我感受到了什么是绝望,刚开始的两天完全是一种朦胧的状态,不知道干什么,敲得命令也不理解,完全是生搬硬套,没有自己的认知,当时就有一种冲动想要一走了之,后来想想算了 ,然后就坚持到了现在,此时感觉当时的决定是对的,经过一周的学…

    2017-07-15
  • 浅谈Android应用HTTP框架选型

    我们知道,Android应用主要是通过客户端-服务器的方式进行数据交互,一般在APP开发时可能是一个接口对应一个显示模块,这样的设计会让客户端更新时很麻烦。为了解决这个问题,通常的做法是和服务端之间定义协议,设计显示模式,进而达到实时更新UI界面的目的,增加或删除显示模块时不需要让用户手动更新apk,这种方法最常见的就是在做活动或者广告的时候用到。以我们的优…

    2016-06-22
  • Linux-raid的工作原理与管理

    本章内容:        1、RAID详细描述        2、mdadm 工具        3、创建raid        4、磁盘阵列的管理 RAID描述:     &nb…

    2017-06-24
  • N22-第一周作业

    1、描述计算机的组成及功能      计算机系统由硬件系统和软件系统组成。硬件系统(Hardware system)是计算机完成计算工作  的物质基础。软件系统(Software system):是在计算机硬件设备上运行的各种程序,是介于用户  和硬件系统之间的界面。1.1 计算机的硬件系…

    Linux干货 2016-08-22
  • Linux文件类型及颜色标识

    查看文件类型:      指令:ll 文件名或目录名      –  白色 普通文件(可执行文件)      l  扩写:sysbolic link 淡蓝色 符号链接文件   &…

    Linux干货 2016-10-18
  • grep命令

    ]# chmod -R 700 /home/tuser1 ]# ll /home/tuser1/ total 0 drwx——. 2 root root 72 Nov 20 17:33 skel 新增用户与组: 显示/proc/meminfo: 显示shell 非 /sbin/nologin 的用户 找出/etc/passwd文件中一…

    Linux干货 2016-11-20

评论列表(1条)

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

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