在信息化时代,服务器安全是每个系统管理员都需要重视的问题。CentOS 7作为一款流行的Linux发行版,它的安全加固也是必不可少的。今天,就让我们来了解一下如何使用一键脚本实现CentOS 7的自动化安全加固。
1. 前言
服务器安全加固主要包括以下几个方面:
- 基础配置:设置防火墙、关闭不必要的端口、设置root密码复杂度等。
- 系统更新:及时更新系统补丁,修复已知的安全漏洞。
- 软件安全:关闭不必要的服务,限制用户权限等。
- 日志审计:设置日志审计,实时监控系统日志,发现异常行为。
以下将详细阐述如何使用一键脚本实现CentOS 7的自动化安全加固。
2. 一键脚本内容
以下是一个CentOS 7安全加固的一键脚本示例,它包含了上述提到的各个方面:
#!/bin/bash
# 更新系统
yum update -y
# 设置root密码复杂度
sed -i '/root:*!$/a root:*:1500:' /etc/login.defs
# 安装防火墙
yum install -y firewalld
# 设置防火墙策略
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --reload
# 关闭不必要的服务
systemctl stop cups
systemctl disable cups
systemctl stop cups
systemctl disable cups
# 限制用户权限
setenforce 1
# 安装日志审计工具
yum install -y auditd
auditctl -w / -p warx -k files_read
# 设置日志审计策略
auditctl -a -S read -F path=/etc/passwd -k passwd_read
auditctl -a -S read -F path=/etc/shadow -k passwd_read
# 启用系统日志审计
systemctl enable auditd
systemctl start auditd
# 安装安全增强工具
yum install -y openssh-clients
# 重启ssh服务,更改默认端口
systemctl restart sshd
sed -i 's/^Port\ 22/Port\ 2222/' /etc/ssh/sshd_config
# 设置SSH密钥登录
ssh-keygen -t rsa -b 2048
mv /root/.ssh/id_rsa /root/.ssh/id_rsa.old
mv /root/.ssh/id_rsa.pub /root/.ssh/id_rsa.pub.old
ssh-keygen -t rsa -b 2048
# 配置SSH免密登录
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys
echo "安全加固完成!"
3. 使用方法
- 将上述脚本保存为
security_script.sh。 - 使用
chmod +x security_script.sh命令,赋予脚本执行权限。 - 使用
./security_script.sh命令执行脚本。
4. 注意事项
- 在执行脚本之前,请确保服务器已经安装了必要的软件包,例如
firewalld、auditd等。 - 执行脚本时,请谨慎操作,确保不会误删重要文件。
- 脚本中的防火墙策略仅供参考,请根据实际需求进行调整。
通过使用一键脚本,您可以快速实现CentOS 7的自动化安全加固。当然,这只是一个基础的安全配置,您还可以根据自己的需求,继续进行深入的安全加固。
