当前位置: 首页 > news >正文

RHCSA---权限管理

权限管理

对于权限的查看,我们可以通过 ls -l 命令或者 ll 命令来查看。

权限分为以下:

1.普通权限

[root@server ~]# mkdir d1
[root@server ~]# touch f1 
[root@server ~]# ll f1
-rw-r--r--. 1 root root 0 Nov  6 16:44 f1上面的数据由以下几个部分组成:
-rw-r--r--. 	1 		root 	root 	0 		Oct 26 15:15 		f1
文件的类型和权限	文件的硬链接数	  所有者   所属组   文件大小  文件最后一次修改时间   文件名称

-rw-r–r–. 看这一部分(权限相关)

-	rw-r--r--	. 
1	2			3
1 表示文件类型- 表示普通文件d 表示目录文件l 表示链接文件c 表示字符文件s 表示套接字文件b 表示块设备文件p 表示管道文件
2 表示文件权限
3 表示文件是否有访问控制权限,如果有则为 + 号,否则为 .rw-			r--			r--
1			2			3
1 表示所有者权限,可以用字符 u 表示
2 表示所属组权限,可以用字符 g 表示
3 表示其他用户权限,可以用字符 o 表示r		w		x		-
1		2		3		4
1 表示可读,用数字 4 表示
2 表示可写,用数字 2 青示
3 表示可执行,用数字 1 表示
4 表示无权限,用数字 0 青示

1.1 chmod

对于权限设置,我们可以使用 chmod 命令。

这个命令用于修改文件的权限。语法如下:

Usage: chmod [OPTION]... MODE[,MODE]... FILE...or:  chmod [OPTION]... OCTAL-MODE FILE...or:  chmod [OPTION]... --reference=RFILE FILE...
Change the mode of each FILE to MODE.
With --reference, change the mode of each FILE to that of RFILE.-c, --changes          like verbose but report only when a change is made-f, --silent, --quiet  suppress most error messages-v, --verbose          output a diagnostic for every file processed--no-preserve-root  do not treat '/' specially (the default)--preserve-root    fail to operate recursively on '/'--reference=RFILE  use RFILE's mode instead of MODE values-R, --recursive        change files and directories recursively   递归设置,针对目录有效--help     display this help and exit--version  output version information and exitEach MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.chmod [OPTION] [ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+  FILE...ugoa:
u 表示为所有者设置权限
g 表示为所属组设置权限
o 表示为其他用户设置权限
a 表示为所有者、所属组以及其他用户同时设置权限-+=:
- 表示取消某个权限
+ 表示增加某个权限
= 如果设置的权限原本没有则增加,如果设置的权限原本有则取消0-7:
0 表示没有权限
1 表示设置可执行
2 表示设置可写
3 表示设置可写可执行
4 表示设置可读
5 表示设置可读可执行
6 表示设置可读可写
7 表示设置可读可写可执行

eg:

[root@server ~]# ll f1
-rw-r--r--. 1 root root 0 Nov  6 16:44 f11.#用+
[root@server ~]# chmod a+x f1
[root@server ~]# ll f1
-rwxr-xr-x. 1 root root 0 Nov  6 16:44 f12.#用=
[root@server ~]# chmod u=rwx,g=rw,o=r f1
[root@server ~]# ll f1
-rwxrw-r--. 1 root root 0 Nov  6 16:44 f13.#用数字
[root@server ~]# chmod 744 f1
[root@server ~]# ll f1
-rwxr--r--. 1 root root 0 Nov  6 16:44 f1

1.2 chown

