RHCSA---权限管理

权限管理

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

权限分为以下:

1.普通权限

bash 复制代码
[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--. 看这一部分(权限相关)

bash 复制代码
-	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 命令。

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

bash 复制代码
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 exit

Each 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:

bash 复制代码
[root@server ~]# ll f1
-rw-r--r--. 1 root root 0 Nov  6 16:44 f1

1.#用+
[root@server ~]# chmod a+x f1
[root@server ~]# ll f1
-rwxr-xr-x. 1 root root 0 Nov  6 16:44 f1

2.#用=
[root@server ~]# chmod u=rwx,g=rw,o=r f1
[root@server ~]# ll f1
-rwxrw-r--. 1 root root 0 Nov  6 16:44 f1

3.#用数字
[root@server ~]# chmod 744 f1
[root@server ~]# ll f1
-rwxr--r--. 1 root root 0 Nov  6 16:44 f1

1.2 chown

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

bash 复制代码
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 is
                         the 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 the
                         ownership of a symlink)
      --from=CURRENT_OWNER:CURRENT_GROUP
                         change the owner and/or group of each file only if
                         its current owner and/or group match those specified
                         here.  Either may be omitted, in which case a match
                         is 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 than
                         specifying OWNER:GROUP values
  -R, --recursive        operate on files and directories recursively

The 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 link
                         to a directory, traverse it
  -L                     traverse every symbolic link to a directory
                         encountered
  -P                     do not traverse any symbolic links (default)

      --help     display this help and exit
      --version  output version information and exit

Owner 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:

bash 复制代码
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 f1


2.#改文件所属组
[root@server ~]# chown :zhoulanping f1
[root@server ~]# ll f1
-rw-r--r--. 1 zhoulanping zhoulanping 0 Nov  6 17:12 f1


3.#改文件所有者&所属组
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 d1

2.#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)都没有任何权限的(包括读)

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

切换普通用户查看严重

bash 复制代码
[root@server ~]# su - zhoulanping 
[zhoulanping@server ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied

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

bash 复制代码
[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 命令来访问:

bash 复制代码
[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

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

bash 复制代码
#数据准备
[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来进行测试:

bash 复制代码
[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 文件:

bash 复制代码
[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 特殊权限:

bash 复制代码
[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 文件:

bash 复制代码
[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

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

bash 复制代码
1# 创建日志组
[root@server ~]# groupadd loggroup

2# 创建日志文件并设置权限
[root@server ~]# touch /var/log/app.log
[root@server ~]# chown root:loggroup /var/log/app.log
[root@server ~]# chmod 664 /var/log/app.log

3# 创建简单的日志脚本
[root@server ~]# cat > /usr/local/bin/writelog << 'EOF'
#!/bin/bash
echo "$(date): $USER - $*" >> /var/log/app.log
echo "日志记录成功"
EOF

4# 设置脚本的SGID权限
[root@server ~]# chown root:loggroup /usr/local/bin/writelog
[root@server ~]# chmod 2755 /usr/local/bin/writelog

5.#检查权限
[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/writelog



6.对比
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 这个特殊权限是只对目录有效 ,它的作用是如果目录有这个特殊权限,好么在这个目录下所创建的目录和文件只能是创建者或者该目录的创建者才能删除。

案例:防止公共目录中的文件误删除
bash 复制代码
# 数据准备
[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对可执行文件作用

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

3.访问控制权限(ACL)

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

3.1 setfacl

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

bash 复制代码
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 text


   ACL ENTRIES
       The 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.


EXAMPLES
       Granting an additional user read access
              setfacl -m u:lisa:r file

       Revoking write access from all groups and all named users (using the effective rights mask)
              setfacl -m m::rx file

       Removing a named group entry from a file's ACL
              setfacl -x g:staff file

       Copying the ACL of one file to another
              getfacl file1 | setfacl --set-file=- file2

       Copying the access ACL into the Default ACL
              getfacl --access dir | setfacl -d -M- dir
使用示例
bash 复制代码
[root@server ~]# cd /test/
[root@server test]# touch f1

1.#用普通用户修改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 权限。

bash 复制代码
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 权限。

bash 复制代码
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
使用示例
bash 复制代码
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 命令来完成。

bash 复制代码
[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 命令来查看。

bash 复制代码
[root@server test]# umask
0022

0   022
1    2

1.特殊权限位
普通权限 : 0
SUID	: 8
SGID	: 4
SBIT	:1

2.标准权限位

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

bash 复制代码
[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 ->  rwxrwxrwx
           022 ->  ----w--w-
           -----------------
           755 ->  rwxr-xr-x


文件:
           777  -> rwxrwxrwx
           022 ->  ----w--w-
           -----------------
           755 ->  rwxr-xr-x
               ->  --x--x--x  #文件不能有x权限
           -----------------
        
       	   644 ->  rw-r--r--

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

bash 复制代码
[root@server ~]# umask 031
[root@server ~]# umask
0031

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

bash 复制代码
[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 -> rwxrwxrwx
       031 -> ----wx--x
       ----------------
       746 -> rwxr--rw-

文件:
       777 -> rwxrwxrwx
       031 -> ----wx--x
       ----------------
       746 -> rwxr--rw-
           -> --x------  #文件不能有x权限
       ----------------
       646 -> rw-r--rw-
相关推荐
神奇的程序员3 小时前
从已损坏的备份中拯救数据
运维·后端·前端工程化
虾..4 小时前
Linux 软硬链接和动静态库
linux·运维·服务器
Evan芙4 小时前
Linux常见的日志服务管理的常见日志服务
linux·运维·服务器
玄斎5 小时前
MySQL 单表操作通关指南:建库 / 建表 / 插入 / 增删改查
运维·服务器·数据库·学习·程序人生·mysql·oracle
trayvontang5 小时前
Nginx之location配置
运维·nginx
十六年开源服务商6 小时前
WordPress定制开发最佳公司的用户画像
运维
hkhkhkhkh1236 小时前
Linux设备节点基础知识
linux·服务器·驱动开发
世岩清上7 小时前
AI驱动的智能运维:从自动化到自主化的技术演进与架构革新
运维·人工智能·自动化
HZero.chen7 小时前
Linux字符串处理
linux·string
张童瑶7 小时前
Linux SSH隧道代理转发及多层转发
linux·运维·ssh