13.4. 配置示例
以下示例展示了 SELinux 如何补充 Apache HTTP 服务器以及如何维护 Apache HTTP 服务器的完整功能。
13.4.1. 运行静态站点
要创建静态网站,请使用
httpd_sys_content_t
类型标记该网站的 .html
文件。默认情况下,Apache HTTP 服务器无法写入标记为 httpd_sys_content_t
类型的文件。以下示例为只读网站创建一个新目录来存储文件:
- 以 root 用户身份使用
mkdir
工具来创建顶级目录:~]# mkdir /mywebsite
- 以 root 用户身份,创建一个
/mywebsite/index.html
文件。将以下内容复制并粘贴到/mywebsite/index.html
中:<html> <h2>index.html from /mywebsite/</h2> </html>
- 要允许 Apache HTTP 服务器只读访问
/mywebsite/
,以及其下的文件和子目录,请使用httpd_sys_content_t
类型标记该目录。以 root 用户身份输入以下命令,将标签更改添加到 file-context 配置中:~]# semanage fcontext -a -t httpd_sys_content_t "/mywebsite(/.*)?"
- 以 root 用户身份使用
restorecon
工具进行标签更改:~]# restorecon -R -v /mywebsite restorecon reset /mywebsite context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 restorecon reset /mywebsite/index.html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
- 在本例中,以 root 用户身份编辑
/etc/httpd/conf/httpd.conf
文件。注释掉现有的DocumentRoot
选项。添加DocumentRoot "/mywebsite"
选项。编辑后,这些选项应如下所示:#DocumentRoot "/var/www/html" DocumentRoot "/mywebsite"
- 以 root 身份输入以下命令,查看 Apache HTTP 服务器的状态。如果服务器停止,启动它:
~]# systemctl status httpd.service httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: inactive (dead)
~]# systemctl start httpd.service
如果服务器正在运行,请以 root 用户身份执行以下命令来重新启动服务(这也应用对httpd.conf
所做的任何更改):~]# systemctl status httpd.service httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: active (running) since Wed 2014-02-05 13:16:46 CET; 2s ago
~]# systemctl restart httpd.service
- 使用 Web 浏览器导航到
http://localhost/index.html
。此时会显示以下内容:index.html from /mywebsite/
13.4.2. 共享 NFS 和 CIFS 卷
默认情况下,客户端上的 NFS 挂载使用 NFS 卷策略定义的默认上下文标记。在常见策略中,此默认上下文使用
nfs_t
类型。此外,默认情况下,挂载在客户端上的 Samba 共享使用策略定义的默认上下文标记。在常见策略中,此默认上下文使用 cifs_t
类型。
根据策略配置,服务可能无法读取使用
nfs_t
或 cifs_t
类型标记的文件。这可能会导致标记为这些类型的文件系统被挂载,然后被其他服务读取或导出。可以启用或禁用布尔值,以控制允许哪些服务访问 nfs_t
和 cifs_t
类型。
启用
httpd_use_nfs
布尔值,以允许 httpd
访问和共享 NFS 卷(使用 nfs_t
类型标记):
~]# setsebool -P httpd_use_nfs on
启用
httpd_use_cifs
布尔值,以允许 httpd
访问和共享 CIFS 卷(使用 cifs_t
类型标记):
~]# setsebool -P httpd_use_cifs on
注意
如果您不希望 setsebool 更改在重启后保留,请不要使用 the
-P
选项。
13.4.3. 在服务间共享文件
类型强制可帮助阻止进程访问供其他进程使用的文件。例如,默认情况下,Samba 无法读取标记为
httpd_sys_content_t
类型的文件,这些类型旨在由 Apache HTTP 服务器使用。如果所需的文件使用 public_content_t
或 public_content_rw_t
类型标记,则可以在 Apache HTTP 服务器、FTP、rsync 和 Samba 之间共享文件。
以下示例创建了目录和文件,并允许通过 Apache HTTP 服务器、FTP、rsync 和 Samba 共享(只读)目录和文件:
- 以 root 用户身份使用
mkdir
工具来创建新的顶级目录,以在多个服务间共享文件:~]# mkdir /shares
- 与 file-context 配置中不匹配模式的文件和目录可以使用
default_t
类型进行标记。受限制的服务无法访问此类型:~]$ ls -dZ /shares drwxr-xr-x root root unconfined_u:object_r:default_t:s0 /shares
- 以 root 用户身份,创建
/shares/index.html
文件。将以下内容复制并粘贴到/shares/index.html
中:<html> <body> <p>Hello</p> </body> </html>
- 使用
public_content_t
类型标记/shares/
允许对 Apache HTTP 服务器、FTP、rsync 和 Samba 进行只读访问。以 root 用户身份输入以下命令,将标签更改添加到 file-context 配置中:~]# semanage fcontext -a -t public_content_t "/shares(/.*)?"
- 以 root 用户身份使用
restorecon
工具应用标签更改:~]# restorecon -R -v /shares/ restorecon reset /shares context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0 restorecon reset /shares/index.html context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
通过 Samba 共享
/shares/
:
- 确认已安装了 samba 、samba-common 和 samba-client 软件包(版本号可能有所不同):
~]$ rpm -q samba samba-common samba-client samba-3.4.0-0.41.el6.3.i686 samba-common-3.4.0-0.41.el6.3.i686 samba-client-3.4.0-0.41.el6.3.i686
如果没有安装任何这些软件包,请以 root 用户身份运行以下命令安装它们:~]# yum install package-name
- 以 root 用户身份编辑
/etc/samba/smb.conf
文件。在该文件的底部添加以下条目,以通过 Samba 共享/shares/
目录:[shares] comment = Documents for Apache HTTP Server, FTP, rsync, and Samba path = /shares public = yes writable = no
- 需要 Samba 帐户来挂载 Samba 文件系统。以 root 身份输入以下命令来创建 Samba 帐户,其中 username 是现有 Linux 用户。例如,smbpasswd -a testuser 为 Linux
testuser
用户创建一个 Samba 帐户:~]# smbpasswd -a testuser New SMB password: Enter a password Retype new SMB password: Enter the same password again Added user testuser.
如果您运行上述命令,指定系统上不存在的帐户的用户名,则会导致Cannot locate Unix account for 'username'!
错误。 - 启动 Samba 服务:
~]# systemctl start smb.service
- 输入以下命令列出可用的共享,其中 username 是第 3 步中添加的 Samba 帐户。提示输入密码时,在第 3 步中输入分配给 Samba 帐户的密码(版本号可能有所不同):
~]$ smbclient -U username -L localhost Enter username's password: Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.4.0-0.41.el6] Sharename Type Comment --------- ---- ------- shares Disk Documents for Apache HTTP Server, FTP, rsync, and Samba IPC$ IPC IPC Service (Samba Server Version 3.4.0-0.41.el6) username Disk Home Directories Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.4.0-0.41.el6] Server Comment --------- ------- Workgroup Master --------- -------
- 用户
mkdir
实用程序创建新目录。此目录将用于挂载共享 Samba共享
:~]# mkdir /test/
- 以 root 用户身份运行以下命令,将共享 Samba
共享挂载到
/test/
,将 username 替换为第 3 步中的用户名:~]# mount //localhost/shares /test/ -o user=username
输入 username 的密码,这是在第 3 步中配置的。 - 查看通过 Samba 共享的 文件的内容:
~]$ cat /test/index.html <html> <body> <p>Hello</p> </body> </html>
通过 Apache HTTP 服务器共享
/shares/
:
- 确认已安装 httpd 软件包(版本号可能有所不同):
~]$ rpm -q httpd httpd-2.2.11-6.i386
如果没有安装此软件包,请以 root 用户身份使用yum
工具安装它:~]# yum install httpd
- 进入
/var/www/html/
目录。以 root 用户身份输入以下命令,创建到/
目录的链接(名为 shares):shares
/html]# ln -s /shares/ shares
- 启动 Apache HTTP 服务器:
~]# systemctl start httpd.service
- 使用 Web 浏览器导航到
http://localhost/shares
。此时会显示/shares/index.html
文件。
默认情况下,Apache HTTP 服务器会读取
index.html
文件(如果存在)。如果 /shares/
没有 index.html
,而使用 file1
、file2
和 file3
,则在访问 http://localhost/shares
时会出现目录列表:
- 删除
index.html
文件:~]# rm -i /shares/index.html
- 以 root 用户身份使用
touch
工具在/shares/
中创建三个文件:~]# touch /shares/file{1,2,3} ~]# ls -Z /shares/ -rw-r--r-- root root system_u:object_r:public_content_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:public_content_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:public_content_t:s0 file3
- 以 root 用户身份输入以下命令查看 Apache HTTP 服务器的状态:
~]# systemctl status httpd.service httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: inactive (dead)
如果服务器停止,启动它:~]# systemctl start httpd.service
- 使用 Web 浏览器导航到
http://localhost/shares
。此时会显示目录列表:
13.4.4. 更改端口号
根据策略配置,服务只能在某些端口号中运行。尝试更改服务在没有更改策略的情况下运行的端口可能会导致服务无法启动。以 root 用户身份使用
semanage
工具列出允许 httpd
侦听的端口 SELinux:
~]# semanage port -l | grep -w http_port_t
http_port_t tcp 80, 443, 488, 8008, 8009, 8443
默认情况下,SELinux 允许
httpd
侦听 TCP 端口 80、443、488、8008、8009 或 8443。如果配置了 /etc/httpd/conf/httpd.conf
,以便 httpd
侦听没有为 http_port_t
列出的任何端口,则httpd
无法启动。
将
httpd
配置为在 TCP 端口 80、443、488、8008、8009 或 8443 之外的端口上运行:
- 以 root 用户身份编辑
/etc/httpd/conf/httpd.conf
文件,以便Listen
选项列出了httpd
策略中没有配置的端口。以下示例将httpd
配置为侦听 10.0.0.1 IP 地址,并在 TCP 端口 12345 上侦听:# Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses (0.0.0.0) # #Listen 12.34.56.78:80 Listen 10.0.0.1:12345
- 以 root 用户身份输入以下命令,将端口添加到 SELinux 策略配置中:
~]# semanage port -a -t http_port_t -p tcp 12345
- 确认添加了端口:
~]# semanage port -l | grep -w http_port_t http_port_t tcp 12345, 80, 443, 488, 8008, 8009, 8443
如果您不再在端口 12345 上运行
httpd
,以 root 用户身份使用 semanage
工具从策略配置中删除端口:
~]# semanage port -d -t http_port_t -p tcp 12345