Red Hat Training
A Red Hat training course is available for Red Hat Enterprise Linux
4.7. SELinux 上下文 - 标记文件
在运行 SELinux 的系统上,所有进程和文件都采用代表安全相关信息的标记方式。此信息称为 SELinux 上下文。对于文件,可使用 ls -Z 命令查看:
~]$ ls -Z file1
-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1
在本例中,SELinux 提供了用户(
unconfined_u)、角色(object_r)、类型(user_home_t)和级别(s0)。此信息用于制定访问控制决策。在 DAC 系统上,访问权限根据 Linux 用户和组群 ID 进行控制。在 DAC 规则后检查 SELinux 策略规则。如果 DAC 规则首先拒绝访问,则不会使用 SELinux 策略规则。
注意
默认情况下,新建的文件和目录继承其父目录的 SELinux 类型。例如,当在
/etc 目录中使用 etc_t 类型创建新文件时,新文件将继承同一类型:
~]$ ls -dZ - /etc drwxr-xr-x. root root system_u:object_r:etc_t:s0 /etc
~]# touch /etc/file1~]# ls -lZ /etc/file1 -rw-r--r--. root root unconfined_u:object_r:etc_t:s0 /etc/file1
SELinux 提供多个命令来管理文件系统标签,如 chcon、semanage fcontext、restorecon 和
matchpathcon。
4.7.1. 临时更改:chcon
chcon 命令更改文件的 SELinux 上下文。但是,通过 chcon 命令所做的更改不会在文件系统重新标记或执行 restorecon 命令之间保留。SELinux 策略控制用户是否能够修改任何给定文件的 SELinux 上下文。使用 chcon 时,用户提供要更改的所有或部分 SELinux 上下文。文件类型不正确是 SELinux 拒绝访问的常见原因。
快速参考
- 运行 chcon -t 类型 file-name 命令更改文件类型,其中 type 是 SELinux 类型,如
httpd_sys_content_t,file-name 是文件或目录名称:~]$chcon -t httpd_sys_content_t file-name - 运行 chcon -R -t 类型 directory-name 命令以更改目录及其内容的类型,其中 type 是 SELinux 类型,如
httpd_sys_content_t,directory-name 是目录名称:~]$chcon -R -t httpd_sys_content_t directory-name
过程 4.6. 更改文件或目录的类型
以下步骤演示了更改类型,而不是 SELinux 上下文的其他属性。对于目录,本节中的示例的作用相同,例如,如果
file1 是目录。
- 更改到您的主目录。
- 创建新文件并查看其 SELinux 上下文:
~]$touch file1~]$ls -Z file1 -rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1在本例中,file1 的SELinux 上下文包括 SELinuxunconfined_u用户、object_r角色、user_home_t类型和s0级别。有关 SELinux 上下文的每个部分的描述,请参阅 第 2 章 SELinux Contexts。 - 输入以下命令将类型更改为
samba_share_t。t选项仅更改类型。然后查看更改:~]$chcon -t samba_share_t file1~]$ls -Z file1 -rw-rw-r-- user1 group1 unconfined_u:object_r:samba_share_t:s0 file1 - 使用以下命令恢复
file1文件的 SELinux 上下文。使用-v选项查看哪些更改:~]$restorecon -v file1 restorecon reset file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:user_home_t:s0在本例中,上一类型samba_share_t已恢复到正确的user_home_t类型。使用目标策略(Red Hat Enterprise Linux 中的默认 SELinux 策略)时,restorecon 命令读取/etc/selinux/targeted/contexts/files/目录中的文件,以查看应具有哪些 SELinux 上下文文件。
过程 4.7. 更改目录及其内容类型
以下示例演示了创建新目录,并将目录的文件类型及其内容更改为 Apache HTTP 服务器使用的类型。如果您希望 Apache HTTP 服务器使用不同的文档根目录(而不是
/var/www/html/),则使用本示例中的配置:
- 以 root 用户身份,在此目录中创建一个新的
Web/目录,再创建 3 个空文件(和file1、file2file3)。其中包含的web/目录和文件使用default_t类型标记:~]#mkdir /web~]#touch /web/file{1,2,3}~]#ls -dZ /web drwxr-xr-x root root unconfined_u:object_r:default_t:s0 /web~]#ls -lZ /web -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:default_t:s0 file3 - 以 root 用户身份,输入以下命令将
web/目录(及其内容)的类型改为httpd_sys_content_t:~]#chcon -R -t httpd_sys_content_t /web/~]#ls -dZ /web/ drwxr-xr-x root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/~]#ls -lZ /web/ -rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 file3 - 要恢复默认 SELinux 上下文,以 root 用户身份使用
restorecon工具程序:~]#restorecon -R -v /web/ restorecon reset /web context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file3 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0 restorecon reset /web/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
有关 chcon 的详情,请查看 chcon(1) 手册页。
注意
类型强制是 SELinux targeted 策略中使用的主要权限控制。在大多数情况下,可以忽略 SELinux 用户和角色。