权限的支持程度与文件系统有关,一些文件系统不一定完整支持所有的权限配置(尤其是隐藏属性)。
执行对应的语句前,请确认文件系统支持。
本章的案例仅确保在 ext4、xfs 文件系统上能够运行。
权限 | 读 | 写 | 执行 |
---|---|---|---|
字母 | r ead |
w rite |
ex ecute |
十进制数字 | 4 |
2 |
1 |
二进制数字 | 100 |
010 |
001 |
组件 | 内容 | 替代对象 | 读 | 写 | 执行 |
---|---|---|---|---|---|
文件 | 详细数据 | 文件夹 | 读到文件内容 | 修改文件内容 | 执行文件内容 |
目录 | (其下的) 文件名 | 可分类抽屉 | 读到文件名 | 修改文件名 | 进入该目录 |
三位十进制数字。
从左到右,三位分别代表以下人员的权限:
root 不受影响。
常见:777
、775
。
现有目录:
drwxr--r-- 2 root root 4096 May 23 16:20 root_dir/
该目录下有文件:
-rw-r--r-- 1 root root 22 May 23 16:20 test.txt
该目录的上级目录:
drwxrwxr-x 3 foxconn foxconn 4096 May 23 16:43 ./
当前用户信息:
uid=1000(foxconn) gid=1000(foxconn) groups=1000(foxconn),4(adm)
则该用户:
foxconn@ding-server:~/p$ cd root_dir/
-bash: cd: root_dir/: Permission denied # 不能进入
foxconn@ding-server:~/p$ ll root_dir/
ls: cannot access 'root_dir/..': Permission denied
ls: cannot access 'root_dir/.': Permission denied
ls: cannot access 'root_dir/test.txt': Permission denied
total 0
d????????? ? ? ? ? ? ./
d????????? ? ? ? ? ? ../
-????????? ? ? ? ? ? test.txt # 仅能够列出文件名
foxconn@ding-server:~/p$ cat root_dir/test.txt
cat: root_dir/test.txt: Permission denied # 不能查看文件内容
foxconn@ding-server:~/p$ mv root_dir foxconn_dir
# 能够重命名目录
foxconn@ding-server:~/p$ mv foxconn_dir/test.txt foxconn_dir/t.txt
mv: failed to access 'foxconn_dir/t.txt': Permission denied # 不能重命名文件
foxconn@ding-server:~/p$ cp -a foxconn_dir/ root_dir
cp: cannot stat 'foxconn_dir/test.txt': Permission denied
# 不能复制
# 但是存在想要复制到的目录,不过权限未复制过去
foxconn@ding-server:~/p$ ll
...
drwxr--r-- 2 root root 4096 May 23 16:20 foxconn_dir/
drwxr--r-- 2 foxconn foxconn 4096 May 23 16:20 root_dir/
foxconn@ding-server:~/p$ mv foxconn_dir/ root_dir/
mv: cannot move 'foxconn_dir/' to 'root_dir/foxconn_dir': Permission denied
# 不能移动
修改目录的权限为:
d--------- 2 root root 4096 May 23 16:20 root_dir/
其余不变,即:
该目录下有文件:
-rw-r--r-- 1 root root 22 May 23 16:20 test.txt
该目录的上级目录:
drwxrwxr-x 3 foxconn foxconn 4096 May 23 16:43 ./
当前用户信息:
uid=1000(foxconn) gid=1000(foxconn) groups=1000(foxconn),4(adm)
则该用户:
修改目录的权限为:
drwx--x--x 2 root root 4096 May 23 16:20 root_dir/
其余不变,即:
该目录下有文件:
-rw-r--r-- 1 root root 22 May 23 16:20 test.txt
该目录的上级目录:
drwxrwxr-x 3 foxconn foxconn 4096 May 23 16:43 ./
当前用户信息:
uid=1000(foxconn) gid=1000(foxconn) groups=1000(foxconn),4(adm)
则该用户:
修改目录的权限为:
drwx--x-wx 2 root root 4096 May 23 16:20 root_dir/
其余不变,即:
该目录下有文件:
-rw-r--r-- 1 root root 22 May 23 16:20 test.txt
该目录的上级目录:
drwxrwxr-x 3 foxconn foxconn 4096 May 23 16:43 ./
当前用户信息:
uid=1000(foxconn) gid=1000(foxconn) groups=1000(foxconn),4(adm)
则该用户:
现有目录:
-rwx------ 1 root root 18 May 23 17:10 root_test.txt*
该文件所在目录:
drwxrwxr-x 3 foxconn foxconn 4096 May 23 16:43 ./
当前用户信息:
uid=1000(foxconn) gid=1000(foxconn) groups=1000(foxconn),4(adm)
则该用户能否删除该文件?
为什么?
foxconn
对文件所在目录有 x
权限
前例不能删除目录的原因
目录下有文件,而 foxconn
对文件所在目录无 x
权限,故无法删除文件,进而无法删除目录
现有以下目录和文件:
/dir1/file1
/dir2
操作 | /dir1 |
/dir1/file1 |
/dir2 |
重点 |
---|---|---|---|---|
读取 file1 内容 |
x |
r |
要能够进入 /dir1 才能读到里面的文件数据 |
|
修改 file1 内容 |
x |
rw |
能够进入 /dir1 且修改 file1 |
|
执行 file1 内容 |
x |
rx |
能够进入 /dir1 且 file1 能够执行才行 |
|
删除 file1 |
wx |
能够进入 /dir1 具有目录修改的权限即可 |
||
将 file1 复制到 /dir2 |
x |
r |
wx |
能够读 file1 且能够修改 /dir2 内的数据 |
chmod
- 文件权限设置执行条件:所有者或 root 用户。
命令 | 参数 1 | 参数 2 | 参数 3 | |||
---|---|---|---|---|---|---|
chmod |
-R (包括其下的文件) |
u (所有者) |
+ (加上) |
r |
(多个部分用, 隔开) |
路径 |
g (用户组) |
w |
|||||
o (其他用户) |
- (减去) |
x |
||||
a (所有人,单用可省略) |
s |
|||||
= (设置为) |
t |
|||||
3(或 4)位十进制数字 | ||||||
--reference=文件 (参照给定文件的权限配置) |
# 对特定文件,给用户组内用户添加读写权限
chmod g+rw 文件
# 对特定目录,所有者的权限设置为可读可写不可执行,取消用户组内用户写的权限,给其他用户读的权限;且该目录下所有文件也是如此
chmod -R u=rw,g-w,o+r 目录
# 对特定文件,全部用户可读可写可执行
chmod -R a=rwx 文件 # a 可以省略
# 或
chmod -R 777 文件
chown
- 更改所有者需要 root 权限。
chown [-R] 用户名[:用户组名] 路径
选项:
-R
:递归修改:连同目录下文件一并修改chgrp
- 更改所属用户组普通用户可以把自己所属文件改成自己所属的用户组;root 可以更改成任何有效用户组。
chgrp [-R] 用户组名 路径
选项:
-R
:递归修改:连同目录下文件一并修改仅对二进制程序文件有效。
执行者执行(前提是有 x
权限)该程序时,具有该程序的所有者的权限。
如原先文件所有者有 x
权限,则为 s
;否则为 S
(此时实际上无法执行)。
-rwsr-xr-x 1 root root 68208 Mar 14 16:26 /usr/bin/passwd*
-rwSrw-rw- 1 root docker 16696 May 23 14:43 test
用于目录和二进制程序。
二进制程序:执行者执行(前提是有 x
权限)该程序时,具有该程序的用户组的权限。
目录:
rx
权限)如原先文件用户组有 x
权限,则为 s
;否则为 S
(此时实际上无法进入 / 执行)
-rwx--s--x. 1 root slocate 40K Apr 11 2018 /usr/bin/locate*
-rw-rwSrw- 1 root docker 16696 May 23 14:43 test
用于目录。
用户在该目录下新建的文件,仅有自己和 root 可以删除、更名、移动(前提是用户对该目录有 wx
权限)。
如原先文件其他用户有 x
权限,则为 t
;否则为 T
。
drwxrwxrwt 14 root root 49152 May 24 11:07 tmp/
-rw-rw-rwT 1 root docker 16696 May 23 14:43 test
权限 | SUID | SGID | SBIT |
---|---|---|---|
字母 | s |
s |
t |
十进制数字 | 4 |
2 |
1 |
二进制数字 | 100 |
010 |
001 |
特殊权限的数字在普通权限之前(这时为四位)。
如果特殊权限为 0,可省略(这时为三位)。
chmod
与文件特殊权限几例# 对特定文件,添加 SUID
chmod u+s 文件
# 对特定目录,去掉 SGID
chmod g-s 目录
# 对特定目录,设置 SBIT,用户可读写、执行,用户组和其他用户可读、执行
chmod u=rwx,g=rx,o=rxt 目录 # o 的 x 不可省略
# 或
chmod 1755 目录
umask
- 文件默认权限umask
的值作用范围为当前会话和子进程。
foxconn@ding-server:~/p$ umask
0002
foxconn@ding-server:~/p$ umask -S
u=rwx,g=rwx,o=rx
数字与权限的关系如下:
# 数字与权限的关系
0 0 0 2 # umask 数字
-------w- # umask 对应权限
rwxrwxrwx # 全部权限
- -------w- # umask 对应权限
------------ # 相减
rwxrwxr-x # umask -S 对应的权限
如 umask
为 0000
:
rwxrwxrwx
(0777
)rw-rw-rw-
(0666
)umask
值是在此之上,默认减掉的值。
普通用户默认为 0002
,root 默认为 0022
,可配置。
foxconn@ding-server:~/p$ mkdir umask_dir
foxconn@ding-server:~/p$ touch umask_file
foxconn@ding-server:~/p$ ll
...
drwxrwxr-x 2 foxconn foxconn 4096 May 24 14:29 umask_dir/
-rw-rw-r-- 1 foxconn foxconn 0 May 24 14:29 umask_file
# 目录权限
rwxrwxrwx # umask 为 0000 时,目录默认权限
- -------w- # 普通用户默认 umask
------------ # 相减
rwxrwxr-x # 普通用户默认目录权限
# 文件权限
rw-rw-rw- # umask 为 0000 时,文件默认权限
- -------w- # 普通用户默认 umask
------------ # 相减
rw-rw-r-- # 普通用户默认文件权限
设置时,第一位的 0
可以省略。
foxconn@ding-server:~/p$ umask 037
foxconn@ding-server:~/p$ mkdir umask_dir
foxconn@ding-server:~/p$ touch umask_file
foxconn@ding-server:~/p$ ll
...
drwxr----- 2 foxconn foxconn 4096 May 24 14:57 umask_dir/
-rw-r----- 1 foxconn foxconn 0 May 24 14:57 umask_file
# 讲解
0 0 3 7 # umask 数字
----wxrwx # umask 对应权限
rwxrwxrwx # umask 为 0000 时,目录默认权限
- ----wxrwx # umask 对应权限
------------ # 相减
rwxr----- # 目录默认权限
rw-rw-rw- # umask 为 0000 时,文件默认权限
- ----wxrwx # umask 对应权限
------------ # 相减
rw-r----- # 文件默认权限
Linux 中,文件有三个时间:
用 stat
命令可查看这三个参数;用 touch
命令可强制更新这些时间。
foxconn@ding-server:~/p/attr$ stat a.txt
File: a.txt
Size: 14 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 4988693 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ foxconn) Gid: ( 1000/ foxconn)
Access: 2022-05-24 15:25:37.575181675 +0800
Modify: 2022-05-24 15:25:34.159183936 +0800
Change: 2022-05-24 15:25:34.159183936 +0800
Birth: -
foxconn@ding-server:~/p/attr$ touch a.txt
foxconn@ding-server:~/p/attr$ stat a.txt
File: a.txt
Size: 14 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 4988693 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ foxconn) Gid: ( 1000/ foxconn)
Access: 2022-05-25 11:04:35.731137469 +0800
Modify: 2022-05-25 11:04:35.731137469 +0800
Change: 2022-05-25 11:04:35.731137469 +0800
Birth: -
lsattr
- 查看文件隐藏属性不支持符号链接的属性查看:
foxconn@ding-server:~/p/attr$ lsattr
lsattr: Operation not supported While reading flags on ./ori_link
--------------e----- ./ori
s-S-iadAc-----e----- ./attrtest
选项:
-a
:显示隐藏文件的属性-d
:如果后接目录,则只显示目录的属性,而非目录下文件的属性-R
:连同目录下的文件的属性一齐显示chattr
- 设置文件隐藏属性chattr [+-=]属性字母 路径
+
:增加属性-
:删除属性=
:设为属性a
- 只能增加数据设置该属性后,文件只能增加数据,而不能修改、删除数据。
只有 root 可以添加该属性。
适用于一些日志文件。
foxconn@ding-server:~/p/attr$ cat > a.txt << EOF
> line 1
> EOF
foxconn@ding-server:~/p/attr$ sudo chattr +a a.txt
foxconn@ding-server:~/p/attr$ rm a.txt
rm: cannot remove 'a.txt': Operation not permitted
foxconn@ding-server:~/p/attr$ sudo rm a.txt
rm: cannot remove 'a.txt': Operation not permitted
foxconn@ding-server:~/p/attr$ cat > a.txt << EOF
> line 1
> EOF
-bash: a.txt: Operation not permitted
foxconn@ding-server:~/p/attr$ cat >> a.txt << EOF
> line 1
> EOF
foxconn@ding-server:~/p/attr$ cat a.txt
line 1
line 1
i
- 锁定文件设置该属性后,文件不能修改、删除;也不能通过建立的链接进行这些操作。
只有 root 可以添加该属性。
foxconn@ding-server:~/p/attr$ cat > i.txt << EOF
> line 1
> EOF
foxconn@ding-server:~/p/attr$ sudo chattr +i i.txt
foxconn@ding-server:~/p/attr$ sudo rm i.txt
rm: cannot remove 'i.txt': Operation not permitted
foxconn@ding-server:~/p/attr$ sudo cat > i.txt < a.txt
-bash: i.txt: Operation not permitted
foxconn@ding-server:~/p/attr$ sudo cat >> i.txt < a.txt
-bash: i.txt: Operation not permitted
foxconn@ding-server:~/p/attr$ ln -s i.txt ilink
foxconn@ding-server:~/p/attr$ sudo cat >> ilink < a.txt
-bash: ilink: Operation not permitted
可以查看是否有隐藏属性;如果有,去掉再删。
A
- 不改变 atime设置该属性后,访问该文件,不会改变 atime(Access time,访问时间),避免 IO 较慢的机器过度读写磁盘。
用 touch 仍然会改变。
foxconn@ding-server:~/p/attr$ cat > A.txt < a.txt
foxconn@ding-server:~/p/attr$ cat > ori.txt < A.txt
foxconn@ding-server:~/p/attr$ chattr +A A.txt
foxconn@ding-server:~/p/attr$ stat -c %x A.txt ori.txt
2022-05-24 15:48:07.956174468 +0800
2022-05-24 15:48:07.952174488 +0800
foxconn@ding-server:~/p/attr$ cat > A.txt < a.txt
foxconn@ding-server:~/p/attr$ cat > ori.txt < a.txt
foxconn@ding-server:~/p/attr$ cat A.txt ori.txt > /dev/null
foxconn@ding-server:~/p/attr$ stat -c %x A.txt ori.txt
2022-05-24 15:48:07.956174468 +0800
2022-05-24 15:49:48.167698132 +0800
foxconn@ding-server:~/p/attr$ touch A.txt ori.txt
foxconn@ding-server:~/p/attr$ stat -c %x A.txt ori.txt
2022-05-24 15:50:07.567611933 +0800
2022-05-24 15:50:07.567611933 +0800
实际上,为了减少 atime 的频繁读写,一般会在挂载分区时,设置 relatime
属性,在以下条件都满足的情况下才会更新 atime:
此外还有 noatime
(禁止更新文件及目录的 atime)、nodiratime
(禁止更新目录的 atime)属性可选。
foxconn@ding-server:~/p/attr$ mount
...
/dev/mapper/ubuntu--vg-ubuntu--lv on / type ext4 (rw,relatime)
...