Linux 小知识 文件属性 Linux 小知识 文

这是我参与11月更文挑战的第13天,活动详情查看:2021最后一次更文挑战

Linux 小知识 | 文件属性

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
bash复制代码[root@VM-8-10-centos /]# ls -al # 文件属性
total 80
# 权限
dr-xr-xr-x. 19 766777777777root root 4096 11月 5 16:14 .
dr-xr-xr-x. 19 root root 4096 11月 5 16:14 ..
-rw-r--r-- 1 root root 0 11月 26 2019 .autorelabel
lrwxrwxrwx 1 root root 7 11月 3 2020 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 9月 14 20:49 boot
drwxr-xr-x 2 root root 4096 12月 10 2019 data
drwxr-xr-x 19 root root 2900 9月 15 00:51 dev
drwxr-xr-x. 104 root root 12288 11月 3 14:18 etc
drwxr-xr-x. 8 root root 4096 11月 3 14:16 home
lrwxrwxrwx 1 root root 7 11月 3 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 11月 3 2020 lib64 -> usr/lib64
drwx------. 2 root root 16384 11月 26 2019 lost+found
drwxr-xr-x. 2 root root 4096 11月 3 2020 media
drwxr-xr-x. 2 root root 4096 11月 3 2020 mnt
drwxr-xr-x. 3 root root 4096 9月 16 10:59 opt
dr-xr-xr-x 118 root root 0 9月 15 00:51 proc
dr-xr-x---. 10 root root 4096 11月 4 10:48 root
drwxr-xr-x 31 root root 1000 11月 4 14:34 run
lrwxrwxrwx 1 root root 8 11月 3 2020 sbin -> usr/sbin
drwxr-xr-x. 2 root root 4096 11月 3 2020 srv
dr-xr-xr-x 13 root root 0 9月 15 00:51 sys
drwxrwxrwt. 5 root root 4096 11月 5 15:13 tmp
drwxr-xr-x. 15 root root 4096 9月 16 09:13 usr
drwxr-xr-x. 20 root root 4096 6月 18 11:34 var

权限

权限一共可以有10位
第一位

  • d 表示目录/文件夹
  • - 文件
  • l 链接文档

r 可读

w可写

x可执行

-没有当前权限

第二位 - 第四位

属主权限: 文件所在用户可以做什么

谁创建的, 默认属主就是谁

第五位 - 第七位

属组的权限: 文件所在的用户组可以做什么

谁创建的就是谁的用户组

第八为 - 第十位

其它用户的权限

除了属组以外的其他用户

1
2
3
4
5
bash复制代码-rwxr-xr-x
# [-] 这是一个文件
# [rwx] 属主 可读可写可以执行
# [r-x] 用户组 可读不能写可以执行
# [r-x] 其它用户 可读 不可写 可执行
文件类型 属主权限 属组权限 其它用户权限
0 1 2 3 4 5 6 7 8 9
d目录 / -文件 r 读 w 写 x 执行 r 读 - 不可写 x执行 r读 -不可写 x执行

更改文件属性

chgrp 命令 changegroup 修改所在的用户组

1
2
3
bash复制代码chgrp [选项参数] [所属群组] [文件或目录]
[root@VM-8-10-centos app]# chgrp root test
[root@VM-8-10-centos app]# chgrp -v root test # 显示提示语句

chown 更改属主 | 更改数组和属组

1
2
3
4
bash复制代码chown 属主名 文件名 # 更改属主
chown [参数选项] 属主名 : 属组名 文件名 # 更改数组和属组
# 选项参数
-R 处理指定目录以及子目录下的所有文件

示例

1
2
3
4
5
6
7
8
9
10
11
12
bash复制代码# 修改属主
[root@VM-8-10-centos app]# ls -al
drwxr-xr-x 2 root root 4096 11月 4 18:00 test
[root@VM-8-10-centos app]# chown user1 test
[root@VM-8-10-centos app]# ls -al
drwxr-xr-x 2 user1 root 4096 11月 4 18:00 test
# 修改 属主 + 属组
[root@VM-8-10-centos app]# chown root:root test
[root@VM-8-10-centos app]# ls -al
drwxr-xr-x 2 root root 4096 11月 4 18:00 test
# test 以及 test 内所有文件都改为 root
[root@VM-8-10-centos app]# chown -R root:root test

chmod 权限命令

作用: 修改属主,属组,其他用户的权限

参数选项

参数 作用
-c 若该文件权限确实已经更改,才显示其更改动作
-f 若该文件权限无法被更改也不要显示错误信息
-v 显示权限变更的详细资料
-R 对目前目录下的所有文档与子目录进行相同的权限变更(即以递归的方式逐个变更)
–help 显示帮助说明
–version 显示版本
数字权限 rwx对应数值相加的和

修改方式1: 数字方式

权限 英文 缩写 数字序号
read r 4
write w 2
执行 execute x 1
无权限 - 0
1
2
3
4
5
6
7
8
9
bash复制代码# rwx 4+2+1 = 7
# 5: 4+1 => r-x
[root@VM-8-10-centos test]# ls -l
total 4
drwxr-xr-x 2 root root 4096 11月 5 17:57 aaa
[root@VM-8-10-centos test]# chmod -R 777 aaa
[root@VM-8-10-centos test]# ls -l
total 4
drwxrwxrwx 2 root root 4096 11月 5 17:57 aaa

修改方式2: 符号方式

符号 英文 说明
u user 属主权限
g group 属组权限
o others 其他权限
a all 全部身份
+ 加入权限
- 除去权限
= 设定权限
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bash复制代码# chmod -R u=rwx,g=rx,o=r a.txt

[root@VM-8-10-centos test]# ls -l
total 4
drwxrwxrwx 2 root root 4096 11月 5 17:57 aaa
-rw-r--r-- 1 root root 0 11月 5 18:01 a.txt
[root@VM-8-10-centos test]# chmod -R u=rwx,g=rx,o=r a.txt
[root@VM-8-10-centos test]# ls -l
total 4
drwxrwxrwx 2 root root 4096 11月 5 17:57 aaa
-rwxr-xr-- 1 root root 0 11月 5 18:01 a.txt

# 去掉其他用户 a.txt的所有权限
chmod -R o-rwx a.txt
[root@VM-8-10-centos test]# chmod -R o-rwx a.txt
[root@VM-8-10-centos test]# ls -l
total 4
drwxrwxrwx 2 root root 4096 11月 5 17:57 aaa
-rwxr-x--- 1 root root 0 11月 5 18:01 a.txt

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%