这个命令用于修改文件所有者和所属组权限。语法如下:

Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...or:  chown [OPTION]... --reference=RFILE FILE...
Change the owner and/or group of each FILE to OWNER and/or GROUP.
With --reference, change the owner and group of each FILE to those of RFILE.-c, --changes          like verbose but report only when a change is made-f, --silent, --quiet  suppress most error messages-v, --verbose          output a diagnostic for every file processed--dereference      affect the referent of each symbolic link (this isthe default), rather than the symbolic link itself-h, --no-dereference   affect symbolic links instead of any referenced file(useful only on systems that can change theownership of a symlink)--from=CURRENT_OWNER:CURRENT_GROUPchange the owner and/or group of each file only ifits current owner and/or group match those specifiedhere.  Either may be omitted, in which case a matchis not required for the omitted attribute--no-preserve-root  do not treat '/' specially (the default)--preserve-root    fail to operate recursively on '/'--reference=RFILE  use RFILE's owner and group rather thanspecifying OWNER:GROUP values-R, --recursive        operate on files and directories recursivelyThe following options modify how a hierarchy is traversed when the -R
option is also specified.  If more than one is specified, only the final
one takes effect.-H                     if a command line argument is a symbolic linkto a directory, traverse it-L                     traverse every symbolic link to a directoryencountered-P                     do not traverse any symbolic links (default)--help     display this help and exit--version  output version information and exitOwner is unchanged if missing.  Group is unchanged if missing, but changed
to login group if implied by a ':' following a symbolic OWNER.
OWNER and GROUP may be numeric as well as symbolic.Examples:chown root /u        Change the owner of /u to "root".chown root:staff /u  Likewise, but also change its group to "staff".chown -hR root /u    Change the owner of /u and subfiles to "root".

eg:

1.#改文件的所有者
[root@server ~]# ll f1
-rw-r--r--. 1 root root 0 Nov  6 17:12 f1
[root@server ~]# chown zhoulanping f1
[root@server ~]# ll f1
-rw-r--r--. 1 zhoulanping root 0 Nov  6 17:12 f12.#改文件所属组
[root@server ~]# chown :zhoulanping f1
[root@server ~]# ll f1
-rw-r--r--. 1 zhoulanping zhoulanping 0 Nov  6 17:12 f13.#改文件所有者&所属组
1.#xxx:
[root@server ~]# ll -d d1
drwxr-xr-x. 2 root root 6 Nov  6 16:43 d1
[root@server ~]# chown zhoulanping: d1
[root@server ~]# ll -d d1
drwxr-xr-x. 2 zhoulanping zhoulanping 6 Nov  6 16:43 d12.#xxx:xxx
[root@server ~]# chown zhoulanping:root d1
[root@server ~]# ll -d d1
drwxr-xr-x. 2 zhoulanping root 6 Nov  6 16:43 d1
xxx: 和 xxx:xxx区别?
  • 前者只能指定所有者&所属组为同一个
  • 后者可以不同

2.特殊权限

2.1 SUID

这个特殊权限是只对可执行文件有效,它的作用是临时以文件的创建者身份来运行,运行结束后权限回收——简单来说就是临时提权

案例:将cat赋予suid权限 以查看shadow文件

/etc/shadow是 :任何用户(不包括root)都没有任何权限的(包括读)

[root@server ~]# ll /etc/shadow
----------. 1 root root 725 Nov  6 17:12 /etc/shadow

切换普通用户查看严重

