前言
服务器安全是运维工作的基础。本文整理了一套完整的Linux服务器安全加固方案,适用于云服务器和物理服务器。
一、SSH安全加固
1.1 禁用root远程登录
# /etc/ssh/sshd_config
PermitRootLogin no
1.2 修改SSH默认端口
# /etc/ssh/sshd_config
Port 22222 # 使用非标准端口
1.3 仅允许密钥认证
# /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
1.4 限制登录用户
# /etc/ssh/sshd_config
AllowUsers deploy admin
二、防火墙配置(firewalld)
只开放必要的端口:
# 启动防火墙
systemctl start firewalld
systemctl enable firewalld
# 开放SSH端口
firewall-cmd --permanent --add-port=22222/tcp
# 开放HTTP/HTTPS
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
# 重载规则
firewall-cmd --reload
# 查看已开放端口
firewall-cmd --list-ports
三、Fail2ban防暴力破解
Fail2ban通过监控日志文件,自动封禁多次登录失败的IP:
# 安装
yum install -y fail2ban
# 配置 /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 22222
filter = sshd
logpath = /var/log/secure
maxretry = 5
bantime = 3600
findtime = 600
# 启动
systemctl start fail2ban
systemctl enable fail2ban
# 查看封禁状态
fail2ban-client status sshd
四、系统安全配置
4.1 禁用不必要的服务
# 查看运行中的服务
systemctl list-units --type=service --state=running
# 禁用不需要的服务
systemctl disable telnet
systemctl disable rsh
4.2 内核安全参数
# /etc/sysctl.conf
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
五、自动安全更新
配置自动安装安全补丁:
# CentOS/RHEL
yum install -y yum-cron
# 编辑 /etc/yum/yum-cron.conf
update_cmd = security
apply_updates = yes
systemctl start yum-cron
systemctl enable yum-cron
六、日志审计
定期检查关键日志:
/var/log/secure- SSH登录记录/var/log/messages- 系统日志/var/log/audit/audit.log- 审计日志- 使用
last和lastb查看登录历史
七、安全检查清单
| 检查项 | 状态 | 说明 |
|---|---|---|
| SSH密钥认证 | ✅ | 禁用密码登录 |
| 防火墙配置 | ✅ | 只开放必要端口 |
| Fail2ban | ✅ | 防暴力破解 |
| 自动更新 | ✅ | 及时修补漏洞 |
| 日志监控 | ✅ | 发现异常行为 |
| 定期备份 | ✅ | 数据安全保障 |
安全是一个持续的过程,建议每月进行一次安全巡检,及时更新系统和软件版本。