权限管理
linux权限管理介绍
主要给文件或者目录设置权限,权限对用户生效
权限:
读:r 4
写:w 2
执行:x 1
权限 U G O 3部分
rw- — —
U G O
U==文件的所有者 所属主 user
G==文件的所属组 所属组 group
O==除了文件所有者和所属组以外的其他用户 其他人 other
[root@localhost ~]# ll
total 4
-rw——-. 1 root root 1206 Aug 16 14:07 anaconda-ks.cfg
设置权限对象
chown 文件的所有者 所属组 其他人
语法格式:chown 用户.组 文件
更改文件的属主、属组
=chown:
语法结构:
chown 属主.属组 文件
-R 递归
文件:
案例:将file1文件的所属主和所属组改为hr01和hr
[root@localhost ~]# chown hr01.hr file1
案例:
将file1文件的所属主改为hr02
[root@localhost ~]# chown hr02 file1
案例:
将文件file1的所属组改为op
[root@localhost ~]# chown .op file1
目录:
目录下可以有内容(目录/文件)
将dir1这个目录以及这个目录下的所有内容所有者和所属组改为hr01和op
[root@localhost ~]# chown hr01.op dir1 -R
将dir2目录下所有内容的所有者和所属组改为hr01和op
[root@localhost ~]# chown hr01.op dir2/* -R
设置权限
chmod 使用rwx
在设置权限的时候要考虑 UGO
利用rwx设置权限
a all
增加 +
赋予 =
减少 –
将file1文件的其他人设置为读写执行的权限
-rw-r–r– 1 hr02 op 0 Aug 19 14:46 file1
[root@localhost ~]# chmod o+wx file1
[root@localhost ~]# chmod o=rwx file1
[root@localhost ~]# chmod g+wx file1
[root@localhost ~]# chmod g=rwx file1
将file1文件的所有者设置为只读权限
[root@localhost ~]# chmod u-w file1
[root@localhost ~]# chmod u=r file1
将file1文件所有者设置为读和执行,所属组设置为只读,其他人设置为只写
[root@localhost ~]# chmod u=rx,g=r,o=w file1
将file1文件的所有人设置为只读(所有者说所属组其他人权限相同的情况下)
[root@localhost ~]# chmod a=r file1
chmod数字权限
利用数字设置权限
r 表示4
w 表示2
x 表示1
rw=4+2=6
rx=4+1=5
不赋予权限表示0
r–r–r– 444
rwxr-xr-x 755
rw——- 600
案例:给file1赋予全部的权限
[root@localhost ~]# chmod 777 file1
给file1赋予643权限
[root@localhost ~]# chmod 643 file1
将file1文件所有者设置为读和执行,所属组设置为只读,其他人设置为只写
r-xr---w- 542
[root@localhost ~]# chmod 542 file1
chgrp 修改文件/目录的所属组
对文件/目录的影响
rwx对文件的影响
r:读 因为我们查看文件是用cat head tail …. (vi/vim),所以,如果没r权限,这些命令是无法查看的
w:是写的权限,vi或vim是无法往里面写东西的。但是echo可以写,不能读
例如 echo > 覆盖 >> 追加 (vi/vim)
x:执行文件,执行可以用下面的命令: / ./ (bash sh)
rwx对目录的影响
r:ls是查看目录的,但是如果没有r权限,就无法查看
w:创建和删除 如果没有w权限,就无法创建文件,创建目录,删除目录,删除目录里的文件(touch mkdir rm)
x:如果没有x,那么cd命令也是无法使用的。
高级权限管理
高级权限:
suid 4
sgid 2
sticky 1
suid4
suid 给命令文件提权(针对是文件)
当利用suid给命令文件提权后,普通用户在去执行该命令时,就拥有该命令所有者的权限
准备工作:
[root@localhost ~]# which cat
/usr/bin/cat
[root@localhost ~]# ll /usr/bin/cat
-rwxr-xr-x. 1 root root 54080 Nov 6 2016 /usr/bin/cat
[root@localhost ~]# useradd gg
[root@localhost ~]# touch /opt/gg
出现的问题:
[root@localhost ~]# su – gg
Last login: Mon Aug 22 09:20:29 CST 2022 on pts/0
[gg@localhost ~]$ cat /opt/gg
[gg@localhost ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[gg@localhost ~]$ exit
Logout
注意:这种无法查看报错的问题不能用提权的方式解决,因为用户提权只不过是把普通用户提升为管理员用户,能够执行管理员命令
[root@localhost ~]# usermod -G wheel gg
[root@localhost ~]# su – gg
Last login: Mon Aug 22 09:20:47 CST 2022 on pts/0
[gg@localhost ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[gg@localhost ~]$ exit
logout
解决方案:高级权限suid,让普通用户执行命令的时候,拥有命令文件所有者的权限
[root@localhost ~]# chmod u+s /usr/bin/cat
[root@localhost ~]# ll /usr/bin/cat
-rwsr-xr-x. 1 root root 54080 Nov 6 2016 /usr/bin/cat
[root@localhost ~]# su – gg
Last login: Mon Aug 22 09:25:56 CST 2022 on pts/0
[gg@localhost ~]$ cat /etc/shadow
sgid2
sgid:组继承权限 (针对目录)
设置了组继承权限的目录,在该目录下创建的所有内容都会继承该目录的属组
准备工作
[root@localhost ~]# mkdir /opt/dir110
[root@localhost ~]# ll /opt/dir110/
total 0
[root@localhost ~]# ll /opt/dir110/ -d
drwxr-xr-x 2 root root 6 Aug 22 09:38 /opt/dir110/
[root@localhost ~]# chown .hr /opt/dir110
[root@localhost ~]# ll /opt/dir110/ -d
drwxr-xr-x 2 root hr 6 Aug 22 09:38 /opt/dir110/
没有添加sgid权限之前:
[root@localhost ~]# touch /opt/dir110/file1
[root@localhost ~]# ll /opt/dir110/
total 0
-rw-r–r– 1 root root 0 Aug 22 09:39 file1
添加了sgid权限之后:
[root@localhost ~]# chmod g+s /opt/dir110/
[root@localhost ~]# ll /opt/dir110/ -d
drwxr-sr-x 2 root hr 19 Aug 22 09:39 /opt/dir110/
[root@localhost ~]# touch /opt/dir110/file2
[root@localhost ~]# ll /opt/dir110/
total 0
-rw-r–r– 1 root root 0 Aug 22 09:39 file1
-rw-r–r– 1 root hr 0 Aug 22 09:40 file2
sticky1
sticky:防止别人误删除 (都不限制root用户)
当多个用户向同一个目录上传文件时,只能允许用户删除自己的文件
准备工作:
[root@localhost ~]# mkdir /opt/dir120
[root@localhost ~]# chmod 777 /opt/dir120
[root@localhost ~]# su – gg
Last login: Mon Aug 22 09:33:42 CST 2022 on pts/0
[gg@localhost ~]$ touch /opt/dir120/gg
[gg@localhost ~]$ exit
logout
[root@localhost ~]# ll /opt/dir120/
total 0
-rw-rw-r– 1 gg gg 0 Aug 22 09:45 gg
没有设置sticky权限之前:
[root@localhost ~]# su – kk
Last login: Mon Aug 22 09:13:49 CST 2022 on pts/0
[kk@localhost ~]$ rm -rf /opt/dir120/gg
[kk@localhost ~]$ touch /opt/dir120/kk
[kk@localhost ~]$ exit
logout
设置了sticky权限之后:
[root@localhost ~]# chmod o+t /opt/dir120/
[root@localhost ~]# su – gg
Last login: Mon Aug 22 09:44:59 CST 2022 on pts/0
[gg@localhost ~]$ ll /opt/dir120/
total 0
-rw-rw-r– 1 kk kk 0 Aug 22 09:46 kk
[gg@localhost ~]$ rm -rf /opt/dir120/kk
rm: cannot remove ‘/opt/dir120/kk’: Operation not permitted
设置权限:
chmod u+s suid
chmod g+s sgid
chmod o+t sticky
假设无论是目录还是文件,基本权限都为777
chmod 4777
chmod 2777
chmod 1777
取消权限:
chmod u-s 文件
chmod g-s 文件
chmod o-t 文件
隐藏权限
隐藏权限 文件系统属性 (可以限制root用户)
命令:
lsattr 查看
chattr 设置
权限:
a : 可以看,可以追加,其他什么都不能干
i : 只能看,其他什么不能干
chattr +i
chattr +a
chattr -i
chattr -a
如何发现一个文件不能删除,应该如何排查? 检查以下内容:
基本权限:当该文件对应的目录具有w权限的时候,用户可以删除该文件
隐藏权限:如果给文件设置了隐藏权限(i a),root用户都无法删除
umask 掩码
权限掩码:umask
默认在创建一个目录时,目录的默认权限为755
默认在创建一个文件时,文件的默认权限为644
默认系统中权限掩码为022
如果修改了权限掩码(umask),那么默认的文件和目录的权限也会发生变化
查看umask
设置umask 值
已知条件:
文件的默认最大权限:666
目录的默认最大权限:777

已知umask可以得出文件权限
案例:umask为022,求文件的默认权限
权限:666 rw- rw- rw- 110 110 110 110 110 110
掩码:022 — -w- -w- 000 010 010 ==取反== 111 101 101
与运算:真真为真,真假为假,假假为假
真:1
假:0
案例:umask为431,求目录的默认权限
权限: 777 111 111 111 111 111 111
掩码: 431 100 011 001 ==取反== 011 100 110
注意:
只要有权限的位置就为1
没有权限的位置”-“为0
已知默认的文件权限可以的出umask
案例:要创建一个目录,目录权限为654,算出umask为多少
权限: 777 111 111 111
掩码:
目录权限: 654 110 101 100

文件:
权限: 666 110 110 110
掩码:
文件权限: 422 100 010 010
案例:需要创建一个权限为600的文件
第一种:先创建一个文件,然后直接按照更改权限的方法创建
touch 文件名
chmod 600 文件名
第二种:计算umask掩码,然后进行创建
首先文件的最高权限是666
由此得出
权限为666 rw- rw- rw- 110 110 110➡ 110 110 110
和掩码取反之后进行“与”运算得出权限数
掩码未知 --- --- --- --- --- ---➡取反进行与运算 111 000 001
⬅取反得出umask ⬅ 110 001 000
权限为600 rw- --- --- 110 000 000➡ 110 000 000
第一种umask掩码得出umask码为:
000 111 110 计算得出掩码为076
然后运行(umask 076;touch 文件名)
以此类推,根据组合出来的,可以获得其他几种掩码,都可以创建权限为600的文件
000 111 111 掩码077
(umask 077;touch 文件名)
000 110 110 掩码066
(umask 066;touch 文件名)
000 110 111 掩码067
(umask 067;touch 文件名)
001 111 110 掩码176
(umask 177;touch 文件名)
001 111 111 掩码177
(umask 177;touch 文件名)
001 110 110 掩码166
(umask 166;touch 文件名)
001 110 111 掩码167
(umask 167;touch 文件名)

到此Linux权限管理的相关问题就结束了,如果有不懂的可以评论区留言或者私信,关注博主,主页有Linux和云计算的文章,后面带你一块学习Linux和云计算的相关知识,每天更新,码博不易,点个赞或者给个关注我也有动力了,谢谢

本文详细介绍了Linux权限管理的基本概念,包括文件和目录的权限结构、chown/chmod命令的用法,以及suid、sgid和sticky等高级权限的原理和实战示例。通过实例演示了如何设置权限、修改属主和属组,以及如何理解和应用umask和隐藏权限。


被折叠的 条评论
为什么被折叠?



