文章目录
Linux 系统文件权限管理
文件系统权限介绍
文件系统权限介绍
Linux文件权限简单灵活,易于理解和应用,能够处理大多数权限使用情况。
文件有三个适用权限的用户类别:
- 单个用户拥有者,通常是创建该文件的用户。
- 单个组拥有者,通常是创建该文件的用户的主要组。
- 除了用户拥有者和组拥有者之外的其他用户。
[laoma@centos7 ~]$ ls -l myfile
-rw-r-----. 1 laoma wheel 2262 Dec 23 08:47 myfile
对于/etc/passwd文件来说,-rw-r–r–字符串分成四份,格式如下:
- 第一位代表文件类型,例如,**-**代表普通文件,d代表目录,l(L的小写)代表软链接等。
- 第二到第四位,代表user-owner具有的权限,也就是laoma用户具有的权限。
- 第五到第七位,代表group-owner具有的权限,也就是laoma组中成员具有的权限。
- 第八到第十位,代表user-owner和group-owner之外的用户具有的权限。

权限优先级
如果文件的用户是laoma,组成员中也有laoma用户,那么laoma获得最终权限是laoma用户拥有者的权限,而不是组中成员具有的权限。

rwx 权限解读
目录中保存文件,文件中保存数据(例如字符串)。
文件系统权限管理
chmod 命令
作用:更改文件不同ower权限。
语法1: chmod WhoHowWhat /path/to/file
- Who: u(user) g(group) o(other) a(all)
- How: +(添加) -(减去) =(精确设置)
- What: r(read) w(write) x(excute) -(不具有权限)
针对文件
[root@centos7 ~]# mkdir /lab
[root@centos7 ~]# cd /lab
[root@centos7 lab]# cp /etc/passwd .
# 给user增加x权限
[root@centos7 lab]# chmod u+x ./passwd
[root@centos7 lab]# ls -l passwd
-rwxr--r--. 1 root root 2312 11月 8 14:17 passwd
# 一次性设置多个
[root@centos7 lab]# chmod u-wx,g+w,o=- passwd
[root@centos7 lab]# ls -l passwd
-r--rw----. 1 root root 2312 11月 8 14:17 passwd
# 一次性设置所有对象
[root@centos7 lab]# chmod a=rwx passwd
[root@centos7 lab]# ls -l passwd
-rwxrwxrwx. 1 root root 2312 11月 8 14:17 passwd
[root@centos7 lab]# chmod a-wx passwd
[root@centos7 lab]# ls -l passwd
-r--r--r--. 1 root root 2312 11月 8 14:17 passwd
针对目录
# 针对目录,准备目录和文件
[root@centos7 lab]# mkdir dir01
[root@centos7 lab]# touch dir01/file01
[root@centos7 lab]# ls -ld dir01 dir01/file01
drwxr-xr-x. 2 root root 20 11月 8 14:22 dir01
-rw-r--r--. 1 root root 0 11月 8 14:22 dir01/file01
# 递归清除所有对象所有权限
[root@centos7 lab]# chmod -R a=- dir01
[root@centos7 lab]# ls -ld dir01 dir01/*
d---------. 2 root root 20 11月 8 14:22 dir01
----------. 1 root root 0 11月 8 14:22 dir01/file01
# 递归设置user对象权限为rwx
[root@centos7 lab]# chmod -R u+rwx dir01
[root@centos7 lab]# ls -ld dir01 dir01/*
drwx------. 2 root root 20 11月 8 14:22 dir01
-rwx------. 1 root root 0 11月 8 14:22 dir01/file01
语法2: chmod ### /path/to/file
- 第1个#,代表user权限
- 第2个#,代表group权限
- 第3个#,代表other权限
#,是一个数字范围是0(—)到7(rwx)。
补充:二进制与10进制转换
二进制 十进制 对应权限
000 0 — 无
001 1 --x 执行
010 2 -w- 写
011 3 -wx 写和执行
100 4 r-- 读
101 5 r-x 读和执行
110 6 rw- 读和写
111 7 rwx 读、写、执行
# 示例1,文件权限为 -rw- r-- r--
# 用二进制表达权限为 110 100 100,对应10进制为644
# 示例2,文件权限为 -rwx rw- r-x
# 用二进制表达权限为 111 110 101,对应10进制为765
# 文件权限为634对应的权限
[root@centos7 ~]# mkdir /lab;cd /lab
[root@centos7 lab]# cp /etc/passwd .
[root@centos7 lab]# chmod 634 passwd
[root@centos7 lab]# ls -l passwd
-rw--wxr--. 1 root root 2312 11月 8 14:17 passwd
[root@centos7 lab]# stat -c %A passwd
-rw--wxr--
[root@centos7 lab]# stat -c %a passwd
634
# 文件权限为755对应的权限
[root@centos7 lab]# chmod 755 passwd
[root@centos7 lab]# ls -l passwd
-rwxr-xr-x. 1 root root 2312 11月 8 14:17 passwd
chown chgrp 命令
作用:更改文件属主。
[root@centos7 lab]# ls -l passwd
-rwxr-xr-x. 1 root root 2312 11月 8 14:17 passwd
# 修改user owner
[root@centos7 lab]# chown laoma passwd
[root@centos7 lab]# ls -l passwd
-rwxr-xr-x. 1 laoma root 2312 11月 8 14:17 passwd
# 修改group owner
[root@centos7 lab]# chgrp wheel passwd
[root@centos7 lab]# ls -l passwd
-rwxr-xr-x. 1 laoma wheel 2312 11月 8 14:17 passwd
# 同时修改 user和group owner
[root@centos7 lab]# chown laowang:root passwd
[root@centos7 lab]# ls -l passwd
-rwxr-xr-x. 1 laowang root 2312 11月 8 14:17 passwd
# 对目录递归修改
[root@centos7 lab]# ls -R -ld dir01 dir01/*
drwxrwx---. 2 root root 20 11月 8 14:22 dir01
-rwxrwx---. 1 root root 0 11月 8 14:22 dir01/file01
# 对目录递归修改user owner
[root@centos7 lab]# chown -R laoma dir01/
[root@centos7 lab]# ls -R -ld dir01 dir01/*
drwxrwx---. 2 laoma root 20 11月 8 14:22 dir01
-rwxrwx---. 1 laoma root 0 11月 8 14:22 dir01/file01
# 对目录递归同时修改user和group owner
[root@centos7 lab]# chown -R laowang:wheel dir01/
[root@centos7 lab]# ls -R -ld dir01 dir01/*
drwxrwx---. 2 laowang wheel 20 11月 8 14:22 dir01
-rwxrwx---. 1 laowang wheel 0 11月 8 14:22 dir01/file01
案例:准备一个普通用户家目录
模拟:创建一个用户tom,该用户没有自动创建家目录
[root@centos7 ~]# useradd -M tom
准备用户家目录
[root@centos7 ~]# cp -r /etc/skel/ /home/tom
[root@centos7 ~]# ls -ld /home/tom
drwxr-xr-x. 3 root root 78 7月 22 11:52 /home/tom
[root@centos7 ~]# chmod u=rwx,go=- /home/tom
[root@centos7 ~]# ls -ld /home/tom
drwx------. 3 root root 78 7月 22 11:52 /home/tom
[root@centos7 ~]# chown -R tom:tom /home/tom
# 验证
[root@centos7 ~]# su - tom
[tom@centos7 ~]$
验证 rwx 权限-针对文件
# 初始环境准备
[root@centos7 ~]# mkdir /lab
[root@centos7 lab]# cd /lab
[root@centos7 lab]# cp /etc/hosts .
[root@centos7 lab]# chmod o=- hosts
[root@centos7 lab]# cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# 验证
[laoma@centos7 lab]$ ls -l hosts
-rw-r-----. 1 root root 158 Nov 8 14:57 hosts
[laoma@centos7 lab]$ cat hosts
cat: hosts: Permission denied
[laoma@centos7 lab]$ echo hello world > hosts
-bash: hosts: Permission denied
[laoma@centos7 lab]$ /lab/hosts
-bash: /lab/hosts: Permission denied
# r权限验证--准备
[root@centos7 lab]# chmod o=r hosts
# r权限验证
[laoma@centos7 lab]$ ls -l hosts
-rw-r--r--. 1 root root 158 Nov 8 14:57 hosts
[laoma@centos7 lab]$ cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# w权限验证--准备
[root@centos7 lab]# chmod o=w hosts
# w权限验证
[laoma@centos7 lab]$ ls -l hosts
-rw-r---w-. 1 root root 158 Nov 8 14:57 hosts
[laoma@centos7 lab]$ echo ni hao > hosts
[laoma@centos7 lab]$ cat hosts
cat: hosts: Permission denied
# 是否可以使用vim修改文件内容
[laoma@centos7 lab]$ vim hosts
# 仍然无法读取文件内容,只能覆盖修改
[laoma@centos7 lab]$ echo hello world >> hosts
[root@centos7 lab]# cat hosts
ni hao
hello world
# x权限验证--准备
[root@centos7 lab]# chmod o=x hosts
[root@centos7 lab]# echo 'echo hello world' > mycommand
[root@centos7 lab]# cat /lab/mycommand
echo hello world
[root@centos7 lab]# chmod u+x mycommand
[root@centos7 lab]# ls -l mycommand
-rwxr----x. 1 root root 17 11月 8 15:07 hosts
[root@centos7 lab]# /lab/mycommand
hello world
# x权限验证
[laoma@centos7 lab]$ /lab/mycommand
bash: /lab/hosts: Permission denied
# 原因:无法读取文件代码
[root@centos7 lab]# chmod o=rx mycommand
[laoma@centos7 lab]$ /lab/mycommand
hello world
验证 rwx 权限-针对目录
# 初始环境准备
[root@centos7 lab]# mkdir dir01
[root@centos7 lab]# mv hosts dir01
[root@centos7 lab]# chown -R root:root dir01/
[root@centos7 lab]# chmod -R a=- dir01
[root@centos7 lab]# chmod o=r dir01/hosts
[root@centos7 lab]# ls -ld dir01 dir01/*
d---------. 2 root root 19 11月 8 15:29 dir01
-------r--. 1 root root 17 11月 8 15:07 dir01/hosts
# 验证
[laoma@centos7 lab]$ ls dir01/
ls: cannot open directory dir01/: Permission denied
[laoma@centos7 lab]$ cd dir01/
-bash: cd: dir01/: Permission denied
[laoma@centos7 lab]$ touch dir01/file01
touch: cannot touch 'dir01/file01': Permission denied
# r权限验证--准备
[root@centos7 lab]# chmod o=r dir01/
# r权限验证
[laoma@centos7 lab]$ ls dir01/
hosts
[laoma@centos7 lab]$ ls -l dir01/
ls: cannot access dir01/hosts: Permission denied
total 0
-????????? ? ? ? ? ? hosts
# x权限验证--准备
[root@centos7 lab]# chmod o=x dir01/
[root@centos7 lab]# ls -ld dir01 dir01/*
d--------x. 2 root root 19 11月 8 15:29 dir01
-------r--. 1 root root 17 11月 8 15:07 dir01/hosts
# x权限验证
[laoma@centos7 lab]$ cat dir01/hosts
echo hello world
[laoma@centos7 lab]$ cd dir01/
[laoma@centos7 dir01]$ ls
ls: cannot open directory .: Permission denied
[laoma@centos7 dir01]$ cat hosts
echo hello world
# w权限验证--准备
[root@centos7 lab]# chmod o=w dir01/
[root@centos7 lab]# ls -ld dir01 dir01/*
d-------w-. 2 root root 19 11月 8 15:29 dir01
-------r--. 1 root root 17 11月 8 15:07 dir01/hosts
# w权限验证
[laoma@centos7 lab]$ touch dir01/file01
touch: cannot touch 'dir01/file01': Permission denied
[laoma@centos7 lab]$ rm dir01/hosts
rm: cannot remove 'dir01/hosts': Permission denied
# 追加x权限
[root@centos7 lab]# chmod o=wx dir01/
[root@centos7 lab]# ls -ld dir01 dir01/*
d-------wx. 2 root root 19 11月 8 15:29 dir01
-------r--. 1 root root 17 11月 8 15:07 dir01/hosts
# 体会wx效果
[laoma@centos7 lab]$ cat dir01/hosts
echo hello world
[laoma@centos7 lab]$ touch dir01/file01
[laoma@centos7 lab]$ rm dir01/hosts
rm: remove write-protected regular file 'dir01/hosts'? yes
[root@centos7 lab]# ls -ld dir01 dir01/*
d-------wx. 2 root root 20 11月 8 15:41 dir01
-rw-r--r--. 1 laoma laoma 0 11月 8 15:41 dir01/file01
权限补充说明
- 对于文件来说:
- 赋予 w 权限的时候,也会赋予 r 权限。
- 赋予 x 权限的时候,也会赋予 r 权限。
- 对于目录来说:
- 赋予 r 权限的时候,也会赋予 x 权限。
- 赋予 w 权限的时候,也会赋予 rx 权限。
文件权限总结

