这是SELinux案例的教学方法:
找出SELinux是否处于活动状态:
$ sestatus
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: enforcing
Policy version: 24
Policy from config file: targeted
如果是这样,进行一些比较检查可能会有所帮助。例如,服务器的默认DocumentRoot位于/var/www/html
,但我们希望它在其他位置/path/to/document/root
。
如果SELinux没有主动弄乱资源,ls -dZ
则目录上将显示类似以下内容:
$ ls -dZ /path/to/document/root
? /path/to/document/root/
另一方面,如果应用SELinux上下文,则ls -dZ
看起来更像:
$ ls -dZ /path/to/document/root
drwxrws--x+ cfgadm cfgadmin system_u:object_r:file_t:s0 /path/to/document/root
如果我们将其与正常工作的DocumentRoot相比较,它将看起来像:
$ ls -dZ /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
在_r
和_t
涉及到-r
(--role
和-t
(--type
)参数chcon
这里是一个切下的手册页:
NAME
chcon - change file security context
SYNOPSIS
chcon [OPTION]... CONTEXT FILE...
chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...
chcon [OPTION]... --reference=RFILE FILE...
DESCRIPTION
Change the security context of each FILE to CONTEXT. With --reference,
change the security context of each FILE to that of RFILE.
--reference=RFILE
use RFILE's security context rather than specifying a CONTEXT value
-R, --recursive
operate on files and directories recursively
乍一看,以下似乎有效,但可能无效。
$ sudo chcon -R -t httpd_sys_content_t /path/to/document/root
如果Web服务器仍然看不到DocumentRoot,请注意上下文一直到根都很重要:
$ sudo chcon -R -t httpd_sys_content_t /path/to/document
$ sudo chcon -R -t httpd_sys_content_t /path/to
$ sudo chcon -R -t httpd_sys_content_t /path
此时,Web服务器可以看到目录。
是的,我今晚学会了艰难的方法。
注意:从RedHat文档(5.6.1。临时更改:chcon)来看,使用chcon在概念上有一个缺点,其中指出:
The chcon command changes the SELinux context for files. However, changes
made with the chcon command do not survive a file system relabel, or the
execution of the restorecon command.
使用semanage和restorecon进行更永久的更改。一个简单的例子:
$ sudo semanage fcontext --add -t httpd_sys_content_t -s system_u \
"/path/to/document/root(/.*)?"
$ sudo restorecon -FR /path/to/document/root
关于restorecon,请注意,需要-F来影响整个上下文(即用户和类型)。同样,-R意味着递归进行更改。参数-v或-p可以以冗长或简洁的方式显示进度。使用-FRnv来查看会发生什么,而无需实际进行任何更改。
一旦semanage的是采用这种方式,就可以查看用以下命令本地安全更改:
$ sudo semanage export
语义存储导出的输出可以保存并由语义存储导入使用,以使其更容易将一组更改应用于各种系统。
注意:此答案为站点提供了最基本的类型上下文。安全性可以更加精细。例如,使用以下命令查看可应用于Web服务器页面的类型列表:
$ seinfo -t | grep http
注意:默认情况下可能未安装诸如semanage和seinfo之类的实用程序。至少在某些发行版中,必需的软件包可能会这样命名:
policycoreutils-python
setools-console
DocumentRoot
,这可能使您对Web服务器正在查看的内容有所了解。您可能还想检查路径中的其他目录,尽管如果确实在/var/www/
这些目录下应该不成问题