RHEL简单管理SELINUX

Security Enhanced Linux(SELinux)是一个额外的系统安全层,主要目的是防止已遭泄露的系统服务访问用户数据。

对于一个服务来说,要关注SELinux的三个方面,一是文件SELinux安全上下文中的类型字段要和服务的类型字段匹配,二是服务SELinux允许端口,三是服务某个功能的布尔值。

下面以http服务为例子,一一阐述。

1.安装httpd包,查看是否安装成功 rpm -qa | grep http

httpd-2.4.6-17.el7.x86_64
httpd-tools-2.4.6-17.el7.x86_64

2.编辑配置文件,这里我们需要3个页面,因此需要准备3台虚拟主机,分别提供3个页面做测试。由于虚拟主机不能和本机一起使用,所以关闭本机服务。

vim /etc/httpd/conf/httpd.conf 

#DocumentRoot "/var/www/html"

:wq

cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d/

vim /etc/httpd/conf.d/httpd-vhosts.conf

<VirtualHost 192.168.1.126:80>
    DocumentRoot "/var/www/html"
    ServerName www1.example.com
    ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
    CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>


Listen 808
<VirtualHost 192.168.1.126:808>
    DocumentRoot "/var/www/html2"
    ServerName www2.example.com
    ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
    CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>


Listen 8088
<VirtualHost 192.168.1.126:8088>
    DocumentRoot "/html3"
    ServerName www3.example.com
    ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
   CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>


<Directory "/html3">
   AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

:wq

3.由于防火墙规则限制,我们先开放httpd服务    

firewall-cmd --permanent --add-service=http

firewall-cmd --permanent --add-port=808/tcp

firewall-cmd --permanent --add-port=8088/tcp

firewall-cmd --reload

4.准备3个页面

vim /var/www/html/index.html

<h1>www1.example.com</h1>

:wq

vim /var/www/html2/index.html

<h1>www2.example.com</h1>

:wq

vim /html3/index.html

<h1>www3.example.com</h1>

:wq

5.重启httpd服务

systemctl restart httpd

Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.重启失败,是什么原因呢,根据系统提示查看日志

6.journalctl -xn

可以看到有段提示,这就是我们前面讲的端口问题,需要在SELinux中允许服务使用此端口

If you want to allow /usr/sbin/httpd to bind to network port 808
                                      Then you need to modify the port type.

7.可以使用命令查看http服务端口的类型名

semanage port -l | grep http

http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000

8.依据端口名添加使用端口

semanage port -a -t http_port_t -p tcp 808

semanage port -a -t http_port_t -p tcp 8088

9.修改完成后再次重启服务,此时服务正常运行

10.访问80端口的网页,成功访问

11.访问808端口的网页,成功访问

12.访问8088端口的网页,访问失败

13.这时候产生这个结果的原因是,文件SELinux安全上下文中的类型字段要和服务的类型字段不匹配
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0                 html

drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0  html2

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0  html3

14.修改html3的类型字段

semanage fcontext -a -t httpd_sys_content_t '/html3(/.*)?'

restorecon -vFR /html3/

15.重启服务

16.访问8088端口的网页,访问成功

17.最后一个测试httpd服务某个功能的布尔值

18.一httpd为每个用户提供家目录展示功能为例

19.开启用户家目录访问功能

vim /etc/httpd/conf.d/userdir.conf

#    UserDir disabled

UserDir public_html

:wq

20.新建一个用户,并为其准备展示网页

useradd abc

mkdir /home/abc/public_html

vim /home/abc/public_html/index.html

<h1>abc.example.com</h1>

:wq

chmod o+rx /home/abc

21.重启服务

22.访问用户展示网页 http://192.1685.1.126/~abc

23.该访问被禁止的原因是该功能的布尔值为off,需要更改该功能的布尔值

getsebool -a | grep http   查看http服务支持的功能

httpd_enable_homedirs --> off      用户家目录展示功能

setsebool -P httpd_enable_homedirs on    开启该功能

24.重启服务

25.重新访问

26.到此基本管理SELinux已经介绍完毕

原文地址:https://www.cnblogs.com/cq146637/p/7806556.html