管理文件默认权限
umask 命令
设置创建新文件时,要取消的权限。
# 默认情况
[laoma@centos7 ~]$ mkdir lab;cd lab
[laoma@centos7 lab]$ touch f1;mkdir d1
[laoma@centos7 lab]$ ls -l
total 0
drwxr-xr-x. 2 laoma laoma 6 Nov 8 17:05 d1
-rw-r--r--. 1 laoma laoma 0 Nov 8 17:05 f1
# umask值中权限是要剔除掉的
[laoma@centos7 lab]$ umask
0022
# 设置为0
[laoma@centos7 lab]$ umask 0
[laoma@centos7 lab]$ umask
0000
# 再次创建文件
[laoma@centos7 lab]$ touch f2;mkdir d2
[laoma@centos7 lab]$ ls -ld *2
drwxrwxrwx. 2 laoma laoma 6 Nov 8 17:07 d2
-rw-rw-rw-. 1 laoma laoma 0 Nov 8 17:07 f2
# 此时目录的权限是777,文件的权限是666
# 根据用户需求定制umask值,例如希望group和other位置不具有权限
[laoma@centos7 lab]$ umask 077
[laoma@centos7 lab]$ umask
0077
[laoma@centos7 lab]$ touch f3;mkdir d3
[laoma@centos7 lab]$ ls -ld *3
drwx------. 2 laoma laoma 6 Nov 8 17:10 d3
-rw-------. 1 laoma laoma 0 Nov 8 17:10 f3
umask 持久化生效
# 针对单个用户
[laoma@centos7 lab]$ echo 'umask 077' >> ~/.bashrc
# 针对所有用户
[root@centos7 ~]# echo 'umask 077' >> /etc/bashrc
管理文件特殊权限
命令对文件能执行哪些操作,取决于执行者。

