1.rwx是什么?
在 Linux 中,rwx 是文件和目录的权限标记。它代表读、写和执行这三个操作。
r 表示读取权限。如果一个用户有读取权限,他可以查看文件或目录中的内容。用数字4表示
w 表示写入权限。如果一个用户有写入权限,他可以修改文件或目录中的内容。用数字2表示
x 表示执行权限。如果一个用户有执行权限,他可以运行文件或访问目录。用数字1表示
2.当前登录用户对于文件权限是什么?
1.当前用户是谁
2.用户和文件的关系
3.根据和文件的关系找到对应的权限位
用户和文件的关系总共三种:
1.属主 表示文件的主人
2.属组 表示小组
3.陌生人 其他人
当前test 用户对于hosts文件的权限是:
[test @centos ~]$ll /etc/hosts
-rw-r--r--. 1 root root 158 Mar 13 10:18 /etc/hosts
[test @centos ~]$whoami
test
九位权限位:
前三位: 主人
中三位: 属组
后三位: 陌生人
test对于hosts文件的权限是: 后三位 r-- 只读权限
3.修改文件权限
3.1.使用ugo方式修改
第一种方式: 使用ugo方式修改
属主: u 表示 user
属组: g 表示 group
其他: O 表示 other
表示所有位置的方式:
ugo+x
+x
a+x
chmod u+x file
chmod o+w file
chmod go+x file
chmod ugo+x file
chmod +x file
chmod a+x file
chmod u-x file
chmod o-w file
chmod go-x file
chmod ugo-x file
chmod -x file
chmod a-x file
[root@centos ~]
[root@centos ~]
-rw-rw----. 1 root root 20 Mar 11 12 :24 test.txt
= 先清空后赋值
[root@centos ~]
[root@centos ~]
-r-xr-xr-x. 1 root root 20 Mar 11 12 :24 test.txt
[root@centos ~]
[root@centos ~]
-rw-rw-rw-. 1 root root 20 Mar 11 12 :24 test.txt
案例1 : 修改test.txt属主为rwx权限
[root@centos ~]
-rw-r--r--. 1 root root 20 Mar 11 12 :24 test.txt
[root@centos ~]
[root@centos ~]
-rwxr--r--. 1 root root 20 Mar 11 12 :24 test.txt
3.2.使用数字方式授权
使用数字方式进行授权
r 4
w 2
x 1
案例: 授权test.txt 为rw-r-xr-x 权限
rw-r-xr-x 换算为数字: 每三位权限位相加
rw-= 4 +2 +0 =6
r-x = 4 +0 +1 =5
r-x = 4 +0 +1 =5
655 权限
[root@centos ~]
[root@centos ~]
-rw-r-xr-x. 1 root root 20 Mar 11 12 :24 test.txt
授权test文件755
[root@centos ~]
[root@centos ~]
-rwxr-xr-x. 1 root root 20 Mar 11 12 :24 test.txt
授权test文件600 权限
[root@centos ~]
[root@centos ~]
-rw-------. 1 root root 20 Mar 11 12 :24 test.txt
chown 修改属主属组
chmod 修改文件权限
4.rwx权限对于文件的作用
4.1.创建测试文件
创建测试文件:
[root@centos ~]
[root@centos ~]
[root@centos /test]
[root@centos /test]
total 0
-rw-r--r--. 1 root root 0 Mar 14 10:40 1.txt
-rw-r--r--. 1 root root 0 Mar 14 10:40 2.txt
修改属主属组为test 用户
[root@centos /test]
[root@centos /test]
total 0
-rw-r--r--. 1 test test 0 Mar 14 10:40 1.txt
-rw-r--r--. 1 test test 0 Mar 14 10:40 2.txt
第二步: 切换到test 用户进行测试
[root@centos ~]
Last login: Mon Mar 13 12:28:36 CST 2023 on pts/1
[test @centos ~]$cd /test/
[test @centos /test]$ll
total 4
-rw-r--r--. 1 test test 4 Mar 14 10:43 1.txt
-rw-r--r--. 1 test test 0 Mar 14 10:40 2.txt
4.2.r权限对于文件的作用
[root@centos /test]
[root@centos /test]
-r--r--r--. 1 test test 4 Mar 14 10:43 1.txt
1.可读
2.不可写 但是可以强制写入
3.不能执行
总结: r的权限只能查看文件
4.3.w权限对于文件的作用
W权限对于文件的作用:
修改属主为w权限
[root@centos /test]
[root@centos /test]
--w-r--r--. 1 test test 7 Mar 14 10:45 1.txt
1.不允许查看文件
2.不能写入内容到文件,可以追加内容到文件
3.不能执行
总结: w权限啥都不能干
4.3.x权限对于文件的作用
X权限对于文件的作用:
修改文件属主为x权限
[root@centos /test]
[root@centos /test]
total 4
---xr--r--. 1 test test 11 Mar 14 10:48 1.txt
1.不能读
2.不能写
3.不能执行
1.只读 给r权限即可
2.写入 给rw权限
3.执行 给rw权限
5.rwx权限对于目录的作用
5.1.创建测试目录
[root@centos /test]
[root@centos /test]
drwxr-xr-x. 2 test test 32 Mar 14 10:40 /test
5.2.r权限对于目录的作用
[root@centos /test]
[root@centos /test]
dr--r-xr-x. 2 test test 32 Mar 14 10:40 /test
1.不能进入目录
2.可以看到文件名称 但是看不到文件的详细信息
3.不能删除 不能创建 不能改名
4.不能查看文件内容
5.3.w权限对于目录的作用
[root@centos ~]
dr--r-xr-x. 2 test test 32 Mar 14 11:00 /test
[root@centos ~]
[root@centos ~]
d-w-r-xr-x. 2 test test 32 Mar 14 11:00 /test
1.不能进入
2.不能查看
3.不能创建 不能删除
5.4.x权限对于目录的作用
X权限对于目录的作用
[root@centos ~]
[root@centos ~]
d--xr-xr-x. 2 test test 32 Mar 14 11:00 /test/
1.可以进入目录
2.不能创建 不能删除 不能改名
6.隐藏权限
作用: 保护重要的系统文件 服务文件
a 只能追加文件 除了追加其他事情都不能干
i 只能看 啥都不能干
查看隐藏权限:
[root@centos ~] #ll
total 0
-rw-r--r-- . 1 root root 0 Mar 14 12 :11 1 .txt
[root@centos ~] #lsattr 1 .txt
---------------- 1 .txt
给1 .txt 设置隐藏权限a chattr +a 1 .txt
chattr +a # 设置隐藏权限a 只能追加内容到文件
chattr +i # 设置隐藏权限i 只能看
chattr -a # 取消权限
chattr -i # 取消权限
其他情况: 登录系统无法删除code .txt 没有隐藏权限
第三方安全软件限制
7.特殊权限位
特殊权限位里s 相当于4 4755
运行命令时相当于这个命令的属主的权限 设置到属主的位置
在root用户下使用命令
[root@centos~]
切换到普通用户测试
[root@centos ~]
改回来
t 粘滞位 控制目录的权限只能管理自己的文件
chmod o+t
chmod 777 /data
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构