Linux ACL权限 学习总结
一、前言:
传统印象中,相较于windows系统,Linux系统对于权限管理更加重视,因为在实际角色分配时,分配权限是必不可少的一环。有多少权限干多少事,权限分配的越详细,安全性越高,所以对于运维来讲,掌握权限管理是必须的。
本篇将总结Linux权限管理中的 ACL权限 。
二、ACL权限
2.1 ACL权限
2.2 ACL权限开启
2.2.1 检测ACL权限是否开启
1、首先查看根目录挂载情况
#用 mount 查看根目录 / 挂在情况
[root@localhost ~]# mount
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
2、用 dumpe2fs 命令查看 /dev/sda3 的超级块和块信息
[root@localhost ~]# dumpe2fs -h /dev/sda3
dumpe2fs 1.41.12 (17-May-2010)
Filesystem volume name: <none>
Last mounted on: /
Filesystem UUID: dd1a8899-21aa-4311-a8cc-bdf51c431a88
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 1114112
Block count: 4456192
Reserved block count: 222809
Free blocks: 3786584
Free inodes: 1042757
...
3、ACL权限开启方式
[root@localhost ~]# mount -o remount,acl /
#使用 vim 查看配置文件
[root@localhost ~]# vim /etc/fstab
# /etc/fstab 文件内容
3 # /etc/fstab
4 # Created by anaconda on Sat Apr 23 09:07:24 2022
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=dd1a8899-21aa-4311-a8cc-bdf51c431a88 / ext4 defaults 1 1
10 UUID=ffc47bf0-df6b-4fa6-adf5-3099993c645f /boot ext4 defaults 1 2
11 UUID=e582af58-facf-4c09-9974-944c36c9436e swap swap defaults 0 0
12 tmpfs /dev/shm tmpfs defaults 0 0
13 devpts /dev/pts devpts gid=5,mode=620 0 0
14 sysfs /sys sysfs defaults 0 0
15 proc /proc proc defaults 0 0
#修改 /etc/fstab 文件
#将下面这条配置内容
9 UUID=dd1a8899-21aa-4311-a8cc-bdf51c431a88 / ext4 defaults 1 1
#修改成 :
9 UUID=dd1a8899-21aa-4311-a8cc-bdf51c431a88 / ext4 defaults,acl 1 1
#修改完成,保存退出
# 修改之后的 /etc/fstab 文件
2 #
3 # /etc/fstab
4 # Created by anaconda on Sat Apr 23 09:07:24 2022
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=dd1a8899-21aa-4311-a8cc-bdf51c431a88 / ext4 defaults,acl 1 1
10 UUID=ffc47bf0-df6b-4fa6-adf5-3099993c645f /boot ext4 defaults 1 2
11 UUID=e582af58-facf-4c09-9974-944c36c9436e swap swap defaults 0 0
12 tmpfs /dev/shm tmpfs defaults 0 0
13 devpts /dev/pts devpts gid=5,mode=620 0 0
14 sysfs /sys sysfs defaults 0 0
15 proc /proc proc defaults 0 0
2.3 ACL权限 基本命令
2.3.1 查看文件 ACL权限
#命令示例
[root@localhost ~]# getfacl suidcheck.sh
# file: suidcheck.sh
# owner: root
# group: root
user::rwx
group::r--
other::r--
2.3.2 给文件设定 ACL权限
举例:为 suidcheck.sh 设置 ACL权限,并删除相应权限
[root@localhost ~]# getfacl suidcheck.sh
# file: suidcheck.sh
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@localhost ~]# ll suidcheck.sh
-rw-r--r--. 1 root root 652 4月 27 14:00 suidcheck.sh
通过两种方法,查看 suidcheck.sh 都没有 ACL权限
[root@localhost ~]# setfacl -m u:root:7,g:root:5 suidcheck.sh
[root@localhost ~]# ll suidcheck.sh
-rw-rwxr--+ 1 root root 652 4月 27 14:00 suidcheck.sh
#权限最后一个字符变成“+”号,代表有了 ACL权限,需要用 getfacl命令 查看
[root@localhost ~]# getfacl suidcheck.sh
# file: suidcheck.sh
# owner: root
# group: root
user::rw-
user:root:rwx
group::r--
group:root:r-x
mask::rwx
other::r--
[root@localhost ~]# setfacl -b suidcheck.sh
[root@localhost ~]# ll suidcheck.sh
-rw-r--r--. 1 root root 652 4月 27 14:00 suidcheck.sh
[root@localhost ~]# getfacl suidcheck.sh
# file: suidcheck.sh
# owner: root
# group: root
user::rw-
group::r--
other::r--
# 还原默认权限
[root@localhost ~]# chmod u+x suidcheck.sh
[root@localhost ~]# ll suidcheck.sh
-rwxr--r--. 1 root root 652 4月 27 14:00 suidcheck.sh
2.3.3 给目录设置ACL权限
举例:在 /root 目录下,创建测试目录 test ,并在 /root/test/ 创建两个测试文件 test1.txt 和 test2.txt,给予 /root/test 目录设置 ACL权限。
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog suidcheck.sh suid.list
[root@localhost ~]# mkdir test
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog suidcheck.sh suid.list test
[root@localhost ~]# cd test
[root@localhost test]# touch test1.txt test2.txt
[root@localhost test]# ls
test1.txt test2.txt
[root@localhost test]# ll -d
drwxr-xr-x. 2 root root 4096 4月 30 18:38 .
[root@localhost test]# ll
总用量 0
-rw-r--r--. 1 root root 0 4月 30 18:38 test1.txt
-rw-r--r--. 1 root root 0 4月 30 18:38 test2.txt
# 给 /root/test 目录递归设置 ACL权限,所属组权限设置为 5,其他人设置为 5
[root@localhost test]# setfacl -m g:root:5,o:5 -R /root/test
[root@localhost test]# ll -d
drwxr-xr-x+ 2 root root 4096 4月 30 18:38 .
[root@localhost test]# getfacl /root/test
getfacl: Removing leading '/' from absolute path names
# file: root/test
# owner: root
# group: root
user::rwx
group::r-x
group:root:r-x
mask::r-x
other::r-x
[root@localhost test]# ll
总用量 8
-rw-r-xr-x+ 1 root root 0 4月 30 18:38 test1.txt
-rw-r-xr-x+ 1 root root 0 4月 30 18:38 test2.txt
2.3.4 给目录设置默认 ACL权限
举例:给 /root/test/ 设置默认 ACL权限 555 ,设置完权限之后,创建两个测试文件 test4.txt 和 test5.txt 验证。
[root@localhost test]# pwd
/root/test
[root@localhost test]# setfacl -m d:u:root:5,d:g:root:5,d:o:5 /root/test
[root@localhost test]# getfacl .
# file: .
# owner: root
# group: root
user::rwx
group::r-x
group:root:r-x
mask::r-x
other::r-x
default:user::rwx
default:user:root:r-x
default:group::r-x
default:group:root:r-x
default:mask::r-x
default:other::r-x
[root@localhost test]# touch test4.txt test5.txt
[root@localhost test]# ls
test1.txt test2.txt test3.txt test4.txt test5.txt
[root@localhost test]# ll
总用量 16
-rw-r-xr-x+ 1 root root 0 4月 30 18:38 test1.txt
-rw-r-xr-x+ 1 root root 0 4月 30 18:38 test2.txt
-rw-r--r--. 1 root root 0 4月 30 19:01 test3.txt
-rw-r--r--+ 1 root root 0 4月 30 19:04 test4.txt
-rw-r--r--+ 1 root root 0 4月 30 19:04 test5.txt
[root@localhost test]# getfacl test4.txt
# file: test4.txt
# owner: root
# group: root
user::rw-
user:root:r-x #effective:r--
group::r-x #effective:r--
group:root:r-x #effective:r--
mask::r--
other::r--
通过验证 test4.txt 文件确实赋予了默认 ACL权限
2.4 最大有效权限 mask
举例、给 /root/test 目录设置 最大有效权限
[root@localhost test]# pwd
/root/test
[root@localhost test]# setfacl -m m:7 /root/test
[root@localhost test]# getfacl .
# file: .
# owner: root
# group: root
user::rwx
group::r-x
group:root:r-x
mask::rwx
other::r-x
default:user::rwx
default:user:root:r-x
default:group::r-x
default:group:root:r-x
default:mask::r-x
default:other::r-x
2.5 删除 ACL权限
[root@localhost ~]# setfacl -x u:root /root
举例:删除 /root 目录下 test 子目录的所有 ACL权限
[root@localhost ~]# setfacl -b test
[root@localhost ~]# ll -d /root/test
drwxr-xr-x. 2 root root 4096 4月 30 19:04 /root/test
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
三、小结
初学Linux,有不对之处,欢迎大神指正,我会及时修正自己的内容,鞠躬,拜谢!