SELinux个人学习:[1]解决web网站无法访问

2025-10-26 17:36:03

1、setroubleshoot

用于把SELinux 的错误讯息与克服方法记录到 /var/log/messages 与 /var/log/setroubleshoot/* 里头,所以一定要启动该服务。

所以我们先来安装。

输入一下命令安装。

# yum install setroubleshoot setroubleshoot-server

SELinux个人学习:[1]解决web网站无法访问

2、安装之后,要重新启动 auditd,否则 setroubleshootd 的功能不会被启动的

输入以下命令

# service auditd restart

SELinux个人学习:[1]解决web网站无法访问

3、在浏览器输入网页地址。

显示一下信息,这个时候错误的信息就被记录了。

Forbidden

You don't have permission to access /index.html on this server.

SELinux个人学习:[1]解决web网站无法访问

4、输入一下命令。来查看错误信息

# cat /var/log/messages | grep setroubleshoot

Apr 21 23:34:37 centos setroubleshoot: SELinux is preventing /usr/sbin/httpd from getattr access on the file /var/www/html/index.html. For complete SELinux messages. run sealert -l 47d2c8e2-308b-4931-ae63-88bb4f518155

最重要的是

sealert -l 47d2c8e2-308b-4931-ae63-88bb4f518155

SELinux个人学习:[1]解决web网站无法访问

5、输入之前找到的信息

sealert -l 47d2c8e2-308b-4931-ae63-88bb4f518155

显示以下内容

SELinux is preventing /usr/sbin/httpd from getattr access on the 文件 /var/www/html/index.html.

*****  插件 restorecon (99.5 置信度) 建议  ******************************************

If 您想要修复标签。

/var/www/html/index.html 默认标签应为 httpd_sys_content_t。

Then 您可以运行 restorecon。

Do

# /sbin/restorecon -v /var/www/html/index.html

*****  插件 catchall (1.49 置信度) 建议  ********************************************

If 您确定应默认允许 httpd getattr 访问 index.html file。

Then 您应该将这个情况作为 bug 报告。

您可以生成本地策略模块允许这个访问。

Do

请执行以下命令此时允许这个访问:

# grep httpd /var/log/audit/audit.log | audit2allow -M mypol

# semodule -i mypol.pp

SELinux个人学习:[1]解决web网站无法访问

6、从上面的内容,就已经提示你解决的方法。

If 您想要修复标签。

/var/www/html/index.html 默认标签应为 httpd_sys_content_t。

Then 您可以运行 restorecon。

Do

# /sbin/restorecon -v /var/www/html/index.html

所以输入# /sbin/restorecon -v /var/www/html/index.html

网站可以正常浏览了。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