linux文件系统上的特殊权限及细致权限ACL

linux文件系统上的特殊权限及细致权限ACL

Chattr命令:

常用重要参数与选项:

  • :增加某一个特殊参数,其他原本存在的参数则不动;
  • :移除某一个特殊参数,其他原本存在的参数则不动;
    = :设定一定,且仅有后面接的参数;
    a : 当设定 a 之后,这个文件将只能增加数据,而不能删除也不能修改数据,只有root 才能设定这属性
    i :让一个文件『不能被删除、改名、设定连结也无法写入或新增数据!对于系统安全性有相当大的帮助!只有 root 能设定此属性

lsattr 命令:显示特定属性

语法格式:lsattr [-adR] 文件或目录
选项与参数:
-a :将隐藏文件的属性也秀出来;
-d :如果接的是目录,仅列出目录本身的属性而非目录内的文件名;
-R :连同子目录的数据也一并列出来 !
示例:

[root@CentOS7 data]#touch testdir 
[root@CentOS7 data]#chattr +i testdir 
[root@CentOS7 data]#ls 
scripts  testdir
[root@CentOS7 data]#ll 
total 4
drwxr-xr-x. 5 root root 4096 Mar 19 19:38 scripts
-rw-r--r--. 1 root root    0 Mar 19 19:39 testdir
[root@CentOS7 data]#rm -r testdir 
rm: remove regular empty file ‘testdir’? y 
rm: cannot remove ‘testdir’: Operation not permitted
[root@CentOS7 data]#lsattr testdir 
----i----------- testdir

文件特殊权限:SUID,SGID,SBIT

我们一直使用的文件重要权限,那就是rwx这三个读、写、执行权限,那么除了这个就没有其他权限了吗?
细心的人会注意到当我们查看/tmp和/usr/bin/passwd,在权限位上会出现我们不认识的t、s,那么这里的t、s就是文件隐藏的特殊权限了。

[root@CentOS7 data]#ll -d /tmp /usr/bin/passwd 
drwxrwxrwt. 32 root root  4096 Mar 19 19:57 /tmp
-rwsr-xr-x.  1 root root 27832 Jun 10  2014 /usr/bin/passwd
[root@CentOS7 data]#lsattr -a /etc/shadow
---------------- /etc/shadow

特殊权限SUID:

我们都知道/etc/shadow文件的权限为000;那么就是除了root外的所有用户将对这个文件没有任何权限,然而当普通用户使用passwd命令时,发现我们能修改用户密码并保存进/etc/shadow中,那岂不是就前后矛盾了?原因就是/usr/bin/passwd上的特殊权限s,当普通用户执行passwd命令时,会临时拥有root的权限。
特殊权限SUID的功能:
(1)SUID权限仅对二进制程序有效; //普通用户的/usr/bin/passwd 这个程序来说拥有x权限,表示普通用户也能执行passwd;
(2)执行者对于该程序需要具有x的可执行权限; //passwd的拥有者时root,root对文件拥有绝对控制权;
(3)本权限仅在执行该程序的过程中有效; //普通用户执行passwd的过程中,会暂时获得root的权限;
(4)执行者将具有该程序拥有者的权限; // 文件/etc/shadow可以被普通用户执行的passwd命令修改;
那么普通用户能使用cat命令读取/etc/shadow的内容吗?通下面命令我们发现cat命令并不存在s特殊权限位,也不能读取shadow的内容;

[root@CentOS7 data]#ll /bin/cat 
-rwxr-xr-x. 1 root root 54160 Oct 31 03:16 /bin/cat
[root@CentOS7 data]#su gong 
[gong@CentOS7 data]$ cat /etc/shadow 
cat: /etc/shadow: Permission denied

特殊权限SGID:

当s标志在文件的拥有者的x项目为SUID,那s在群组的x时则称为Set GID,简写为SGID;SUID可作用在文件或目录上;
当SGID作用在文件上,其起到的功能为:
(1)SGIU对二进制程序有用;
(2)程序执行者对于该程序来说,需具备x 的权限;
(3)执行者在执行的过程中将会获得该程序群组的支持;

[hh@CentOS7 data]$ ls -l testfile                  //查看文件的权限,在群组位置有的执行位上为s,other用户对程序只有x执行权限;
-rw-r-s--x 1 root gong 10 Mar 20 09:31 testfile
[hh@CentOS7 data]$ id hh                           //用户hh属于组gong的成员
uid=1004(hh) gid=1004(hh) groups=1004(hh),1000(gong)
[hh@CentOS7 data]$ cat testfile                    //这时用户hh在执行的过程中获得了该程序群组的支持,从而可以读取文件内容;
d
sd
sd

当SGID作用在文件上,其起到的功能为:
(1)用户若对此目录具有r与x的权限时,该用户能够进入此目录;
(2)用户在此目录下的有效群组将会变成该目录的群组;
(3)用途:若用户在此目录下具有w的权限(可以新建文件),则使用者所建立的新文件,改新文件的群组与此目录的群组相同;
注:通常用于创建一个协作目录;

