Linux权限管理 记录一下Linux权限相关的内容,基本文件权限、扩展文件权限、特殊文件权限。
一、基本文件权限 1.1 基本文件权限概述 1 2 3 4 5 6 7 8 [root@centos7 ~]# ll total 544 -rw------- 1 root root 1492 2025-09-28 16:07 anaconda-ks.cfg drwxr-xr-x 2 root root 38 2025-10-05 14:04 dir1 -rw-r--r-- 1 root root 575 2025-10-05 13:38 init.sh -rw-r--r-- 1 root root 543132 2021-01-18 21:09 nginx.rpm -rw------- 1 root root 3471 2025-10-04 22:59 sshd_config [root@centos7 ~]#
1 2 3 4 5 6 7 8 9 10 11 drwxr-xr-x 2 root root 38 2025-10-05 14:04 dir1 │ │ | | │ │ │ │ │ └ 文件名 │ │ | | │ │ │ │ └ 修改时间 │ │ | | │ │ │ └ 文件大小(字节) │ │ | | │ │ └ 所属组 │ │ | | │ └ 所属用户 │ │ | | └ 硬链接数 │ | | └ 权限(rwx、其他人) │ | └ 权限(rwx、属组) │ └ 权限(rwx、属主) └ 文件类型(- 文件, d 目录, l 链接 …)
基本文件权限一共占9位,对属主、属组、其他人分别有 r w x 三种权限。
文件
文件夹
二进制数字
十进制数字
r
可读
可列出文件列表
100
4
w
可写
可删除、创建文件
010
2
x
可执行
可进入
001
1
1.2 基本文件权限的修改
字符型方式修改.
1 2 3 4 5 6 7 8 9 chmod u+x 文件chmod g-x 文件chmod o+r 文件chmod a+x 文件chmod +x 文件chmod u=r--,g=r--,o=--- 文件chmod u=rwx 文件chmod ug=rw- 文件chmod a=r-- 文件
a 代表所有人(操作对象)
u 代表属主(操作对象)
g 代表属组(操作对象)
o 代表其他人(操作对象)
+ 添加权限(操作符)
- 减去权限(操作符)
= 赋值给权限(操作符)
r 可读、可列出文件列表
w 可写、可增删文件
x 可执行、可进入文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 [root@centos7 ~]# ls -l anaconda-ks.cfg -rw------- 1 root root 1492 Sep 28 16:07 anaconda-ks.cfg [root@centos7 ~]# [root@centos7 ~]# chmod +x anaconda-ks.cfg [root@centos7 ~]# [root@centos7 ~]# ls -l anaconda-ks.cfg -rwx--x--x 1 root root 1492 Sep 28 16:07 anaconda-ks.cfg [root@centos7 ~]#
对这部分内容很熟悉了,并且配置很灵活,案例太繁琐,跳过了。。。
注意
1.3 属主、属组的修改
修改属主、数组
1 2 chown 属主:属组 文件 chown 属主.属组 文件
1 2 3 4 5 6 7 8 9 10 11 [root@centos7 ~]# ls -l anaconda-ks.cfg -rw-rw-r-- 1 root root 1492 Sep 28 16:07 anaconda-ks.cfg [root@centos7 ~]# chown qiankong:qiankong anaconda-ks.cfg [root@centos7 ~]# ls -l anaconda-ks.cfg -rw-rw-r-- 1 qiankong qiankong 1492 Sep 28 16:07 anaconda-ks.cfg [root@centos7 ~]#
修改属组
1 2 3 4 5 6 7 8 9 10 11 [root@centos7 ~]# ls -l anaconda-ks.cfg -rw-rw-r-- 1 root root 1492 Sep 28 16:07 anaconda-ks.cfg [root@centos7 ~]# chgrp qiankong anaconda-ks.cfg [root@centos7 ~]# ls -l anaconda-ks.cfg -rw-rw-r-- 1 root qiankong 1492 Sep 28 16:07 anaconda-ks.cfg [root@centos7 ~]# =
chgrp 只能修改属组,不如 chown 好用。
二、扩展文件权限 也有一些扩展权限,可以做到一些特殊的功能。
说明
用途
a
只能追加
日志文件
i
不可修改
关键系统文件
A
不更新访问时间
读取频繁的文件,加快磁盘IO
c
压缩存储
占用空间较大的文件
2.1 查看文件的扩展权限
1 2 3 [root@centos7 ~]# lsattr anaconda-ks.cfg ---------------- anaconda-ks.cfg [root@centos7 ~]#
2.2 修改文件的扩展权限 1 2 3 4 5 chattr +权限符 文件 chattr -权限符 文件
2.3 特殊属性示范 2.3.1 仅可追加 a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [root@centos7 ~]# touch filea [root@centos7 ~]# lsattr filea ---------------- filea [root@centos7 ~]# [root@centos7 ~]# echo "111" > filea [root@centos7 ~]# echo "111" >> filea [root@centos7 ~]# [root@centos7 ~]# chattr +a filea [root@centos7 ~]# lsattr filea -----a---------- filea [root@centos7 ~]# [root@centos7 ~]# echo "111" > filea -bash: filea: Operation not permitted [root@centos7 ~]# [root@centos7 ~]# echo "111" >> filea [root@centos7 ~]# cat filea 111 111 111 [root@centos7 ~]#
2.3.2 不可修改 i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@centos7 ~]# touch filei [root@centos7 ~]# lsattr filei ---------------- filei [root@centos7 ~]# echo helloworld > filei [root@centos7 ~]# echo helloworld >> filei [root@centos7 ~]# [root@centos7 ~]# chattr +i filei [root@centos7 ~]# lsattr filei ----i----------- filei [root@centos7 ~]# [root@centos7 ~]# echo hello > filei -bash: filei: Permission denied [root@centos7 ~]# echo hello >> filei -bash: filei: Permission denied [root@centos7 ~]#
2.3.3 不更新访问时间 A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 [root@centos7 ~]# touch fileA [root@centos7 ~]# lsattr fileA ---------------- fileA [root@centos7 ~]# stat fileA File: ‘fileA’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 67193657 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2025-10-06 16:53:43.337163277 +0800 Modify: 2025-10-06 16:53:43.337163277 +0800 Change: 2025-10-06 16:53:43.337163277 +0800 Birth: - [root@centos7 ~]# cat fileA [root@centos7 ~]# stat fileA File: ‘fileA’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 67193657 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2025-10-06 16:54:10.056163550 +0800 Modify: 2025-10-06 16:53:43.337163277 +0800 Change: 2025-10-06 16:53:43.337163277 +0800 Birth: - [root@centos7 ~]# [root@centos7 ~]# chattr +A fileA [root@centos7 ~]# lsattr fileA -------A-------- fileA [root@centos7 ~]# stat fileA File: ‘fileA’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 67193657 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2025-10-06 16:54:10.056163550 +0800 Modify: 2025-10-06 16:53:43.337163277 +0800 Change: 2025-10-06 16:54:30.810163763 +0800 Birth: - [root@centos7 ~]# cat fileA [root@centos7 ~]# stat fileA File: ‘fileA’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 67193657 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2025-10-06 16:54:10.056163550 +0800 Modify: 2025-10-06 16:53:43.337163277 +0800 Change: 2025-10-06 16:54:30.810163763 +0800 Birth: - [root@centos7 ~]#
2.3.4 压缩存储 c1 2 3 4 5 6 7 [root@centos7 ~]# chattr +c bigfile chattr: Operation not supported while setting flags on bigfile [root@centos7 ~]# df -hT |grep xfs /dev/mapper/centos_centos7-root xfs 37G 2.3G 35G 6% / /dev/sda1 xfs 1014M 138M 877M 14% /boot [root@centos7 ~]#
发现不支持,c 属性属于早期 ext2/ext3 的实验功能,现代 ext4 也不默认启用。
三、特殊权限 Linux上还存在一些特殊权限,SUID、SGID、SBIT。
用途
十进制
符号
SUID
针对文件,文件执行时拥有属主的权限
4
s/S
SGID
针对文件夹,新建的文件继承目录的属组
2
s/S
SBIT
针对文件夹,只有文件所有者、目录所有者、root可以删除文件
1
t/T
符号位会占用 x 的位置,所以会使用大小写来区分是否有可执行权限。
3.1 SUID 1 2 3 4 5 chmod u+s 文件chmod u-s 文件
1 2 3 4 5 6 7 8 9 10 11 12 [root@centos7 ~]# ls -l /usr/bin/cat -rwxr-xr-x. 1 root root 54080 Aug 20 2019 /usr/bin/cat [root@centos7 ~]# [root@centos7 ~]# chmod u+s /usr/bin/cat [root@centos7 ~]# ls -l /usr/bin/cat -rwsr-xr-x. 1 root root 54080 Aug 20 2019 /usr/bin/cat [root@centos7 ~]#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 [qiankong@centos7 ~]$ cat /etc/shadow cat : /etc/shadow: Permission denied[qiankong@centos7 ~]$ [qiankong@centos7 ~]$ cat /etc/shadow root:$6$C3KUOBmSuydyrnip$XatlE2BFuX6dqoWZLqg1qTrfTN00jtE01Q3PNR8NaouRUzcxnAP2XTNLwd8gKfQ9TeUG6nuKZU2lCNFxPWXzt .::0:99999:7::: bin:*:18353:0:99999:7::: daemon:*:18353:0:99999:7::: adm:*:18353:0:99999:7::: lp:*:18353:0:99999:7::: sync :*:18353:0:99999:7:::shutdown:*:18353:0:99999:7::: halt:*:18353:0:99999:7::: mail:*:18353:0:99999:7::: operator:*:18353:0:99999:7::: games:*:18353:0:99999:7::: ftp:*:18353:0:99999:7::: nobody:*:18353:0:99999:7::: systemd-network:!!:20359:::::: dbus:!!:20359:::::: polkitd:!!:20359:::::: sshd:!!:20359:::::: postfix:!!:20359:::::: yangge:$6$z8733B2J$LghANwcyLmksdeJsTqgGE6OTEYrvwdsDilFViIQuL3T4J49Br6tOMW .3O1g6ZS04DncFzCRMXumKnCAjJ1y7o/:20359:0:99999:7::: rpc:!!:20361:0:99999:7::: rpcuser:!!:20361:::::: nfsnobody:!!:20361:::::: apache:!!:20364:::::: test004:!!:20367:0:99999:7::: test005:!!:20367:0:99999:7::: test006:!!:20367:0:99999:7::: test007:!!:20367:0:99999:7::: test008:!!:20367:0:99999:7::: test009:!!:20367:0:99999:7::: qiankong:$6$vBwPmyiQ$lWaSgzxCjsVEp5xtycyYEE1iJj5povL3g0jOr /FigbOo1aGBJwQ7yoB869lNwvzF/a.sj1/4Y4Dfyh7eLA8Wp0:20367:0:99999:7::165765: [qiankong@centos7 ~]$
验证大小写,对应 x 的情况。
1 2 3 4 5 6 7 8 9 10 11 12 [root@centos7 ~]# ls -l /usr/bin/cat -rwsr-xr-x. 1 root root 54080 Aug 20 2019 /usr/bin/cat [root@centos7 ~]# [root@centos7 ~]# chmod u-x /usr/bin/cat [root@centos7 ~]# ls -l /usr/bin/cat -rwSr-xr-x. 1 root root 54080 Aug 20 2019 /usr/bin/cat [root@centos7 ~]#
3.2 SGID 1 2 3 4 5 chmod g+s 文件夹chmod g-s 文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [root@centos7 ~]# mkdir dir1 [root@centos7 ~]# chown :OPS dir1/ [root@centos7 ~]# ls -ld dir1/ drwxr-xr-x 2 root OPS 6 Oct 6 17:39 dir1/ [root@centos7 ~]# touch dir1/file{1..3} [root@centos7 ~]# ls -l dir1/file{1..3} -rw-r--r-- 1 root root 0 Oct 6 17:39 dir1/file1 -rw-r--r-- 1 root root 0 Oct 6 17:39 dir1/file2 -rw-r--r-- 1 root root 0 Oct 6 17:39 dir1/file3 [root@centos7 ~]# [root@centos7 ~]# chmod g+s dir1/ [root@centos7 ~]# ls -ld dir1/ drwxr-sr-x 2 root OPS 45 Oct 6 17:39 dir1/ [root@centos7 ~]# [root@centos7 ~]# touch dir1/file{4..6} [root@centos7 ~]# ls -l dir1/file{4..6} -rw-r--r-- 1 root OPS 0 Oct 6 17:39 dir1/file4 -rw-r--r-- 1 root OPS 0 Oct 6 17:39 dir1/file5 -rw-r--r-- 1 root OPS 0 Oct 6 17:39 dir1/file6 [root@centos7 ~]#
3.3 SBIT 准备环境
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@centos7 ~]# useradd alice [root@centos7 ~]# useradd bob [root@centos7 ~]# [root@centos7 ~]# gpasswd -a alice IT Adding user alice to group IT [root@centos7 ~]# gpasswd -a bob IT Adding user bob to group IT [root@centos7 ~]# [root@centos7 ~]# mkdir /opt/ITshare [root@centos7 ~]# chown :IT /opt/ITshare/ [root@centos7 ~]#
在共享目录下,没有SBIT权限 ,用户可删除其余用户的文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [root@centos7 ~]# ls -ld /opt/ITshare/ drwxr-xr-x 2 root IT 6 Oct 6 17:47 /opt/ITshare/ [root@centos7 ~]# chmod g+w /opt/ITshare/ [root@centos7 ~]# ls -ld /opt/ITshare/ drwxrwxr-x 2 root IT 6 Oct 6 17:47 /opt/ITshare/ [root@centos7 ~]# [root@centos7 ~]# su alice [alice@centos7 root]$ cd /opt/ITshare/ [alice@centos7 ITshare]$ touch alice.txt [alice@centos7 ITshare]$ [root@centos7 ~]# su bob [bob@centos7 root]$ cd /opt/ITshare/ [bob@centos7 ITshare]$ ls -l total 0 -rw-rw-r-- 1 alice alice 0 Oct 6 17:51 alice.txt [bob@centos7 ITshare]$ [bob@centos7 ITshare]$ rm alice.txt rm : remove write-protected regular empty file ‘alice.txt’? y[bob@centos7 ITshare]$ ls -la total 0 drwxrwxr-x 2 root IT 6 Oct 6 17:52 . drwxr-xr-x. 5 root root 51 Oct 6 17:47 .. [bob@centos7 ITshare]$
在共享目录下,有SBIT权限 ,用户仅可删除自己的文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [root@centos7 ~]# ls -ld /opt/ITshare/ drwxrwxr-x 2 root IT 6 Oct 6 17:52 /opt/ITshare/ [root@centos7 ~]# chmod o+t /opt/ITshare/ [root@centos7 ~]# ls -ld /opt/ITshare/ drwxrwxr-t 2 root IT 6 Oct 6 17:52 /opt/ITshare/ [root@centos7 ~]# [root@centos7 ~]# su alice [alice@centos7 root]$ cd /opt/ITshare/ [alice@centos7 ITshare]$ touch alice.txt [alice@centos7 ITshare]$ [root@centos7 ~]# su bob [bob@centos7 root]$ cd /opt/ITshare/ [bob@centos7 ITshare]$ ls -l total 0 -rw-rw-r-- 1 alice alice 0 Oct 6 17:56 alice.txt [bob@centos7 ITshare]$ [bob@centos7 ITshare]$ rm alice.txt rm : remove write-protected regular empty file ‘alice.txt’? yrm : cannot remove ‘alice.txt’: Operation not permitted[bob@centos7 ITshare]$
四、封面图