探索 SELinux:背景

发布日期:2026-07-18 17:27:03  来源 : 杭州电子商务研究院    作者 :Elle Krout    浏览量 :0
Elle Krout 杭州电子商务研究院 发布日期:2026-07-18 17:27:03  
0

SELinux 的一个关键部分是理解和使用 SELinux上下文。系统上的所有内容都包含一个上下文,这些上下文用于确定哪些用户、应用程序和服务可以访问哪些文件、目录和应用程序。即使不了解详细的策略创建,大多数 SELinux 用户也可以通过使用和更改上下文来管理他们的系统。SELinux 中有三种类型的上下文,通过查看文件的 SELinux 权限可以最好地解释它们。要查看目录的 SELinux 上下文,请使用ls带有-Z标志的命令。这是针对/var/www/目录的:
[vagrant@centos www]$ ls -Zdrwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 cgi-bindrwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 html
我们希望从权限输出中看到的是类似于以下内容的部分system_u:object_r:httpd_sys_script_exec_t。这些是文件的三个上下文。让我们深入了解一下:

SELinux 上下文用户上下文

第一个蓝色部分是用户上下文。它有三个可用值:user_usystem_uroot。每个值都表示总体上哪一类用户可以访问此文件——而不是具体是哪个用户。在用户上下文中user_u,一般登录用户可以访问该文件(就普通文件权限而言);值system_u表示系统用户——如上例所示;最后,root表示只有系统的 root 用户才能访问该文件。

角色上下文

角色上下文(上例中为洋红色)主要用于进程和域。一般 SELinux 用户可能无需担心此上下文。对于文件和目录,此上下文始终为object_u

类型上下文

类型上下文(紫色)可以说是设置 SELinux 权限和对 SELinux 进行故障排除时需要关注的最重要的上下文。类型上下文提供与 SELinux 相关的细粒度控制。即使只启用了默认的 SELinux 并且没有进行任何更改,您的系统也有许多类型上下文。使用命令semanage fcontext -l查看所有可用类型。在查看特定文件或服务的上下文时,您可能需要使用管道。输出使用正则表达式来表示给定的上下文是否是递归的。例如,以下是CentOS 7 安装中 grep所有具有类型上下文的目录:httpd_sys_content_t
[vagrant@centos ~]$ sudo semanage fcontext -l | grep "httpd_sys_content"/srv/([^/]*/)?www(/.*)?                            all files          system_u:object_r:httpd_sys_content_t:s0/var/www(/.*)?                                     all files          system_u:object_r:httpd_sys_content_t:s0/etc/htdig(/.*)?                                   all files          system_u:object_r:httpd_sys_content_t:s0/srv/gallery2(/.*)?                                all files          system_u:object_r:httpd_sys_content_t:s0/var/lib/trac(/.*)?                                all files          system_u:object_r:httpd_sys_content_t:s0/var/lib/htdig(/.*)?                               all files          system_u:object_r:httpd_sys_content_t:s0/var/www/icons(/.*)?                               all files          system_u:object_r:httpd_sys_content_t:s0/usr/share/glpi(/.*)?                              all files          system_u:object_r:httpd_sys_content_t:s0/usr/share/htdig(/.*)?                             all files          system_u:object_r:httpd_sys_content_t:s0/usr/share/drupal.*                                all files          system_u:object_r:httpd_sys_content_t:s0/usr/share/z-push(/.*)?                            all files          system_u:object_r:httpd_sys_content_t:s0/var/www/svn/conf(/.*)?                            all files          system_u:object_r:httpd_sys_content_t:s0/usr/share/icecast(/.*)?                           all files          system_u:object_r:httpd_sys_content_t:s0/var/lib/cacti/rra(/.*)?                           all files          system_u:object_r:httpd_sys_content_t:s0/usr/share/ntop/html(/.*)?                         all files          system_u:object_r:httpd_sys_content_t:s0/usr/share/doc/ghc/html(/.*)?                      all files          system_u:object_r:httpd_sys_content_t:s0/usr/share/openca/htdocs(/.*)?                     all files          system_u:object_r:httpd_sys_content_t:s0/usr/share/selinux-policy[^/]*/html(/.*)?          all files          system_u:object_r:httpd_sys_content_t:s0

改变语境

如果我们愿意,我们可以更改某些目录的上下文。我们可以这样做,因为我们需要更改权限,或者因为我们在不同位置之间移动了文件——虽然文件夹中创建的所有文件都会继承上下文,但移动的文件会保留其原始上下文。假设我们将一个新的index.html文件移动到我们的/var/www/html目录中:
[vagrant@centos ~]$ sudo mv index.html /var/www/html/[vagrant@centos ~]$ cd /var/www/html/[vagrant@centos html]$ ls -Z-rw-rw-r--. vagrant vagrant unconfined_u:object_r:user_home_t:s0 index.html
这个例子特别合适,因为我们可以在实践中看到 SELinux 的效果。如果我们尝试通过 Web 浏览器查看index.html文件,我们会收到Forbidden错误。这是因为如上所示,它保留了其原始user_home_t类型,而不是httpd_sys_content_t所需的上下文。可以使用以下命令更改此restorecon设置:
[vagrant@centos html]$ restorecon index.html[vagrant@centos html]$ ls -Z-rw-rw-r--. vagrant vagrant unconfined_u:object_r:httpd_sys_content_t:s0 index.html
restorecon使用 SELinux 的默认上下文来确保所有文件都是适当的类型。在本例中,它看到index.html/var/www(/.*)?目录的一部分,并确保它继承了适当的上下文。或者,假设我们移动了整个html/目录,需要更改整个目录的 SELinux 上下文。假设出于某种原因,我们的服务器没有 Apache 所需的默认 SELinux 策略。为此,我们可以使用semanage更改类型上下文:
semanage fcontext -a -t httpd_sys_content_t '/var/www/html(/.*)?'
-t标志表示类型。此外,请注意包含(/.*)?-- 这告诉 SELinux,目录下的文件和目录/var/www/html也继承了此样式。如果需要,我们还可以删除目录的上下文:
semanage fcontext -d "/var/www/html(/.*)?"
即使通过管理 SELinux 上下文和权限,我们也只是触及了这个深入工具的表面。请继续查看博客以了解更多探索 SELinux 的内容,或访问LinuxAcademy.com以了解有关 SELinux 和其他系统管理和安全主题的更多课程。
以上内容来自杭州电子商务研究院推送
分享到:

长按或扫码识别 分享给好友

长按或扫码识别 分享给好友
关于我们
热门推荐
合作伙伴
免责声明:本站部分资讯来源于网络,如有侵权请及时联系客服,我们将尽快处理
Copyright © 2025-2027 ToB产业网址导航 公安备案 浙公网安备33010602013138号 浙ICP备16025413号-9
支持 反馈 关注 数据