关于一个selinux的问题

本文记录了Apache HTTP Server在启动过程中遇到的问题,主要是由于SELinux权限限制导致无法打开监听端口及日志文件,通过调整SELinux配置解决了这一问题。
[root@localhost ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2018-05-17 09:44:33 CST; 43s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 1163 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
  Process: 1162 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
 Main PID: 1162 (code=exited, status=1/FAILURE)


May 17 09:44:33 localhost.localdomain httpd[1162]: (13)Permission denied: AH00072: make_sock: could not b...808
May 17 09:44:33 localhost.localdomain httpd[1162]: (13)Permission denied: AH00072: make_sock: could not b...808
May 17 09:44:33 localhost.localdomain httpd[1162]: no listening sockets available, shutting down
May 17 09:44:33 localhost.localdomain httpd[1162]: AH00015: Unable to open logs
May 17 09:44:33 localhost.localdomain systemd[1]: httpd.service: main process exited, code=exited, statu...LURE
May 17 09:44:33 localhost.localdomain kill[1163]: kill: cannot find process ""
May 17 09:44:33 localhost.localdomain systemd[1]: httpd.service: control process exited, code=exited status=1
May 17 09:44:33 localhost.localdomain systemd[1]: Failed to start The Apache HTTP Server.
May 17 09:44:33 localhost.localdomain systemd[1]: Unit httpd.service entered failed state.
May 17 09:44:33 localhost.localdomain systemd[1]: httpd.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# setenforce 1
[root@localhost ~]# systemctl restart httpd

Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.


得出的结果确实是selinux导致的,但是网络上都是说关闭selinux来解决,为什么不能就更改selinux策略来解决呢?

### SELinux 使用示例与配置案例 #### 1. SELinux 配置文件调整 SELinux 的核心配置位于 `/etc/selinux/config` 文件中,此文件决定了系统下次启动时加载的策略类型以及运行模式。以下是常见的配置选项及其含义: - `SELINUX=enforcing`: 启用强制模式,所有不符合策略的行为都会被阻止并记录。 - `SELINUX=permissive`: 启用宽容模式,仅记录违规行为而不实际阻止操作。 - `SELINUX=disabled`: 完全禁用 SELinux。 可以通过编辑配置文件实现永久更改[^4]: ```bash vim /etc/selinux/config ``` 临时切换 SELinux 模式可以使用以下命令: ```bash setenforce 0 # 切换至 Permissive 模式 setenforce 1 # 切换回 Enforcing 模式 ``` #### 2. Web 服务端口自定义配置 假设需要将 HTTPD 服务监听的默认端口从 80 更改为 10000,则需执行以下步骤: - 添加新的端口到 SELinux 策略中: ```bash semanage port -a -t http_port_t -p tcp 10000 ``` 如果未安装 `policycoreutils-python-utils` 工具包,可通过以下方式安装[^4]: ```bash yum -y install policycoreutils-python-utils ``` - 更新防火墙规则以允许新端口流量(如适用)。 - 测试服务是否正常工作: ```bash curl localhost:10000 ``` #### 3. 自定义目录的安全上下文设置 为了使特定目录能够被某些服务访问,例如让 Apache 访问 `/var/www/html` 下的内容,可按照以下方法设置安全上下文: - 增加匹配规则: ```bash semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?" ``` - 应用更新后的上下文: ```bash restorecon -RvF /var/www/html/ ``` 以上过程确保了即使在重新挂载磁盘或其他情况下,指定路径仍保留正确的 SELinux 上下文标签[^4]。 #### 4. 查看当前 SELinux 状态 要了解目前系统的 SELinux 设置状况,包括其状态 (Enforcing/Permissive/Disabled),所使用的策略类型(Targeted/MLS) 及其他细节信息,可以利用 sestatus 命令获取这些资料[^1]: ```bash sestatus ``` #### 5. 编写简单的 SELinux 策略模块 当发现现有策略无法满足需求时,可能需要编写自己的 SELinux 策略模块。下面是一个基本流程概述: - 创建一个新的 .te 文件描述所需权限; - 构建 te 文件成二进制形式 (.pp 文件卡) 并装载它入内核之中去生效改变; 具体例子可见于官方仓库中的 notebook-examples 子目录下的多个实例展示如何构建个性化政策解决方案[^2]. --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值