红帽RHCE考试上午-RHCSA(RH200)
servera.example.com 任务
3. 配置SELinux
背景:非标准端口82上运行的WEB 服务器但是在提供web内容时遇到问题。根据需要调试并解决该问题,并使其满足以下条件:
• 系统上的web 服务器能够提供/var/www/html 中所有现在有的html文件(注意:不要删除或者其他方式改动现有的文件内容)
• Web 服务器通过82端口访问
• Web 服务器在系统启动时自动启动
题目内容解析:
• 82端运行web服务,需要部署http服务,并设置监听端口为82
• 提供html的web页面是在/var/www/html/并需要设置selinux的上下属性httpd_sys_content_t
• web服务是运行状态,同时设置开机自动
• selinux防护是处于强制模式并需要配置selinux防护放开http的82端口
实操演示过程:
web环境检查: 考试时一般是不需要的,但是还是要进行检查
[root@servera ~]# rpm -qa | grep http
httpd-filesystem-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.noarch
httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64
libnghttp2-1.33.0-3.el8_2.1.x86_64
httpd-tools-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64
centos-logos-httpd-85.8-2.el8.noarch
mod_http2-1.15.7-5.module_el8.6.0+1111+ce6f4ceb.x86_64
httpd没有安装的时候就安装运行并设置为开机自启
[root@servera conf]# yum install httpd -y
......
[root@servera conf]# systemctl start httpd
[root@servera html]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
#将http服务监听的端口是否 Listen 82 考试时一般是不需要的,但是还是要进行检查
[root@servera ~]# netstat -anlp | grep 82
tcp 0 0 0.0.0.0:44821 0.0.0.0:* LISTEN -
tcp6 0 0 :::82 :::* LISTEN 1014/httpd
raw6 0 0 :::58 :::* 7 982/NetworkManager
unix 2 [ ACC ] STREAM LISTENING 36882 1961/systemd /run/user/0/bus
unix 3 [ ] STREAM CONNECTED 27482 902/VGAuthService
unix 2 [ ] STREAM CONNECTED 41985 982/NetworkManager
unix 3 [ ] STREAM CONNECTED 29185 982/NetworkManager
unix 2 [ ] DGRAM 29321 982/NetworkManager
unix 3 [ ] STREAM CONNECTED 25282 784/systemd-udevd
unix 3 [ ] STREAM CONNECTED 29328 982/NetworkManager
unix 3 [ ] STREAM CONNECTED 30796 982/NetworkManager
没有监听是82端,就修改一下http的监听配置
[root@servera conf]# vim /etc/httpd/conf/httpd.conf
.........
# Listen 80
Listen 82
验证一下
[root@servera ~]# cat /etc/httpd/conf/httpd.conf | grep -A 3 -B 3 Listen
ServerRoot "/etc/httpd"
#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 82
#
# Dynamic Shared Object (DSO) Support
检查/var/www/html目录存在的文件,做实验如果没有自己创建
[root@servera ~]# ll -Z /var/www/html/
total 4
-rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 14 Sep 5 22:57 index.html
检查是否具备了selinux的http的上下属性
如果不具备selinux的httpd的安全上下文,则进行授权就可以了
[root@servera ~]# restorecon -Rv /var/www/html/
配置selinux防护的放开http的82端口
先检查selinux处于的模式
[root@servera yum.repos.d]# getenforce
Enforcing
selinux防护的配置主要是有semanage服务在管理的
如果semanage命令不存在时则需要安装该命令对应的软件
[root@servera conf]# semana #这里tab没有自动补全时就是该selinux配置的命令不存在,需要进行安装
yum查找该命令对应的软件服务
[root@servera conf]# yum provides semanage
Last metadata expiration check: 0:04:09 ago on Fri 19 Aug 2022 10:52:07 PM CST.
policycoreutils-python-utils-2.9-16.el8.noarch : SELinux policy core python utilities
Repo : base
Matched from:
Filename : /usr/sbin/semanage
安装semanage命令的软件服务就可以提供相关的命令了,不用启动相关服务的
[root@servera conf]# yum install -y policycoreutils-python-utils
......
Complete!
查看selinux防护策略中关于http的配置情况
[root@servera ~]# semanage port -l | grep http
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
注:如上的配置表示http服务对应的端口没有配置82端口的,需要进行配置一下。
设置http服务的新端口,并验证
[root@servera conf]# semanage port -a -t http_port_t -p tcp 82
命令解析:
-a:add新增配置
-t: 执行需要修改配置的类型
-p:指定对应的协议类型
-d:表示删除带哦semanage的策略配置
验证selinux的配置是否添加了
[root@servera ~]# semanage port -l | grep http
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 82, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
验证是否可以访问web服务
[root@servera html]# curl http://servera.lab.example.com:82
welcome httpd 82