用户组和权限管理2

十七、linux文件系统上的特殊权限

    suid:s表示,sgid:s表示,sticky:t表示

    

    安全上下文

    前提:进程有属主和属组,文件有属主和属组

        任何一个可执行程序文件能不能启动为进程,取决发起者对程序是否拥有执行权限

        启动为进程之后,其进程的属主为发起者,进程的属组为发起者所属的组

        进程访问文件是的权限,取决于进程的发起者

            进程的发起者,同文件的属主,则应用文件属主权限

            进程的发起者,属于文件属组,则应用文件属组权限

            应用文件其他权限

    可执行文件上suid权限

        任何一个可执行程序文件能不能启动为进程,取决于发起者对程序文件是否拥有执行权限

        启动为进程之后,其进程的属主为原程序文件的属主

        suid只对二进制可执行程序有效

        suid设置在目录上无意义

            chmod u+s file    

            chmod u-s file

[root@localhost testdir]# ll
total 0
-rwxrwxrwx 1 root wang 0 Aug  4 03:33 file1
-rwxrwxrwx 1 root wang 0 Aug  4 03:35 file2
[root@localhost testdir]# chmod u+s file1
[root@localhost testdir]# ll
total 0
-rwsrwxrwx 1 root wang 0 Aug  4 03:33 file1
-rwxrwxrwx 1 root wang 0 Aug  4 03:35 file2
[root@localhost testdir]# chmod u-s file1
[root@localhost testdir]# ll
total 0
-rwxrwxrwx 1 root wang 0 Aug  4 03:33 file1
-rwxrwxrwx 1 root wang 0 Aug  4 03:35 file2

    可执行文件上sgid权限

        任何一个可执行程序文件能不能启动为进程,取决发起者对程序文件是否拥有执行权限

        启动文件为进程之后,其进程的属主为源程序文件的属组

            chmod g+s file

            chmod g-s file

[root@localhost testdir]# chmod g+s file1
[root@localhost testdir]# ll
total 0
-rwxrwsrwx 1 root wang 0 Aug  4 03:33 file1
-rwxrwxrwx 1 root wang 0 Aug  4 03:35 file2
[root@localhost testdir]# chmod g-s file1
[root@localhost testdir]# ll
total 0
-rwxrwxrwx 1 root wang 0 Aug  4 03:33 file1
-rwxrwxrwx 1 root wang 0 Aug  4 03:35 file2

    目录上的sgid权限

        默认情况下,用户创建文件是,其属组为此用户所属的主组

        一旦某目录被设定了sgid,则对此目录有写权限的和用户在此目录中创建的文件所属的组为此目录的属组

        通常用于创建一个协作目录

                chmod g+s file

            chmod g-s file

[root@localhost testdir]# mkdir 1234
[root@localhost testdir]# ll -a
total 8
drwxr-xr-x   3 1000 user1   41 Aug  4 06:56 .
dr-xr-xr-x. 18 root root  4096 Aug  4 00:17 ..
drwxr-xr-x   2 root root     6 Aug  4 06:56 1234
-rw-r--r--+  1 root root     0 Aug  4 04:49 file1
-rwxrwxrwx   1 root wang     4 Aug  4 04:32 file2
[root@localhost testdir]# chmod o+w 1234
[root@localhost testdir]# useradd 1
[root@localhost testdir]# su - 1
[1@localhost ~]$ cd /testdir/1234
[1@localhost 1234]$ touch file1
[1@localhost 1234]$ ll file1
-rw-rw-r-- 1 1 1 0 Aug  4 06:58 file1
[1@localhost 1234]$ exit
logout
[root@localhost testdir]# chmod g+s 1234
[root@localhost testdir]# su - 1
Last login: Thu Aug  4 06:57:23 CST 2016 on pts/1
[1@localhost ~]$ cd /testdir/1234
[1@localhost 1234]$ touch file2
[1@localhost 1234]$ ll
total 0
-rw-rw-r-- 1 1 1    0 Aug  4 06:58 file1
-rw-rw-r-- 1 1 root 0 Aug  4 06:58 file2

    suicky位

        具有写权限的用户通常用户可以删除该目录中的任何文件,无论该文件的权限或拥有权

        在目录设置sticky位,只有文件的所有者或root可以删除该文件

        sticky设置在文件上无意义

            chmod o+s file

            chmod o-s file

    特殊权限数字法

        suid:chmod 4777 file

        sgid:chmod 2777 file

        sticky:chmod 1777 dir

    权限为映射

        suid:user,占据属主的执行权限位

            s:属主拥有x权限

            S:属主没有x权限

        sgid:group,占据属组的执行权限位

            s:group属组拥有x权限

            S:group没有x权限

        sticky:other,占据other的执行权限位

            t:other拥有x权限

            T:other没有x权限

    设置文件特定属性

        chattr option file

            +A:锁定访问时间,访问不刷新时间

            +i:锁定文件不能删除,改名,更改

            +a:锁定文件,能看,能修改,不能改名,不能删除

            lsattr 显示特定属性

        作用:避免误操作,误删除。

[root@localhost testdir]# chattr +i file2
[root@localhost testdir]# rm -rf file2
rm: cannot remove ‘file2’: Operation not permitted
[root@localhost testdir]# echo "xxx" > file2
-bash: file2: Permission denied
[root@localhost testdir]# lsattr file2
----i----------- file2

