如何解决SELinux问题?
创始人
2024-08-30 18:31:43
0

说起SELinux,多数Linux发行版缺省都激活了它,可见它对系统安全的重要性,可惜由于它本身有一定的复杂性,如果不熟悉的话往往会产生一些看似莫名其妙的问题,导致人们常常放弃使用它,为了不因噎废食,学学如何解决SELinux问题是很有必要的。

我们以CentOS环境为例重现一个非常常见的SELinux问题:

首先需要确认SELinux处于激活状态,可以使用getenforce或sestatus命令:

  1. shell> getenforce  
  2. Enforcing  
  3.  
  4. shell> sestatus  
  5. SELinux status:                 enabled  
  6. SELinuxfs mount:                /selinux  
  7. Current mode:                   enforcing  
  8. Mode from config file:          enforcing  
  9. Policy version:                 24  
  10. Policy from config file:        targeted 

注:关于SELinux的基础知识介绍请参考鸟哥的Linux私房菜中相关的介绍。

我们还需要确认系统已经安装并启动了Apache,没有的话就YUM装一个,这很简单,就不多说了,接着在root目录创建一个测试文件test.html,如下:

  1. shell> cat /root/test.html  
  2. hello, world. 

然后把这个测试文件拷贝到Apache的DocumentRoot目录,我的Apache是通过YUM安装的话,缺省是/var/www/html目录,如下:

  1. shell> cp /root/test.html /var/www/html 

接着浏览一下,如果没出什么幺蛾子,应该一切都在意料之中,如下:

  1. shell> curl http://localhost/test.html  
  2. hello, world. 

看到这,你可能觉得我废话连篇,别着急,下面就是见证奇迹的时候了:

同样还是那个测试文件test.html,不过这次不再是拷贝,而是移动,如下:

  1. shell> mv /root/test.html /var/www/html 

接着浏览一下,怎么样,结果很出人意料吧,竟然提示权限错误,如下:

  1. shell> curl http://localhost/test.html  
  2.  
  3.  
  4. 403 Forbidden 
  5.  
  6. Forbidden

     
  7. You don't have permission to access /test.html  

  8. on this server.

     
  9.  

当然,我们现在知道这个问题是由于SELinux引起的,但还不知其所以然,实际上问题的原因此时已经被audit进程记录到了相应的日志里,可以这样查看:

  1. shell> audit2why < /var/log/audit/audit.log 

如果看不懂的话,推荐安装setroubleshoot套件:

  1. shell> yum install setroubleshoot 

它本身是一个GUI套件,不过其中包含的一个sealert命令对我们命令行用户很有用:

  1. shell> sealert -a /var/log/audit/audit.log  
  2. Summary:  
  3.  
  4. SELinux is preventing /usr/sbin/httpd "getattr" access to  
  5. /var/www/html/test.html.  
  6.  
  7. Detailed Description:  
  8.  
  9. SELinux denied access requested by httpd. /var/www/html/test.html may be a  
  10. mislabeled. /var/www/html/test.html default SELinux type is httpd_sys_content_t,  
  11. but its current type is admin_home_t. Changing this file back to the default  
  12. type, may fix your problem.  
  13.  
  14. File contexts can be assigned to a file in the following ways.  
  15.  
  16.   * Files created in a directory receive the file context of the parent  
  17.     directory by default.  
  18.   * The SELinux policy might override the default label inherited from the  
  19.     parent directory by specifying a process running in context A which creates  
  20.     a file in a directory labeled B will instead create the file with label C.  
  21.     An example of this would be the dhcp client running with the dhclient_t type  
  22.     and creating a file in the directory /etc. This file would normally receive  
  23.     the etc_t type due to parental inheritance but instead the file is labeled  
  24.     with the net_conf_t type because the SELinux policy specifies this.  
  25.   * Users can change the file context on a file using tools such as chcon, or  
  26.     restorecon.  
  27.  
  28. This file could have been mislabeled either by user error, or if an normally  
  29. confined application was run under the wrong domain.  
  30.  
  31. However, this might also indicate a bug in SELinux because the file should not  
  32. have been labeled with this type.  
  33.  
  34. If you believe this is a bug, please file a bug report against this package.  
  35.  
  36. Allowing Access:  
  37.  
  38. You can restore the default system context to this file by executing the  
  39. restorecon command. restorecon '/var/www/html/test.html', if this file is a  
  40. directory, you can recursively restore using restorecon -R  
  41. '/var/www/html/test.html'.  
  42.  
  43. Fix Command:  
  44.  
  45. /sbin/restorecon '/var/www/html/test.html' 

这次应该看懂了吧!原因是说Apache下文件上下文类型应该是httpd_sys_content_t,但是现在是admin_home_t,所以权限错误,并且在结尾处给出了修复命令。

可httpd_sys_content_t,admin_home_t都怎么看啊?很简单,借助ls命令的-Z参数即可:

  1. shell> ls -Z /path 

回到问题的开始,拷贝之所以没出现问题,是因为cp自动修改上下文属性,而移动之所以出现问题是因为mv保留原文件的上下文属性。

注:关于SELinux和Apache的详细介绍,可以参考『man httpd_selinux』。

知道了如何解决SELinux问题,以后如果遇到类似的情况不要急着武断的关闭SELinux。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...