Linux权限管理
编辑
5
2025-10-06

Linux权限管理
记录一下Linux权限相关的内容,基本文件权限、扩展文件权限、特殊文件权限。
一、基本文件权限
1.1 基本文件权限概述
[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 ~]#
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 基本文件权限的修改
- 字符型方式修改.
chmod 操作对象、操作符、权限 文件
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
可执行、可进入文件夹
# 增加权限前
[root@centos7 ~]# ls -l anaconda-ks.cfg
-rw------- 1 root root 1492 Sep 28 16:07 anaconda-ks.cfg
[root@centos7 ~]#
# 增加权限x
[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 属主、属组的修改
- 修改属主、数组
chown 属主:属组 文件
chown 属主.属组 文件
# 修改前
[root@centos7 ~]# ls -l anaconda-ks.cfg
-rw-rw-r-- 1 root root 1492 Sep 28 16:07 anaconda-ks.cfg
# 修改属主、属组 root ----> qiankong
[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 ~]#
- 修改属组
chgrp 属组 文件
# 修改前
[root@centos7 ~]# ls -l anaconda-ks.cfg
-rw-rw-r-- 1 root root 1492 Sep 28 16:07 anaconda-ks.cfg
# 修改属组 root ----> qiankong
[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 查看文件的扩展权限
lsattr 文件
[root@centos7 ~]# lsattr anaconda-ks.cfg
---------------- anaconda-ks.cfg
[root@centos7 ~]#
2.2 修改文件的扩展权限
# 添加权限
chattr +权限符 文件
# 去掉权限
chattr -权限符 文件
2.3 特殊属性示范
2.3.1 仅可追加 a
# 新建一个文件,可以追加写,可以覆盖写
[root@centos7 ~]# touch filea
[root@centos7 ~]# lsattr filea
---------------- filea
[root@centos7 ~]#
[root@centos7 ~]# echo "111" > filea
[root@centos7 ~]# echo "111" >> filea
[root@centos7 ~]#
# 添加扩展权限a
[root@centos7 ~]# chattr +a filea
[root@centos7 ~]# lsattr filea
-----a---------- filea
[root@centos7 ~]#
# 添加扩展权限a后,无法覆盖写
[root@centos7 ~]# echo "111" > filea
-bash: filea: Operation not permitted
[root@centos7 ~]#
# 添加扩展权限a后,可以追加写
[root@centos7 ~]# echo "111" >> filea
[root@centos7 ~]# cat filea
111
111
111
[root@centos7 ~]#
2.3.2 不可修改 i
# 创建普通文件,可写
[root@centos7 ~]# touch filei
[root@centos7 ~]# lsattr filei
---------------- filei
[root@centos7 ~]# echo helloworld > filei
[root@centos7 ~]# echo helloworld >> filei
[root@centos7 ~]#
# 添加扩展权限i
[root@centos7 ~]# chattr +i filei
[root@centos7 ~]# lsattr filei
----i----------- filei
[root@centos7 ~]#
# 添加扩展权限i后,不可写
[root@centos7 ~]# echo hello > filei
-bash: filei: Permission denied
[root@centos7 ~]# echo hello >> filei
-bash: filei: Permission denied
[root@centos7 ~]#
2.3.3 不更新访问时间 A
# 创建文件,使用cat,发现访问时间会变化
[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 ~]#
# 添加扩展权限 A
[root@centos7 ~]# chattr +A fileA
[root@centos7 ~]# lsattr fileA
-------A-------- fileA
# 使用cat 查看文件,发现确实不改变访问时间
[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 压缩存储 c
c
[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
的位置,所以会使用大小写来区分是否有可执行权限。
- 小写,代表有
x
- 大写,代表没有
x
3.1 SUID
# 添加SUID权限
chmod u+s 文件
# 删除SUID权限
chmod u-s 文件
# 添加SUID权限前
[root@centos7 ~]# ls -l /usr/bin/cat
-rwxr-xr-x. 1 root root 54080 Aug 20 2019 /usr/bin/cat
[root@centos7 ~]#
# 添加SUID权限
[root@centos7 ~]# chmod u+s /usr/bin/cat
# 添加SUID权限后
[root@centos7 ~]# ls -l /usr/bin/cat
-rwsr-xr-x. 1 root root 54080 Aug 20 2019 /usr/bin/cat
[root@centos7 ~]#
# cat 没有SUID时
[qiankong@centos7 ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[qiankong@centos7 ~]$
# cat 有SUID时
[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
的情况。
# 开始是小写s
[root@centos7 ~]# ls -l /usr/bin/cat
-rwsr-xr-x. 1 root root 54080 Aug 20 2019 /usr/bin/cat
[root@centos7 ~]#
# 去掉x 权限
[root@centos7 ~]# chmod u-x /usr/bin/cat
# 变成大写S
[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
# 添加SGID权限
chmod g+s 文件夹
# 删除SUID权限
chmod g-s 文件夹
# 创建文件夹、修改属组
[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 ~]#
# 添加SGID权限
[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
准备环境
# 新建两个用户
[root@centos7 ~]# useradd alice
[root@centos7 ~]# useradd bob
[root@centos7 ~]#
# 加入同一个IT组
[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 ~]#
# 创建一个IT组的文件夹
[root@centos7 ~]# mkdir /opt/ITshare
[root@centos7 ~]# chown :IT /opt/ITshare/
[root@centos7 ~]#
在共享目录下,没有SBIT权限,用户可删除其余用户的文件。
# ,设置组内成员可w
[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 ~]#
# 使用alice用户创建文件
[root@centos7 ~]# su alice
[alice@centos7 root]$ cd /opt/ITshare/
[alice@centos7 ITshare]$ touch alice.txt
[alice@centos7 ITshare]$
# 切换到bob,发现可以删除alice的文件
[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权限,用户仅可删除自己的文件。
# 设置SBIT权限
[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 ~]#
# 切换到alice,创建新文件
[root@centos7 ~]# su alice
[alice@centos7 root]$ cd /opt/ITshare/
[alice@centos7 ITshare]$ touch alice.txt
[alice@centos7 ITshare]$
# 切换到bob,发现不可以删除alice的文件
[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’? y
rm: cannot remove ‘alice.txt’: Operation not permitted
[bob@centos7 ITshare]$
四、封面图
- 0
- 0
-
分享