SUID 针对文件
# 普通用户执行passwd命令可以修改/etc/shadow文件原因
[laoma@centos7 ~]$ passwd
Changing password for user laoma.
Changing password for laoma.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[laoma@centos7 ~]$ ls -l /etc/shadow
----------. 1 root root 1264 Nov 8 16:38 /etc/shadow
# 查看passwd程序权限
[laoma@centos7 ~]$ ls -l $(which passwd)
-rwsr-xr-x. 1 root root 27856 Apr 1 2020 /usr/bin/passwd
# 普通用户执行passwd的命令时候,有效身份是root用户, root用户是可以修改shadow文件内容。
# suid应用
[root@centos7 ~]# chmod u+s /usr/bin/vim
[root@centos7 ~]# ls -l /usr/bin/vim
-rwsr-xr-x. 1 root root 2337216 10月 14 2020 /usr/bin/vim
# 添加suid权限
[root@centos7 ~]# chmod u+s /usr/bin/vim
[root@centos7 ~]# ls -l /usr/bin/vim
-rwsr-xr-x. 1 root root 2337216 10月 14 2020 /usr/bin/vim
# 此时普通用户就可以修改任意文件
[laoma@centos7 ~]$ vim /etc/passwd
# 删除suid权限
[root@centos7 ~]# chmod u-s /usr/bin/vim
SGID 针对目录
# 准备用户和组
[root@centos7 lab]# groupadd devops
[root@centos7 lab]# useradd -G devops dev1
[root@centos7 lab]# useradd -G devops dev2
# 准备目录
[root@centos7 lab]# mkdir webapp
[root@centos7 lab]# chgrp devops webapp
[root@centos7 lab]# chmod g=rwx webapp
# 准备默认权限
[root@centos7 lab]# echo "umask 002" >> /etc/bashrc
# 实验一:普通用户创建文件,只有自己可以编辑
[root@centos7 lab]# su dev1
[dev1@centos7 lab]$ touch webapp/dev-f1
[dev1@centos7 lab]$ ll webapp/dev-f1
-rw-rw-r--. 1 dev1 dev1 0 7月 22 15:12 webapp/dev-f1
# 实验二:普通用户创建文件,组中成员也可以编辑
[root@centos7 lab]# chmod g+s webapp
[root@centos7 lab]# su dev1
[dev1@centos7 lab]$ touch webapp/dev-f2
[dev1@centos7 lab]$ ll webapp
总用量 0
-rw-rw-r--. 1 dev1 dev1 0 7月 22 15:12 dev-f1
-rw-rw-r--. 1 dev1 devops 0 7月 22 15:13 dev-f2
[root@centos7 lab]# su dev2
[dev2@centos7 lab]$ echo hello world >> webapp/dev-f2
[dev2@centos7 lab]$ cat webapp/dev-f2
hello world
sticky 针对目录
# 示例文件
[root@centos7 lab]# ls -ld /tmp
drwxrwxrwt. 20 root root 4096 11月 8 16:50 /tmp
[root@centos7 lab]# stat -c %a /tmp
1777
[laoma@centos7 ~]$ rm /tmp/storage.log
rm: remove write-protected regular empty file '/tmp/storage.log'? yes
rm: cannot remove '/tmp/storage.log': Operation not permitted
# 用户只能删除自己创建的文件
[laoma@centos7 ~]$ touch /tmp/laoma-f1
[laoma@centos7 ~]$ ls /tmp/laoma-f1
/tmp/laoma-f1
[laoma@centos7 ~]$ rm /tmp/laoma-f1
查找系统中所有特殊权限文件
# 查找系统中所有具有suid权限的文件
[root@centos7 ~]# find / -perm -4000
# 或者
[root@centos7 ~]# find / -perm -u+s
管理文件扩展权限
需求:创建一个文件,root用户也无法编辑和删除?
解答:文件扩展属性。
[laoma@centos7 lab]$ chattr --help
Usage: chattr [-pRVf] [-+=aAcCdDeijPsStTuF] [-v version] files...
两个常用属性:
- append only (a),只能追加文件内容。
- immutable (i),不可变更属性。
append only
[root@centos7 ~]# touch /opt/operator.log
[root@centos7 ~]# chattr +a /opt/operator.log
[root@centos7 ~]# echo hello world > /opt/operator.log
-bash: /opt/operator.log: 不允许的操作
[root@centos7 ~]# echo hello world 1 >> /opt/operator.log
[root@centos7 ~]# echo hello world 2 >> /opt/operator.log
[root@centos7 ~]# rm -f /opt/operator.log
rm: 无法删除'/opt/operator.log': 不允许的操作
immutable 属性
[root@centos7 ~]# cp /etc/passwd ./passwd
[root@centos7 ~]# chattr +i passwd
[root@centos7 ~]# echo 'lw:x:1000:1000:lw:/home/lw:/bin/bash' >> passwd
-bash: passwd: 不允许的操作
[root@centos7 ~]# rm -f passwd
rm: 无法删除'passwd': 不允许的操作
[root@centos7 ~]# chattr -i passwd
[root@centos7 ~]# echo 'lw:x:1000:1000:lw:/home/lw:/bin/bash' >> passwd
# 重要的文件,内容改完后,再把i属性加回去。
[root@centos7 ~]# chattr +i passwd
管理文件访问控制列表
需求:如何给不同的用户赋予不同的权限?
解答:访问控制列表。
针对用户
# 准备文件
[root@centos7 lab]# cp /etc/passwd ./passwd
[root@centos7 lab]# chmod o=- passwd
[root@centos7 lab]# ll passwd
-rw-r-----. 1 root root 2539 7月 22 16:36 passwd
# 赋予laoma读取权限
[root@centos7 lab]# setfacl -m u:laoma:rw passwd
# 此时 group 位置对应的权限是mask权限,也就是特定用户、所有组和other用户能够获得的最大权限。
[root@centos7 lab]# ls -l passwd
-rw-rw----+ 1 root root 2539 7月 22 16:36 passwd
[root@centos7 lab]# getfacl passwd
# file: passwd
# owner: root
# group: root
user::rw-
user:laoma:rw-
group::r--
mask::rw-
other::---
# 验证
[laoma@centos7 ~]$ ll /lab/passwd
-rw-r-----+ 1 root root 2539 7月 22 16:36 /lab/passwd
[laoma@centos7 ~]$ head -n 1 /lab/passwd
root:x:0:0:root:/root:/bin/bash
# 同时设置多个规则,参照如下
[root@centos7 lab]# setfacl -m u:tom:rwx,u:laoma:r passwd
针对组
[root@centos7 lab]# setfacl -m g:wheel:rwx passwd
# 此时 mask 值变为 rwx
[root@centos7 lab]# ls -l passwd
-rw-rwx---+ 1 root root 2539 7月 22 16:36 passwd
[root@centos7 lab]# getfacl passwd
# file: passwd
# owner: root
# group: root
user::rw-
user:laoma:rw-
group::r--
group:wheel:rwx
mask::rwx
other::---
mask 设置
为了防止权限失控,最后一步设置相关用户的最大权限。
[root@centos7 lab]# setfacl -m m:- passwd
[root@centos7 lab]# ls -l passwd
-rw-------+ 1 root root 2539 7月 22 16:36 passwd
[root@centos7 lab]# getfacl passwd
# file: passwd
# owner: root
# group: root
user::rw-
user:laoma:rw- #effective:---
group::r-- #effective:---
group:wheel:rwx #effective:---
mask::---
other::---
针对目录的 acl
在具有默认acl规则的目录中创建文件,文件会继承目录的默认acl。
[root@centos7 lab]# mkdir test
[root@centos7 lab]# setfacl -m u:laoma:rw test
[root@centos7 lab]# ls -ld test
drwxrwxr-x+ 2 root root 6 7月 22 16:47 test
[root@centos7 lab]# touch test/f1
[root@centos7 lab]# ls -l test/f1
-rw-r--r--. 1 root root 0 7月 22 16:47 test/f1
# 设置目录默认 acl
[root@centos7 lab]# setfacl -m d:u:laoma:rw test
[root@centos7 lab]# getfacl test/
# file: test/
# owner: root
# group: root
user::rwx
user:laoma:rw-
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:laoma:rw-
default:group::r-x
default:mask::rwx
default:other::r-x
[root@centos7 lab]# touch test/f2
[root@centos7 lab]# ls -l test/f2
-rw-rw-r--+ 1 root root 0 7月 22 16:48 test/f2
[root@centos7 lab]# getfacl test/f2
# file: test/f2
# owner: root
# group: root
user::rw-
user:laoma:rw-
group::r-x #effective:r--
mask::rw-
other::r--

1万+

被折叠的 条评论
为什么被折叠?



