• RHCSA认证考试----3.配置 SELinux


    红帽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环境检查: 考试时一般是不需要的,但是还是要进行检查

    1. [root@servera ~]# rpm -qa | grep http
    2. httpd-filesystem-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.noarch
    3. httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64
    4. libnghttp2-1.33.0-3.el8_2.1.x86_64
    5. httpd-tools-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64
    6. centos-logos-httpd-85.8-2.el8.noarch
    7. mod_http2-1.15.7-5.module_el8.6.0+1111+ce6f4ceb.x86_64

    httpd没有安装的时候就安装运行并设置为开机自启

    1. [root@servera conf]# yum install httpd -y
    2. ......
    3. [root@servera conf]# systemctl start httpd
    4. [root@servera html]# systemctl enable httpd
    5. Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

    #将http服务监听的端口是否 Listen 82 考试时一般是不需要的,但是还是要进行检查

    1. [root@servera ~]# netstat -anlp | grep 82
    2. tcp 0 0 0.0.0.0:44821 0.0.0.0:* LISTEN -
    3. tcp6 0 0 :::82 :::* LISTEN 1014/httpd
    4. raw6 0 0 :::58 :::* 7 982/NetworkManager
    5. unix 2 [ ACC ] STREAM LISTENING 36882 1961/systemd /run/user/0/bus
    6. unix 3 [ ] STREAM CONNECTED 27482 902/VGAuthService
    7. unix 2 [ ] STREAM CONNECTED 41985 982/NetworkManager
    8. unix 3 [ ] STREAM CONNECTED 29185 982/NetworkManager
    9. unix 2 [ ] DGRAM 29321 982/NetworkManager
    10. unix 3 [ ] STREAM CONNECTED 25282 784/systemd-udevd
    11. unix 3 [ ] STREAM CONNECTED 29328 982/NetworkManager
    12. unix 3 [ ] STREAM CONNECTED 30796 982/NetworkManager

    没有监听是82端,就修改一下http的监听配置

    1. [root@servera conf]# vim /etc/httpd/conf/httpd.conf
    2. .........
    3. # Listen 80
    4. Listen 82

    验证一下

    1. [root@servera ~]# cat /etc/httpd/conf/httpd.conf | grep -A 3 -B 3 Listen
    2. ServerRoot "/etc/httpd"
    3. #
    4. # Listen: Allows you to bind Apache to specific IP addresses and/or
    5. # ports, instead of the default. See also the
    6. # directive.
    7. #
    8. # Change this to Listen on specific IP addresses as shown below to
    9. # prevent Apache from glomming onto all bound IP addresses.
    10. #
    11. #Listen 12.34.56.78:80
    12. Listen 82
    13. #
    14. # Dynamic Shared Object (DSO) Support

     检查/var/www/html目录存在的文件,做实验如果没有自己创建

    1. [root@servera ~]# ll -Z /var/www/html/
    2. total 4
    3. -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处于的模式

    1. [root@servera yum.repos.d]# getenforce
    2. Enforcing

    selinux防护的配置主要是有semanage服务在管理的

    如果semanage命令不存在时则需要安装该命令对应的软件

    [root@servera conf]# semana #这里tab没有自动补全时就是该selinux配置的命令不存在,需要进行安装

    yum查找该命令对应的软件服务

    1. [root@servera conf]# yum provides semanage
    2. Last metadata expiration check: 0:04:09 ago on Fri 19 Aug 2022 10:52:07 PM CST.
    3. policycoreutils-python-utils-2.9-16.el8.noarch : SELinux policy core python utilities
    4. Repo : base
    5. Matched from:
    6. Filename : /usr/sbin/semanage

    安装semanage命令的软件服务就可以提供相关的命令了,不用启动相关服务的

    1. [root@servera conf]# yum install -y policycoreutils-python-utils
    2. ......
    3. Complete!

    查看selinux防护策略中关于http的配置情况

    1. [root@servera ~]# semanage port -l | grep http
    2. http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
    3. http_cache_port_t udp 3130
    4. http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
    5. pegasus_http_port_t tcp 5988
    6. 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的配置是否添加了

    1. [root@servera ~]# semanage port -l | grep http
    2. http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
    3. http_cache_port_t udp 3130
    4. http_port_t tcp 82, 80, 81, 443, 488, 8008, 8009, 8443, 9000
    5. pegasus_http_port_t tcp 5988
    6. pegasus_https_port_t tcp 5989

    验证是否可以访问web服务

    1. [root@servera html]# curl http://servera.lab.example.com:82
    2. welcome httpd 82

  • 相关阅读:
    Laf Assistant:云开发从未如此爽快!
    Bridging nonnull in Objective-C to Swift: Is It Safe?
    springboot系列(二十一):基于AOP实现自定义注解且记录接口日志|超级超级详细,建议收藏
    如何解决msvcp110.dll丢失问题,分享5个有效的解决方法
    毕业生去向管理系统(毕业生就业招聘系统)
    国内离线安装 Chrome 扩展程序的方法总结
    SpringCloud环境
    Java / Android 线程间通信
    MySQL: 锁
    Oracle数据库体系结构(三)_逻辑结构
  • 原文地址:https://blog.csdn.net/jiang0615csdn/article/details/127588798