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

1、权限简介

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

wKiom1epIfqTFvSSAAAHpgQUSV4376.png

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:递归修改文件的权限

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

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

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

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

[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:只能增加

                -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@localhost /tmp]#ll /bin/nano
-rwxr-xr-x. 1 root root 205904 Jun 10  2014 /bin/nano
[root@localhost /tmp]#chmod u+s /bin/nano     ######添加suid权限
[root@centos7 testdir]# ll /bin/nano
-rwsr-xr-x. 1 root root 205904 Jun 10  2014 /bin/nano
[root@centos7 testdir]# ll /etc/shadow
-r--------. 1 root root 3933 Aug  4 18:34 /etc/shadow
[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 .

注意:当用户对某文件所在的目录的父目录无写权限时,即使其对本目录有写和执行权限,对其下的文件有读权限,也是无法删除文件的。

[root@centos7 b]# pwd
/tmp/a/b
[root@centos7 b]# ls
b.txt
[root@centos7 b]# chmod o=x ../
[root@centos7 b]# ll -d ../
drwxr-x--x. 3 root root 60 Aug  9 19:58 ../
[root@centos7 b]# su zhao
[zhao@centos7 b]$ rm -f b.txt 
rm: cannot remove ‘b.txt’: Permission denied

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规则
[root@localhost /tmp]#setfacl -m d:u:user:rw .    #######设置目录acl默认权限
[root@localhost /tmp]#touch f1
[root@localhost /tmp]#ll
total 4
-rw-rw-rw-+ 1 root root 0 Aug 14 16:06 f1         ######创建文件自动设置acl
[root@localhost /tmp]#setfacl -k .                #######删除目录默认acl
[root@localhost /tmp]#touch f2
[root@localhost /tmp]#ll
total 4
-rw-rw-rw-+ 1 root root 0 Aug 14 16:06 f1
-rw-r--r--. 1 root root 0 Aug 14 16:06 f2          ######创建文件不再有acl
[root@localhost /tmp]#setfacl -R -m d:u:user:rw .  #####递归设置权限,目录和文件都有acl
[root@localhost /tmp]#ll -d .
drwxrwxrwt+ 8 root root 4096 Aug 14 16:06 .
[root@localhost /tmp]#ll
total 8
-rw-rwxrw-+ 1 root root 0 Aug 14 16:06 f1
-rw-rw-r--+ 1 root root 0 Aug 14 16:06 f2

    <3>备份和恢复acl

       必要性:备份数据时,如果压缩文件,会导致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: root
user::rwx
group::r-x
group:user2:r-x
mask::r-x
other::r-x

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

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

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

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

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

# file: acl.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--

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

        第四步:还原acl

[root@centos7 dir]# setfacl --restore=acl.txt       ######还原时不需要递归
[root@centos7 dir]# ll -d .
drwxr-xr-x+ 2 root root 80 Aug  5 20:01 .
[root@centos7 dir]# getfacl -R .
# file: .
# owner: root
# group: root
user::rwx
group::r-x
group:user2:r-x
mask::r-x
other::r-x

# file: acl.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--

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

注意:此处的acl.txt备份在了当前目录,实际操作中不要备份在当前目录下

5、mask和umask

    <1>mask

        在linux系统上,不同的用户建立不同的文件和目录时,文件和目录会默认拥有不同的权限,这与系统上的umask设置有关系,设置umask变量临时生效,重启失效。且其配置文件保存在/etc/profiile和/etc/bashrc中,若只是对单用户生效,保存在家目录下的.bashrc和.bash_profile文件中即可。

[root@centos7 ~]# umask
0022
[root@centos7 ~]# su user1
[user1@centos7 root]$ umask
0002

计算方法

        root用户

                对文件 666-022=nnn

                对目录 777-022=nnn

        普通用户

                对文件 666-002=nnn 

                对目录 777-022=nnn      

注意:如果对文件算得的权限位有奇数,要加1,目的在于屏蔽掉文件的执行权限,防止可执行病毒文件对系统的威胁。         

        <2>umask

               umask是对文件或者目录设置acl权限时,所划定的一个最高权限位,其权限等于文件和文件所属目录的属组权限,且大于受acl规则限制的用户或组的权限。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值