[root@CentOS7 data]#ll -d testdir/               //查看目录发现群组对该目录有s权限,其他用户只有w权限
drwxrws-w- 2 root gong 6 Mar 20 10:01 testdir/
[root@CentOS7 data]#su hh                        //切换用户
[hh@CentOS7 testdir]$ id                         //用户hh时群组gong的组员
uid=1004(hh) gid=1004(hh) groups=1004(hh),1000(gong)
[hh@CentOS7 data]$ cd testdir/                   //hh用户有进入目录权限
[hh@CentOS7 testdir]$ touch testfile1            //hh用户有创建新文件的权限
[hh@CentOS7 testdir]$ ll                         //发现hh用户创建的文件的群组自动属于gong
total 0
-rw-rw-r-- 1 hh gong 0 Mar 20 10:06 testfile1
[hh@CentOS7 testdir]$ echo 11111 > testfile1     //hh用户可修改文件
[hh@CentOS7 testdir]$ cat testfile1              //hh用户可读取文件
11111
[hh@CentOS7 testdir]$ rm testfile1               //hh用户可删除文件

特殊权限SBIT:

SBIT只对目录有效,功能是:
当用户对此目录具有w、x权限,亦即具有写入的权限时;
当用户在该目录下建立文件或目录时,仅有自己与root才有权利删除该文件;

[root@CentOS7 data]#ls -ld testdir/                       //目录testdir的other的权限位上为t,权限为777
drwxrwsrwt 2 root root 23 Mar 20 10:20 testdir/
[root@CentOS7 testdir]#su hh                 
[hh@CentOS7 testdir]$ ll -d testfile1 
-rwxrwxrwx 1 gong gong 0 Mar 20 10:20 testfile1           //文件的所有者和所属组为gong, hh用户对该文件拥有rwx权限
[hh@CentOS7 testdir]$ rm -f testfile1                     //但hh用户不能删除此文件,只能root和文件属主才能删除
rm: cannot remove ‘testfile1’: Operation not permitted

主机的细致权限规划:ACL的使用

ACL 是 Access Control List 的缩写,主要的目的是在提供传统的 owner,group,others 的 read,write,execute 权限之外的细部权限设定。ACL 可以针对单一使用者,单一文件或目录来进行 r,w,x 的权限规范,对于需要特殊权限的使用状况非常有帮助。
查看本机的文件系统是否支持ACL:

[root@CentOS7 data]#dmesg |grep -i acl 
[    0.869680] systemd[1]: systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)
[    3.264996] SGI XFS with ACLs, security attributes, no debug enabled

使用ACL:
getfacl :取得某个文件/目录的ACL设定项目
语法格式:setfacl [-bkRd] [{-m|-x} acl参数] 目标文件名
选项与参数:
-m :设定后续的 acl 参数给文件使用,不可与 -x 合用;
-x :删除后续的 acl 参数,不可与 -m 合用;
-b :移除『所有的』 ACL 设定参数;
-k :移除『预设的』 ACL 参数,关于所谓的『预设』参数于后续范例中介绍;
-R :递归设定 acl ,亦即包括次目录都会被设定起来;
-d :设定『预设 acl 参数』的意思!只对目录有效,在该目录新建的数据会引用此默认值
setfacl :设定某个目录/文件的ACL规范,对用户的细致acl权限设置:setfacl -m u:uname:rwx filename

[root@CentOS7 data]#touch acl_testfile                 //创建测试文件
[root@CentOS7 data]#ll acl_testfile                    //查看文件权限
-rw-r--r-- 1 root root 0 Mar 20 10:44 acl_testfile
[root@CentOS7 data]#setfacl -m u:gong:rw acl_testfile   //使用setfacl给用户gong设置权限
[root@CentOS7 data]#ll acl_testfile                     //发现文件的权限后面多了一个+号
-rw-rw-r--+ 1 root root 0 Mar 20 10:44 acl_testfile
[root@CentOS7 data]#setfacl -m u::rwx acl_testfile      //当使用setfacl设置文件权限中间用户空时,发现默认将文件所有者的修改
[root@CentOS7 data]#ll acl_testfile 
-rwxrw-r--+ 1 root root 0 Mar 20 10:44 acl_testfile

查看文件的具体acl权限:getfacl filename

[root@CentOS7 data]#getfacl acl_testfile
# file: acl_testfile
# owner: root
# group: root
user::rwx
user:gong:rw-
group::r--
mask::rw-
other::r--

setfacl :设定某个目录/文件的ACL规范,对群组的细致acl权限设置:setfacl -m g:group:rwx filename

[root@CentOS7 data]#setfacl -m g:gong:r acl_testfile 
[root@CentOS7 data]#getfacl acl_testfile
# file: acl_testfile
# owner: root
# group: root
user::rwx
user:gong:rw-
group::r--
group:gong:r--
mask::rw-
other::r--

猜你喜欢

转载自blog.51cto.com/14233815/2368366