Linux权限管理小记

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   # a表示所有


chmod u-x file  # 给属主位置去掉执行权限
chmod o-w file  # 给其他用户去掉写的权限
chmod go-x file # 给属组和其他用户去掉执行权限
chmod ugo-x file # 给所有位置取消执行权限
chmod -x  file   # 默认表示所有位置
chmod a-x file   # a表示所有

[root@centos ~]#chmod o-rw test.txt 
[root@centos ~]#ll test.txt
-rw-rw----. 1 root root 20 Mar 11 12:24 test.txt



= 先清空后赋值
[root@centos ~]#chmod u=rx test.txt 
[root@centos ~]#ll test.txt 
-r-xr-xr-x. 1 root root 20 Mar 11 12:24 test.txt
[root@centos ~]#chmod ugo=rw test.txt 
[root@centos ~]#ll test.txt
-rw-rw-rw-. 1 root root 20 Mar 11 12:24 test.txt




案例1: 修改test.txt属主为rwx权限
[root@centos ~]#ll test.txt 
-rw-r--r--. 1 root root 20 Mar 11 12:24 test.txt
[root@centos ~]#chmod u+x test.txt 
[root@centos ~]#ll test.txt
-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 ~]#chmod 655 test.txt 
[root@centos ~]#ll test.txt
-rw-r-xr-x. 1 root root 20 Mar 11 12:24 test.txt

授权test文件755
[root@centos ~]#chmod 755 test.txt 
[root@centos ~]#ll test.txt
-rwxr-xr-x. 1 root root 20 Mar 11 12:24 test.txt

授权test文件600权限
[root@centos ~]#chmod 600 test.txt
[root@centos ~]#ll test.txt
-rw-------. 1 root root 20 Mar 11 12:24 test.txt

chown 修改属主属组
chmod 修改文件权限

4.rwx权限对于文件的作用

4.1.创建测试文件

创建测试文件:
[root@centos ~]#mkdir /test
[root@centos ~]#cd /test/
[root@centos /test]#touch {1..2}.txt
[root@centos /test]#ll
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]#chown test.test *.txt
[root@centos /test]#ll
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 ~]#su - test
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]#chmod u=r 1.txt
[root@centos /test]#ll 1.txt
-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]#chmod u=w 1.txt
[root@centos /test]#ll 1.txt
--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]#chmod u=x 1.txt
[root@centos /test]#ll
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]#chown test.test /test
[root@centos /test]#ll -d /test
drwxr-xr-x. 2 test test 32 Mar 14 10:40 /test

5.2.r权限对于目录的作用

[root@centos /test]#chmod u=r /test
[root@centos /test]#ll -d /test
dr--r-xr-x. 2 test test 32 Mar 14 10:40 /test
1.不能进入目录
2.可以看到文件名称 但是看不到文件的详细信息
3.不能删除 不能创建 不能改名
4.不能查看文件内容

5.3.w权限对于目录的作用

[root@centos ~]#ll -d /test
dr--r-xr-x. 2 test test 32 Mar 14 11:00 /test
[root@centos ~]#chmod u=w /test
[root@centos ~]#ll -d /test
d-w-r-xr-x. 2 test test 32 Mar 14 11:00 /test
1.不能进入
2.不能查看
3.不能创建 不能删除

5.4.x权限对于目录的作用

X权限对于目录的作用
[root@centos ~]#chmod u=x /test/
[root@centos ~]#ll -d /test/
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~]# chmod +s /usr/bin/rm
# 给命令提权,提权以后所有用户使用此命令(rm)都相当于root

切换到普通用户测试


[root@centos ~]# chmod -s /usr/bin/rm
改回来

t 粘滞位 控制目录的权限只能管理自己的文件
chmod o+t
chmod 777 /data
# chmod 1777 /data 加上粘滞位权限
posted @ 2024-01-20 21:41  只为心情愉悦  阅读(19)  评论(0编辑  收藏  举报