[root@server ~]# su - zhoulanping 
[zhoulanping@server ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied

如果希望普通用户要吧读取这个文件中的内容,则可以为 cat 命令(cat就是可执行文件)增加 SUID 权限。

[root@server ~]# which cat
/usr/bin/cat
[root@server ~]# ll /usr/bin/cat
-rwxr-xr-x. 1 root root 36496 Dec  9  2024 /usr/bin/cat
[root@server ~]# chmod u+s /usr/bin/cat
[root@server ~]# ll /usr/bin/cat
-rwsr-xr-x. 1 root root 36496 Dec  9  2024 /usr/bin/cat
#所有者最后一位变成s

设置好后,我们再次切换普通用户,通过 cat 命令来访问:

[zhoulanping@server ~]$ cat /etc/shadow
root:$6$SUi8d9b6ohFL4Isg$aXuhKmZc4i2/53x16wPWfM4HyovS5cBLrP9LFiqRVZdt.rii53ps3QWALzikEWi9ady4m/CtK1ISvOHOMAq8f1::0:99999:7:::
bin:*:19760:0:99999:7:::
daemon:*:19760:0:99999:7:::
adm:*:19760:0:99999:7:::
lp:*:19760:0:99999:7:::
sync:*:19760:0:99999:7:::
...

可以发现现在可以访问了。

2.2 SGID

这个特殊权限是对可执行文件或者目录有效,它的作用是临时以文件或目录的所属者身份去运行,运行结束后回收。

案例1:对目录设置 SGID

作用:该目录下新建的文件继承目录的所属组

#数据准备
[root@server ~]# cd /test
[root@server test]# mkdir d1
[root@server test]# ll
total 0
drwxr-xr-x. 2 root root 6 Nov  6 17:44 d1
[root@server test]# chmod o+w d1
[root@server test]# ll -d d1
drwxr-xrwx. 2 root root 6 Nov  6 17:44 d1[root@server test]# useradd shuxinyi

我们分别增加两个会话窗口,然后登录 zhoulanping 和 shuxinyi来进行测试:

[root@server test]# su - zhoulanping 
Last login: Thu Nov  6 17:41:14 CST 2025 on pts/1
[zhoulanping@server ~]$ cd /test/d1/
[zhoulanping@server d1]$ [root@server test]# su - shuxinyi
[shuxinyi@server ~]$ cd /test/d1/
[shuxinyi@server d1]$ 

然后我们分别用这两个用户来创建 f1 和 f2 文件:

[zhoulanping@server d1]$ touch f1
[zhoulanping@server d1]$ ll f1
-rw-r--r--. 1 zhoulanping zhoulanping 0 Nov  6 17:49 f1[shuxinyi@server d1]$ touch f2
[shuxinyi@server d1]$ ll f2
-rw-r--r--. 1 shuxinyi shuxinyi 0 Nov  6 17:49 f2

从结果可以看到:文件创建好后,它的所有者和所属组都是这个用户的。

接下来我们将 /test/d1 设置 SGID 特殊权限:

[root@server test]# chmod g+s /test/d1
[root@server test]# ll -d /test/d1
drwxr-srwx. 2 root root 26 Nov  6 17:49 /test/d1
#所属组最后一位变成s

设置好权限后, 我们两次使用这两个账号来创建 f3 和 f4 文件:

[zhoulanping@server d1]$ touch f3
[zhoulanping@server d1]$ ll f3
-rw-r--r--. 1 zhoulanping root 0 Nov  6 17:53 f3[shuxinyi@server d1]$ touch f4 
[shuxinyi@server d1]$ ll f4
-rw-r--r--. 1 shuxinyi root 0 Nov  6 17:54 f4
#看到文件所属组是root,而不是用户自己的默认组。
案例2:共享日志写入工具

(对可执行文件设置sgid)

  • 有一个日志文件 /var/log/app.log,属于 loggroup
  • 只有 loggroup 组的成员才能写入这个日志文件
  • 但您希望所有用户都能通过一个工具来写日志,而不需要把所有人都加入 loggroup

作用:执行该文件时,进程会以文件所属组的身份运行,而不是执行者的身份。

1# 创建日志组
[root@server ~]# groupadd loggroup2# 创建日志文件并设置权限
[root@server ~]# touch /var/log/app.log
[root@server ~]# chown root:loggroup /var/log/app.log
[root@server ~]# chmod 664 /var/log/app.log3# 创建简单的日志脚本
[root@server ~]# cat > /usr/local/bin/writelog << 'EOF'
#!/bin/bash
echo "$(date): $USER - $*" >> /var/log/app.log
echo "日志记录成功"
EOF4# 设置脚本的SGID权限
[root@server ~]# chown root:loggroup /usr/local/bin/writelog
[root@server ~]# chmod 2755 /usr/local/bin/writelog5.#检查权限
[root@server ~]# ll /var/log/app.log
-rw-rw-r--. 1 root loggroup 69 Nov  6 21:19 /var/log/app.log
[root@server ~]# ll /usr/local/bin/writelog
-rwxr-sr-x. 1 root loggroup 85 Nov  6 21:10 /usr/local/bin/writelog6.对比
6.1#将shuxinyi用户加入用户组
[shuxinyi@server ~]$ writelog "这是一条测试日志"
日志记录成功
[shuxinyi@server ~]$ tail /var/log/app.log
Thu Nov  6 09:19:55 PM CST 2025: shuxinyi - 这是一条测试日志
[shuxinyi@server ~]$ 
#记录成功6.2#zhoulanping不加入
[zhoulanping@server ~]$ writelog "这是一条测试日志"
/usr/local/bin/writelog: line 2: /var/log/app.log: Permission denied
#没有权限

2.3 SBIT

SBIT 这个特殊权限是只对目录有效,它的作用是如果目录有这个特殊权限,好么在这个目录下所创建的目录和文件只能是创建者或者该目录的创建者才能删除。

案例:防止公共目录中的文件误删除
# 数据准备
[root@server ~]# mkdir -p /test/d2
[root@server ~]# ll -d /test/d2
drwxr-xr-x. 2 root root 6 Nov  6 21:33 /test/d2[root@server ~]# chmod o+w /test/d2
[root@server ~]# ll -d /test/d2
drwxr-xrwx. 2 root root 6 Nov  6 21:33 /test/d2#然后们分别用 zhoulanping 和 shuxinyi 在这个目录下创建文件
[zhoulanping@server ~]$ cd /test/d2
[zhoulanping@server d2]$ touch f1[shuxinyi@server ~]$ cd /test/d2
[shuxinyi@server d2]$ touch f2#查看
[shuxinyi@server d2]$ ll
total 0
-rw-r--r--. 1 zhoulanping zhoulanping 0 Nov  6 21:36 f1
-rw-r--r--. 1 shuxinyi    shuxinyi    0 Nov  6 21:35 f2#接下来我们让shuxinyi去删除 f1,让zhoulanping去删除 f2
[shuxinyi@server d2]$ rm -f f1
[shuxinyi@server d2]$
[zhoulanping@server d2]$ rm -f f2
[zhoulanping@server d2]$ 
#可以删除对方创建的文件#为了避免发生这种误删除操作,我们可以设置 SBIT 权限。
[root@server ~]# chmod o+t /test/d2
[root@server ~]# ll -d /test/d2
drwxr-xrwt. 2 root root 6 Nov  6 21:39 /test/d2#设置好后,我们再次创建好文件后,再进行删除测试:
[zhoulanping@server d2]$ touch f1
[shuxinyi@server d2]$ touch f2[shuxinyi@server d2]$ ll
total 0
-rw-r--r--. 1 zhoulanping zhoulanping 0 Nov  6 21:42 f1
-rw-r--r--. 1 shuxinyi    shuxinyi    0 Nov  6 21:42 f2#再次删除
[shuxinyi@server d2]$ rm -f f1
rm: cannot remove 'f1': Operation not permitted
[zhoulanping@server d2]$ rm -f f2
rm: cannot remove 'f2': Operation not permitted
[zhoulanping@server d2]$ 
#分析不可以删除对方的了

注意:SUID/SGID对可执行文件作用

特性SUIDSGID(对文件)
影响对象用户身份组身份
权限提升用户→文件所有者用户组→文件所属组
安全风险更高(可能获得root)相对较低
典型应用passwd, sudo需要特定组权限的工具

3.访问控制权限(ACL)

ACL 权限的作用:在不改变原本的权限的情况下,额外为某个用户或某个组设置权限。

3.1 setfacl

这个命令用于设置访问控制权限(ACL),语法如下:

setfacl 2.3.1 -- set file access control lists
Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...-m, --modify=acl        modify the current ACL(s) of file(s)   设置acl权限-M, --modify-file=file  read ACL entries to modify from file-x, --remove=acl        remove entries from the ACL(s) of file(s)   删除acl权限-X, --remove-file=file  read ACL entries to remove from file-b, --remove-all        remove all extended ACL entries  删除所有acl权限-k, --remove-default    remove the default ACL--set=acl           set the ACL of file(s), replacing the current ACL--set-file=file     read ACL entries to set from file--mask              do recalculate the effective rights mask-n, --no-mask           don't recalculate the effective rights mask-d, --default           operations apply to the default ACL-R, --recursive         recurse into subdirectories-L, --logical           logical walk, follow symbolic links-P, --physical          physical walk, do not follow symbolic links--restore=file      restore ACLs (inverse of `getfacl -R')--test              test mode (ACLs are not modified)-v, --version           print version and exit-h, --help              this help textACL ENTRIESThe setfacl utility recognizes the following ACL entry formats (blanks inserted for clarity):[d[efault]:] [u[ser]:]uid [:perms]Permissions of a named user. Permissions of the file owner if uid is empty.[d[efault]:] g[roup]:gid [:perms]Permissions of a named group. Permissions of the owning group if gid is empty.[d[efault]:] m[ask][:] [:perms]Effective rights mask[d[efault]:] o[ther][:] [:perms]Permissions of others.EXAMPLESGranting an additional user read accesssetfacl -m u:lisa:r fileRevoking write access from all groups and all named users (using the effective rights mask)setfacl -m m::rx fileRemoving a named group entry from a file's ACLsetfacl -x g:staff fileCopying the ACL of one file to anothergetfacl file1 | setfacl --set-file=- file2Copying the access ACL into the Default ACLgetfacl --access dir | setfacl -d -M- dir
使用示例
[root@server ~]# cd /test/
[root@server test]# touch f11.#用普通用户修改f1
[root@server test]# su - zhoulanping 
Last login: Thu Nov  6 21:12:12 CST 2025 on pts/4
[zhoulanping@server ~]$ cd /test/
[zhoulanping@server test]$ ll
total 0
-rw-r--r--. 1 root root 0 Nov  6 22:22 f1
[zhoulanping@server test]$ echo "hello" > f1
-bash: f1: Permission denied #没有权限

