Linux 练习 - 安全加固SELinux

1、启用 SELinux 策略并安装 httpd 服务,改变网站的默认主目录为 /website,添加 SELinux 文件标签规则,使网站可访问;
# 安装 httpd
[root@localhost ~]# yum install -y httpd

# 创建 /website 及首页文件
[root@localhost ~]# mkdir /website
[root@localhost ~]# echo selinux > /website/index.html

# 修改 /website 目录及文件的 SELinux 属性
[root@localhost ~]# chcon -R -t httpd_sys_content_t /website

# 修改 httpd 配置
[root@localhost ~]# sed -i 's@^DocumentRoot "/var/www/html"@DocumentRoot "/website"@' /etc/httpd/conf/httpd.conf
[root@localhost ~]# sed -i 's@^<Directory "/var/www/html">@<Directory "/website">@' /etc/httpd/conf/httpd.conf

# 重启服务
[root@localhost ~]# systemctl restart httpd

# 访问
[root@localhost ~]# curl 127.0.0.1
selinux
2、修改上述网站的 http 端口为 9527,增加 SELinux 端口标签,使网站可访问;
# 修改监听端口
[root@localhost ~]# sed -i 's/Listen 80/Listen 9527/' /etc/httpd/conf/httpd.conf

# 安装支持 semanage 的包
[root@localhost ~]# yum install -y policycoreutils-python

# 添加端口
[root@localhost ~]# semanage port -a -t http_port_t -p tcp 9527

# 重启 httpd
[root@localhost ~]# systemctl restart httpd

# 访问
[root@localhost ~]# curl 127.0.0.1:9527
selinux
3、启用相关的 SELinux 布尔值,使上述网站的用户 student 的家目录可通过 http 访问;
  • 配置
# 查看是否加载 userdir_modle 模块
## 设置主机名,否则无法查询
[root@localhost ~]# hostnamectl set-hostname web.solin.com
## 查询
[root@web ~]# httpd -M |grep userdir_module
 userdir_module (shared)

# 创建用户
[root@web ~]# useradd student

# 创建 public_html 目录
[root@web ~]# mkdir /home/student/public_html

# 创建 index.html 文件
[root@web ~]# echo student home web > /home/student/public_html/index.html

# 设置acl权限
[root@web ~]# setfacl -m u:apache:x /home/student
## 查看设置的acl权限
[root@web ~]# getfacl /home/student
getfacl: Removing leading '/' from absolute path names
# file: home/student
# owner: student
# group: student
user::rwx
user:apache:--x
group::---
mask::--x
other::---

# 修改配置文件
[root@web ~]# vim /etc/httpd/conf.d/userdir.conf
## UserDir disabled 改为 UserDir public_html
## 添加以下内容
<Directory "/home/student/public_html">
    require all granted
</Directory>

# 设置 SELinux 权限
[root@web ~]# chcon -R -t httpd_sys_content_t /home/student/
  • 验证,在浏览器输入http://10.10.10.54/~student/ 访问成功
    在这里插入图片描述
4、编写脚本 selinux.sh,实现开启或禁用 SELinux 功能。
  • 脚本
#!/bin/bash
if [ $# -eq 0 ];then
        echo 请输入参数,ON 为启用,OFF为禁用。
elif [[ $1 =~ [Oo][Nn] ]];then
        sed -i "s/^SELINUX=[[:alpha:]].*/SELINUX=enforcing/" /etc/selinux/config
        echo SELinux 已启用,重启后生效。
elif [[ $1 =~ [Oo][Ff][Ff] ]];then
        sed -i "s/^SELINUX=[[:alpha:]].*/SELINUX=disabled/" /etc/selinux/config
        echo SELinux 已禁用,重启后生效。
else
        echo 参数错误,未做任何设置。
fi
  • 执行
[root@localhost ~]# chmod +x selinux.sh

# 启用
[root@localhost ~]# ./selinux.sh on
SELinux 已启用,重启后生效。

# 禁用
[root@localhost ~]# ./selinux.sh off
SELinux 已禁用,重启后生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值