文件和目录权限管理

级别和权限介绍

文件和目录的级别
  • 用户所有者(u)
  • 组所有者(g)
  • 其他(o)
文件和目录的权限
  • 读(r)
  • 写(w)
  • 执行(x)

创建文件或目录时,权限基于两个因素:

  • 基本权限

  • umask

自动分配给某个文件或目录的基本权限不是文件和目录最终的默认权限。当创建文件或目录时,基本权限会被umask更改。基本权限和umask的组合会为文件和目录创建默认权限。

基本权限
权限 符号 数值
无权限 ---
执行 --x 1
-w- 2
写和执行 -wx 3
r-- 4
读和执行 r-x 5
读写 rw- 6
读、写、执行 rwx 7
  • 目录的基本权限是777(drwxrwxrwx),任何人授予读、写和执行的权限。意味着目录所有者、组、和其他用户可以列出目录的内容,并且可以在该目录下中创建、删除和编辑。当然一个目录中的文件也可以通过授权改变权限以组织其他用户进行编辑。

  • 文件的基本权限是666(-rw-rw-rw-),任何人都授予读写权限。意味着文件所有者、组和其他用户都可以读和编辑该文件。

-rwxr-xr-x. 1 root root 10271880 Oct 19 23:23 vmlinuz-4.18.0-348.el8.x86_64
- 表示一个文件
rwx 表示文件所有者有读、写和执行权限
r-x 表示组有读和执行权限,但没有在文件中写入的权限
r-x 表示其他用户有读和执行权限,但没有在文件中写入的权限
. 表示为该文件设定了selinux安全上下文
drwxr-xr-x. 3 root root       21 Dec 18 21:55 loader
d 表示是一个目录
rwx 表示目录所有者有读取、写入和访问目录内容的权限
r-x 表示组有写入和访问目录的权限,但是没有读取目录中内容的权限
r-x 表示其他用户有写入和访问目录的权限,但是没有读取目录中内容的权限
. 表示为该目录设定了selinux安全上下文
umask

umask会自动从基本权限值中删除权限,以提高linux系统的整体安全性。主要控制如何为新创建的文件和目录设置文件权限的变量。umask符号和数值表示如下:

权限 符号 数值
读、写、执行 rwx
读写 rw- 1
读和执行 r-x 2
r-- 3
写和执行 -wx 4
-w- 5
执行 --x 6
无权限 --- 7
[sunyinpeng@foundation ~]$ umask
0002
[sunyinpeng@foundation ~]$ umask -S
u=rwx,g=rwx,o=rx
[root@foundation ~]# umask
0022
[root@foundation ~]# umask -S
u=rwx,g=rx,o=rx

标准用户的默认umask是0002;root用户的默认umask是0022。

umask的第一个数字代表特殊权限。最后三位数字分别代表用户拥有者(u)、组群所有者(g)和其他(o)中删除的权限。

举几个例子:介绍基本权限和umask组合分配后生成的默认权限

标准用户创建目录时

umask被设备为002(rwxrwxr-x),目录的基本权限被设置为777(rwxrwxrwx)。则创建目录的默认权限为775(drwxrwxr-x)。可以理解为基本权限数值减去umask数值。

[sunyinpeng@foundation ~]$ mkdir umask_directory
[sunyinpeng@foundation ~]$ ls -l
total 0
drwxrwxr-x. 2 sunyinpeng sunyinpeng 6 Dec 20 21:27 umask_directory

drwxrwxr-x 权限:目录所有者、组可以列出目录的内容,并且可以在该目录下(以及子目录)中创建、删除和编辑项。其他用户只能列出该目录的内容并将其下移到其中。

标准用户创建文件时

umask被设备为002(rwxrwxr-x),文件的基本权限被设置为666(-rw-rw-rw-),最终默认权限为644(-rw-rw-r--)。

[sunyinpeng@foundation ~]$ touch umask_file
[sunyinpeng@foundation ~]$ ls -l umask_file 
-rw-rw-r--. 1 sunyinpeng sunyinpeng 0 Dec 20 21:35 umask_file

-rw-rw-r-- 权限:文件拥有者和组可以读取和编辑该文件,其他用户只能读取文件。

root用户创建目录时

umask被设备为022(rwx-r-xr-x),目录的基本权限被设备为777(rwxrwxrwx)。最终默认权限为755(rwxr-xr-x)。

[root@foundation ~]# mkdir umask_directory
[root@foundation ~]# ls -l
total 0
drwxr-xr-x. 2 root root 6 Dec 20 21:40 umask_directory

drwxr-xr-x 权限:目录所有者可以列出目录的内容,并且可以在该目录下(以及子目录)中创建、删除和编辑。组和其他用户只能列出该目录的内容并将其移动。

root用户创建文件时

umask被设置为022(rwxr-xr-x),文件的基本权限被设置为666(rw-rw-rw-),最终权限为644(-rw-r--r--)。

[root@foundation ~]# touch umask_file
[root@foundation ~]# ls -l umask_file 
-rw-r--r--. 1 root root 0 Dec 20 21:54 umask_file

-rw-r--r-- 权限:文件所有者可以读取和编辑文件,而组和其他用户只能读取文件。