发现此时写权限是被拒绝的。原因是 zhoulanping这个用户对 f1 文件来说是其他用户,而其他用户是没有写权限的,因此会被描绘。

如果我们希望在不改变这个文件的原本权限的情况下,希望 zhoulanping 用户可以对这个文件进行修改,那么我们就需要使用 acl 权限。

2.#设置acl权限
[root@server test]# setfacl -m u:zhoulanping:rw f1
[root@server test]# ll f1
-rw-rw-r--+ 1 root root 0 Nov  6 22:22 f1
#看到最后的.变成了+,设置成功3.#再次用普通用户修改f1
[zhoulanping@server test]$ echo "hello" > f1
[zhoulanping@server test]$ cat f1
hello

3.2 getfacl

我们可以通过这个命令来查看文件或目录所拥有的 ACL 权限。

getfacl 2.3.1 -- get file access control lists
Usage: getfacl [-aceEsRLPtpndvh] file ...-a, --access            display the file access control list only-d, --default           display the default access control list only-c, --omit-header       do not display the comment header-e, --all-effective     print all effective rights-E, --no-effective      print no effective rights-s, --skip-base         skip files that only have the base entries-R, --recursive         recurse into subdirectories-L, --logical           logical walk, follow symbolic links-P, --physical          physical walk, do not follow symbolic links-t, --tabular           use tabular output format-n, --numeric           print numeric user/group identifiers--one-file-system   skip files on different filesystems-p, --absolute-names    don't strip leading '/' in pathnames-v, --version           print version and exit-h, --help              this help text
使用示例
1.#文件f1
[root@server test]# getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
user:zhoulanping:rw- #我们自己设置的
group::r--
mask::rw-
other::r--2.#目录d1(未改变)
[root@server test]# getfacl d1
# file: d1
# owner: root
# group: root
user::rwx
group::r-x
other::r-x

