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

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

相关推荐

  • linux发展史

    通过本篇文章,读者可以了解Linux的基本概念、历史、发展情况; 首先,我们通过简单的介绍Linux,了解什么是linux以及它的发展背景; 前言:操作系统介绍 内核负责控制硬件资源分配,而如果只有内核,则只能让计算机硬件运行,而不能有任何功能,因此需要系统调用提供给开发者使用,从而开发应用程序; 内核能够控制硬件,比如:让CPU可以运算、让硬盘可以读写数据…

    Linux干货 2016-10-14
  • 用户与用户组

    用户与组常见命令与用法

    Linux干货 2017-11-18
  • 关于大型网站技术演进的思考(一):存储的瓶颈(1)

    原文出处: 夏天的森林  前不久公司请来了位互联网界的技术大牛跟我们做了一次大型网站架构的培训,两天12个小时信息量非常大,知识的广度和难度也非常大,培训完后我很难完整理出全部听到的知识,今天我换了个思路是回味这次培训,这个思路就是通过本人目前的经验和技术水平来思考下大型网站技术演进的过程。 首先我们要思考一个问题,什么样的网站才是大型网…

    Linux干货 2015-02-26
  • 马哥教育网络班21期-第八周课程练习

    第八周作业 1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 集线器:集线器工作在OSI七层模型的物理层;功能是将传输信号再生或放大,收到数据后将数据转发至除接收接口外的所有接口(广播)),但是由于集线器所有端口属于共享带宽,属于同一个冲突域和属于同一个广播域,容易产生信号冲突导致帧碎片,现今大部分集线器已经被交换机所替代; 网…

    Linux干货 2016-09-01
  • 如何学好C语言

    有人在酷壳的留言版上询问下面的问题 keep_walker : 今天晚上我看到这篇文章。 http://programmers.stackexchange.com/questions/62502/small-c-projects 我也遇到了和提问的老外一样的问题。。能给像遇到这样烦恼的程序员一点建议嘛?谢谢! 我相信,这可能是很多朋友的问题,我以前…

    Linux干货 2016-08-15
  • Linux文件管理类命令相关

    Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 1)、查看文件命令: (1)ls命令: list,列出目录下的内容 语法: ls [OPTION]… [FILE]… 常用选项: -a: 显示所有文件,包括隐藏文件; -A:显示除.和..之外的所有文件; -l: –long, 长格式列表,即显示文件的详细属性信息; -h, –huma…

    Linux干货 2016-11-06

评论列表(1条)

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

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