Linux服务器安全加固:从系统层到应用层的纵深防御
在云计算时代,服务器安全是所有运维工作的基石。无论使用AWS、阿里云还是自建机房,一台未加固的服务器在互联网上平均存活时间不到30分钟便会遭遇自动化扫描攻击。本文将从系统层、网络层、应用层三个维度,系统性地介绍Linux服务器安全加固的完整方案。
一、系统层加固:最小化攻击面
安全加固的第一原则是"最小权限"——只开放必要的服务,只保留必要的软件包,只赋予必要的权限。以下是从系统层面进行加固的关键操作。
1.1 用户与权限管理
# 禁用root远程登录
sed -i 's/^PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
# 创建普通管理员用户
useradd -m -s /bin/bash admin
passwd admin
# 配置sudo权限(无需密码的特定命令)
echo "admin ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx" >> /etc/sudoers.d/admin
echo "admin ALL=(ALL) NOPASSWD: /usr/bin/journalctl" >> /etc/sudoers.d/admin
# 禁用不必要的系统账户
for user in games lp news uucp; do
usermod -L "$user" 2>/dev/null
done
1.2 SSH安全配置
SSH是服务器管理的主要入口,也是最常被攻击的服务。以下配置大幅提升SSH安全性:
# /etc/ssh/sshd_config 关键安全配置
Port 2220 # 修改默认端口
PermitRootLogin no # 禁止root登录
PasswordAuthentication no # 禁用密码认证
PubkeyAuthentication yes # 启用密钥认证
MaxAuthTries 3 # 最大尝试次数
LoginGraceTime 30 # 登录超时
AllowUsers admin@10.0.0.0/8 # 限制来源IP
ClientAliveInterval 300 # 空闲超时检测
ClientAliveCountMax 2 # 最大空闲次数
密钥认证建议使用Ed25519算法,密钥长度更短但安全性更高:
# 客户端生成密钥对
ssh-keygen -t ed25519 -C "admin@prod-server"
# 将公钥部署到服务器
ssh-copy-id -p 2220 admin@server-ip
二、网络层加固:防火墙与入侵检测
2.1 iptables/nftables 防火墙策略
防火墙策略应遵循"默认拒绝,显式允许"原则:
# nftables 配置示例
table inet firewall {
chain input {
type filter hook input priority 0; policy drop;
# 允许已建立和相关连接
ct state established,related accept
# 允许回环接口
iif lo accept
# 允许SSH(新端口)
tcp dport 2220 ct state new accept
# 允许HTTP/HTTPS
tcp dport {80, 443} ct state new accept
# 允许ICMP(限制速率)
icmp type echo-request limit rate 5/second accept
# 记录并拒绝其他流量
log prefix "DROPPED: " drop
}
chain output {
type filter hook output priority 0; policy accept;
}
chain forward {
type filter hook forward priority 0; policy drop;
}
}
2.2 Fail2ban 自动封禁
Fail2ban通过分析日志自动封禁可疑IP,是对防火墙的重要补充:
# /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2220
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
findtime = 600
bantime = 3600
action = iptables-multiport[name=sshd, port="2220", protocol=tcp]
[nginx-http-auth]
enabled = true
port = http,https
filter = nginx-http-auth
logpath = /var/log/nginx/error.log
maxretry = 5
bantime = 7200
三、应用层加固:服务与运行时安全
3.1 系统服务最小化
# 列出所有监听端口
ss -tulnp
# 禁用不必要的服务
systemctl disable avahi-daemon
systemctl disable cups
systemctl disable bluetooth
systemctl stop avahi-daemon cups bluetooth
# 检查已启用服务列表
systemctl list-unit-files --state=enabled
3.2 文件系统与内核参数
内核参数调优能有效抵御部分网络攻击:
# /etc/sysctl.d/99-security.conf
# 禁用IP转发
net.ipv4.ip_forward = 0
# 禁用源路由
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
# 启用反向路径过滤
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# 禁用ICMP重定向
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# SYN Flood防护
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
# 禁用核心转储
fs.suid_dumpable = 0
四、审计与监控:构建可观测安全体系
安全不是一次性工作,而是持续运营的过程。构建完善的审计与监控体系,是发现和响应安全事件的关键。
4.1 文件完整性监控
# 安装AIDE
apt install aide
# 初始化数据库
aideinit
# 每日检查计划任务
cat > /etc/cron.daily/aide-check << 'EOF'
#!/bin/bash
/usr/bin/aide --check >> /var/log/aide/check-$(date +%Y%m%d).log 2>&1
# 发现变更时发送告警
if [ $? -ne 0 ]; then
mail -s "AIDE: 检测到文件变更" admin@example.com < /var/log/aide/check-$(date +%Y%m%d).log
fi
EOF
chmod +x /etc/cron.daily/aide-check
4.2 审计日志配置
# /etc/audit/auditd.conf 关键配置
max_log_file = 100
max_log_file_action = ROTATE
num_logs = 10
# 审计关键系统调用
auditctl -a always,exit -F arch=b64 -S chmod,chown -F auid>=1000 -k perm_change
auditctl -a always,exit -F arch=b64 -S execve -F auid>=1000 -k user_commands
auditctl -w /etc/sudoers -p wa -k sudoers_change
auditctl -w /etc/ssh/sshd_config -p wa -k sshd_config_change
五、自动化加固脚本
将上述加固措施整合为一个可复用的脚本,实现新服务器的快速安全初始化:
#!/bin/bash
# server-harden.sh - Linux服务器一键安全加固
set -euo pipefail
echo "===== 开始安全加固 ====="
# 1. 系统更新
apt update && apt upgrade -y
# 2. 配置SSH
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sed -i 's/^#PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/^#PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
# 3. 配置防火墙
apt install -y nftables
nft -f /etc/nftables-security.conf
systemctl enable nftables
# 4. 安装Fail2ban
apt install -y fail2ban
systemctl enable fail2ban
# 5. 内核参数
sysctl --system
# 6. 安装AIDE
apt install -y aide
aideinit
echo "===== 安全加固完成 ====="
安全加固是一个持续迭代的过程。建议定期复查配置、更新规则、分析日志,结合漏洞情报动态调整防御策略,才能构建真正可靠的服务器安全体系。