3.3 删除acl权限

还是使用 setfacl 命令来完成。

[root@server test]# setfacl -b f1
[root@server test]# getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
group::r--
other::r--
#恢复

4.权限掩码

权限掩码的作用是为创建文件或目录设置默认权限。默认情况下,权限掩码为 022,这个值是记录在 /etc/login.defs 文件中。我们可以通过 umask 命令来查看。

[root@server test]# umask
00220   022
1    21.特殊权限位
普通权限 : 0
SUID	: 8
SGID	: 4
SBIT	:12.标准权限位

我们创建文件的目录来进行验证:

[root@server ~]# mkdir d1
[root@server ~]# touch f1
[root@server ~]# ll f1
-rw-r--r--. 1 root root 0 Nov  6 22:42 f1
[root@server ~]# ll -d d1
drwxr-xr-x. 2 root root 6 Nov  6 22:42 d1目录:rwxr-xr-x   =  755
文件:rw-r--r--   =  644目录:777 ->  rwxrwxrwx022 ->  ----w--w------------------755 ->  rwxr-xr-x文件:777  -> rwxrwxrwx022 ->  ----w--w------------------755 ->  rwxr-xr-x->  --x--x--x  #文件不能有x权限-----------------644 ->  rw-r--r--

如果我们希望修改文件或目录的默认仅限,我们可以修改 umask 值。