十八、访问控制列表

    acl:access control list。实现灵活的权限管理

    除了文件的所有者,所属组和其他人,可以对更多的用户设置权限

        Centos7默认创建的xfs和ext4文件系统有acl功能

        Centos7之前版本,默认手工创建的ext4文件系统无acl功能

    acl生效顺序:所有者,自定义用户,自定义组,其他人

        setfacl option (d)(ug):username:权限 file

                u:用户

                g:组

                d:在这个目录下新建的文件有acl权限

            -m:修改acl权限

            -x:删除acl权限

            -M:调用脚本修改acl权限

            -X:调用脚本删除acl权限

useradd wangwenjie
[root@localhost testdir]# touch file1
[root@localhost testdir]# setfacl -m g:wangwenjie:rwx file1
[root@localhost testdir]# getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r--
group:wangwenjie:rwx
mask::rwx
other::r--

[root@localhost testdir]# setfacl -x g:wangwenjie file1
[root@localhost testdir]# getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--

        setfacl -k dir:删除默认acl权限

        setfacl -b file:清除所有acl权限

[root@localhost testdir]# ll
total 0
-rw-r--r--+ 1 root root 0 Aug  4 07:10 file1
[root@localhost testdir]# setfacl -b file1
[root@localhost testdir]# ll
total 0
-rw-r--r-- 1 root root 0 Aug  4 07:10 file1

        getfacl:查看特殊权限

        getfacl file1 | setfacl –set-file=-file2  复制file的acl权限给file2

[root@localhost testdir]# setfacl -m g:wangwenjie:rwx file1
[root@localhost testdir]# getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r--
group:wangwenjie:rwx
mask::rwx
other::r--

[root@localhost testdir]# touch file2
[root@localhost testdir]# getfacl file1 | setfacl --set-file=- file2
[root@localhost testdir]# getfacl file2
# file: file2
# owner: root
# group: root
user::rw-
group::r--
group:wangwenjie:rwx
mask::rwx
other::r--

    

    acl文件上的group权限是mask值(自定义用户,自定义组,拥有组的最大权限),而非传统的组权限。

        默认acl权限给x,文件也不会x权限

        bash cal 不能删除

    mask只影响除所有者和other的之外的人和组的最大权限

    nask需要与用户的权限进行逻辑与运算后,才能变成有限的权限

        用户或组的设置存在于mask权限设定范围内才会生效

            setfacl -m mask::权限 file

        –set选项会把所有的acl想都删除,用新的代替,需要注意的是一定要包含ugo的设置,不能像-m一样只是添加acl就可以

            setfacl –set u::权限,g::权限,o::权限 file

    备份和恢复acl

        主要的文件操作命令cp和mv都支持acl,只是cp命令需要加上-p参数,但是tar等常见的备份工具是不会保留目录和文件的acl信息。

            cp -p 保留权限

            getfacl -R 目录 > file1     把目录里的acl权限备份到file1文件里

            setfacl -R -b 目录     删除目录的所有acl权限

            setfacl -R –set-file=file1 目录     通过file1文件把目录的acl权限还原

原创文章,作者:DYW,如若转载,请注明出处:http://www.178linux.com/28960

(0)
上一篇 2016-08-04 21:45
下一篇 2016-08-04 22:34

相关推荐

  • 关于find命令的实际应用

    1、查找/var目录下属主为root,且属组为mail的所有文件 2、查找/var目录下不属于root、lp、gdm的所有文件 3、查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件 4、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件 5、查找/etc目录下大于1M且类型为普通文件的所有文件 6、查找/e…

    Linux干货 2016-08-16
  • SHELL脚本基础速览

    变量的赋值: Shell变量赋值从右到左进进行赋值 格式:NAME=value 变量的声明: export 变量名  export NAME=value declare –x 变量名 declare –x NAME=value   readonly 变量名  特定情况下需要使用到只读变量 readonly NAME=v…

    2017-04-15
  • 计算机基础知识(第一天)

    一、计算机的硬件组成         冯诺依曼体系结构:             CPU、存储器、输入设备、输出设备       &nbsp…

    Linux干货 2016-07-26
  • Linux中的网络管理

    1. ifconfig命令的使用方法 ifconfig是一个比较老的命令了,以后可能会慢慢被ip命令替代。ifconfig命令可以用来配置网卡ip地址,配置网卡别名等信息。 ifconfig ifconfig 网络设备名:用来查看网卡的信息,如ip地址,子网掩码,MAC地址等信息 ifconfig eth0 add 172.16.0.35/16:配置eth0…

    2017-05-02
  • linux 查找过滤及用户和组管理命令的一些事例

    1.列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 ~]# who | cut -d' ' -f1 | sort | uniq 2.列出最后登录到当前系统的用户的相关信息。 ~]# last | head -1 3.列出当前系统上被用户当作其默认shell的最多的那个shell。  ~]#…

    Linux干货 2016-10-05
  • cat命令了解

    1 查看文件在LINUX下一切皆文件,光看见文件名和目录名对我们来说,还远远不够。今天,就来介绍一下可以打开文件的命令cat。当然,二进制的可执行文件,不能用cat。在CentOS7下,以/etc/profile文件为例,如下: 首先,怎么打开这个文件呢?直接执行:cat /etc/passwd. 这么多行,能不能看见行号呢?cat -n /etc/pass…

    Linux干货 2017-04-03

评论列表(1条)

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

    文章的层次结构清晰明了,内容丰满,有理论有实践。