以上内容一定会有一个疑问,怎么管理umask的数值来给定文件默认权限呢?接下来,进行刨析

管理UMASK
显示umask的当前数值和符号值
[sunyinpeng@foundation ~]$ umask
0002
[sunyinpeng@foundation ~]$ umask -S
u=rwx,g=rwx,o=rx

[root@foundation ~]# umask
0022
[root@foundation ~]# umask -S
u=rwx,g=rx,o=rx

bash的umask

bash一般有两种状态,登录状态和非登录状态。

可以使用echo $0确认,显示bash则为非登录shell状态,显示- bash,则为登录shell状态。

[root@foundation ~]# echo $0
bash

非登录的默认umask在/etc/bashrc文件中,登录的默认umask在/etc/profile中。

[root@foundation ~]# grep umask /etc/bashrc
    # By default, we want umask to get set. This sets it for non-login shell.
       umask 002
       umask 022

[root@foundation ~]# grep umask /etc/profile
# By default, we want umask to get set. This sets it for login shell
    umask 002
    umask 022

特定用户的umask

特定用户的umask默认在该用户的家目录下的 .bashrc文件中配置。

[root@foundation ~]# find / -name .bashrc
/etc/skel/.bashrc
/root/.bashrc
/home/sunyinpeng/.bashrc

新创建主目录的umask

新创建用的用户主目录指定的umask权限默认在/etc/login.defs文件中。

[root@foundation ~]# grep UMASK /etc/login.defs
# UMASK is also used by useradd(8) and newusers(8) to set the mode for new
UMASK           022
# If HOME_MODE is not set, the value of UMASK is used to create the mode.

访问控制列表

在Linux中,每个目录或文件每次只能有一个用户所有者和组群所有者,譬如:

[root@foundation ~]# ll
total 0
drwxr-xr-x. 2 root       root       6 Dec 20 21:40 umask_directory
-rw-r--r--. 1 sunyinpeng sunyinpeng 0 Dec 20 21:54 umask_file

在这种情况下要授予用户权限来访问属于不同用户或组群的特定文件或目录,同时将其他文件和目录保留为私密,可以使用ACL,也就是访问控制列表。

例如,下面一个文件为特定用户文件,授予jerry用户和lisa组对该文件也有像特定用户所有者和组相同的权限。

[root@foundation ~]# getfacl umask_file 
# file: umask_file
# owner: sunyinpeng
# group: sunyinpeng
user::rw-
group::r--
other::r--

[root@foundation ~]# setfacl -m u:jerry:rw- umask_file 
[root@foundation ~]# setfacl -m g:lisa:r-- umask_file 
[root@foundation ~]# getfacl umask_file 
# file: umask_file
# owner: sunyinpeng
# group: sunyinpeng
user::rw-
user:jerry:rw-
group::r--
group:lisa:r--
mask::rw-
other::r--

例子

现有两个群组stday_groups和student_groups,10个用户student0-student9,用户和群组分布如下:

组群 用户
stday_groups student0
student1
student2
student3
student4
student5
student_groups student6
student7
student8
student9

需要在环境中创建/opt/material目录,且目录需要特权用户root管理,在该目录下需要创建两个文件confidential、topsecret。

confidential文件所属组为stday_groups,有组内student0用户全权管理,组内其他用户除读取该文件以外不能做任何操作,其它用户对该文件没有任何权限。

topsecret文件所属组为student_groups,有组内student6用户全权管理,组内其它用户除执行以外不能做任何操作,另外除stday_groups组中的student0和student1对该文件有读写权限以外,其他用户对该文件没有任何权限。

[root@foundation ~]# mkdir /opt/meterial
[root@foundation ~]# ls -ld /opt/meterial/
drwxr-xr-x. 2 root root 6 Dec 26 23:20 /opt/meterial/

[root@foundation ~]# touch /opt/meterial/confidential
[root@foundation ~]# touch /opt/meterial/topsecret
[root@foundation ~]# ls -l /opt/meterial/
total 0
-rw-r--r--. 1 root root 0 Dec 26 23:24 confidential
-rw-r--r--. 1 root root 0 Dec 26 23:24 topsecret

[root@foundation ~]# chown student0:stday_groups /opt/meterial/confidential 
[root@foundation ~]# chmod u=rwx,g=r,o=- /opt/meterial/confidential 
[root@foundation ~]# ls -ld /opt/meterial/confidential 
-rwxr-----. 1 student0 stday_groups 0 Dec 26 23:24 /opt/meterial/confidential

[root@foundation ~]# chown student6:student_groups /opt/meterial/topsecret 
[root@foundation ~]# chmod u=rwx,g=x,o=- /opt/meterial/topsecret
-rwx--x----. 1 student6 student_groups 0 Dec 26 23:24 /opt/meterial/topsecret

[root@foundation ~]# setfacl -m u:student0:rw /opt/meterial/topsecret 
[root@foundation ~]# setfacl -m u:student1:rw /opt/meterial/topsecret 
[root@foundation ~]# getfacl /opt/meterial/topsecret 
getfacl: Removing leading '/' from absolute path names
# file: opt/meterial/topsecret
# owner: student6
# group: student_groups
user::rwx
user:student0:rw-
user:student1:rw-
group::--x
mask::rwx
other::---