[root@server ~]# umask 031
[root@server ~]# umask
0031

修改后,我们再次创建目录和文件:

[root@server ~]# mkdir d2
[root@server ~]# touch f2
[root@server ~]# ll -d d2
drwxr--rw-. 2 root root 6 Nov  6 22:52 d2
[root@server ~]# ll f2
-rw-r--rw-. 1 root root 0 Nov  6 22:52 f2目录:777 -> rwxrwxrwx031 -> ----wx--x----------------746 -> rwxr--rw-文件:777 -> rwxrwxrwx031 -> ----wx--x----------------746 -> rwxr--rw--> --x------  #文件不能有x权限----------------646 -> rw-r--rw-
http://www.dtcms.com/a/577577.html

相关文章:

  • Flutter for HarmonyOS开发指南(二):混合开发架构与通信机制
  • 分布式分片执行原理解析
  • 自主建站全攻略:建站系统的选择指南与深度说明
  • 什么网站有做qq群排名的关键词优化价格
  • 网站内容怎么进行编写
  • SSM 房屋租赁系统
  • 网站排名优化服务公司福建省住房和城乡建设网站
  • 开发集成热门小游戏(vue+js)
  • Java-简单项目开发流程
  • 莱芜网站优化平台现在中型公司做网站用的是什么框架
  • 区块链-B站API程序系统方案
  • LVS三种模式及调度算法解析
  • MySql 9.5.0(2025)-Windows安装步骤
  • 查看 MySQL 数据库里的所有信息,包括表结构、数据内容
  • Rocky10 使用kubeadm 安装k8s 单节点
  • K8s 中的Serviceaccount
  • MySQL 事务的两种使用方式
  • 【MySQL 进阶】高性能优化
  • 如何做静态页网站wordpress swf 上传
  • Dotnet-Dapper的用法
  • 深入理解 Spring Boot 中的数据库迁移:Flyway 与 Liquibase 实战指南
  • 使用visa进行仪器控制
  • 百度网站验证创意交易平台官网
  • Node.js异步编程的多种实现方式:从回调地狱到优雅的async/await
  • 全面评测 | Photoshop 2026 新特性深度解析与实测体验
  • FastAPI深度解析
  • wordpress会员数据共同盐城网络优化
  • 学校招聘教师网站建设网站建站前期准备工作
  • springboot系列--自动配置原理
  • Spring Aop实现