Linux服务器暴露在公网环境下面临持续的端口扫描和暴力破解攻击。SSH服务作为远程管理入口,是攻击者首选目标。本文从SSH服务端配置、iptables防火墙规则编写、Fail2ban入侵防护三个维度,给出完整的服务器安全加固操作步骤。
SSH服务端安全配置详解
SSH默认配置存在多项安全隐患:root直接登录、密码认证、默认22端口。以下配置项需逐项修改,修改文件为/etc/ssh/sshd_config:
# 禁止root用户直接SSH登录
PermitRootLogin no
# 禁用密码认证,强制使用密钥登录
PasswordAuthentication no
PubkeyAuthentication yes
# 修改默认端口(避开常见扫描端口)
Port 22022
# 限制可登录用户白名单
AllowUsers deploy admin
# 设置登录超时与最大尝试次数
LoginGraceTime 30
MaxAuthTries 3
# 禁用空密码
PermitEmptyPasswords no
# 禁用X11转发(除非需要图形界面)
X11Forwarding no
# 设置客户端空闲超时自动断开
ClientAliveInterval 300
ClientAliveCountMax 2
修改前先生成SSH密钥对并完成免密登录配置,否则禁用密码认证后将无法远程登录:
# 在客户端生成密钥对
ssh-keygen -t ed25519 -C "deploy@server" -f ~/.ssh/deploy_key
# 将公钥上传到服务器
ssh-copy-id -i ~/.ssh/deploy_key.pub -p 22 deploy@server_ip
# 验证密钥登录成功后再重启SSH服务
sudo systemctl restart sshd
密钥算法选择ed25519而非RSA。ed25519密钥更短(68字符 vs RSA-2048的372字符),计算速度更快,且在同等密钥长度下安全性更高。ed25519的签名验证速度约为RSA-2048的5倍。
iptables防火墙规则编写与策略
iptables是Linux内核级包过滤防火墙,通过规则链对网络数据包进行匹配和过滤。安全加固的核心策略是”默认拒绝,显式允许”——先放行必要服务端口,最后设置默认DROP规则。
# 清除现有规则
iptables -F
iptables -X
iptables -Z
# 设置默认策略:允许出站,拒绝入站和转发
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 允许回环接口
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# 允许已建立连接和相关连接的数据包通过
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许SSH(自定义端口22022),限制源IP段
iptables -A INPUT -p tcp --dport 22022 -s 192.168.1.0/24 -j ACCEPT
# 允许HTTP和HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许ICMP(ping),限制速率防止ICMP Flood
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
# 防止SYN Flood攻击
iptables -A INPUT -p tcp --syn -m limit --limit 20/s --limit-burst 40 -j ACCEPT
iptables -A INPUT -p tcp --syn -j DROP
# 记录被拒绝的连接(调试用,上线后可关闭)
iptables -A INPUT -j LOG --log-prefix "iptables-drop: " --log-level 4
# 保存规则(CentOS/RHEL)
service iptables save
# 或(Debian/Ubuntu)
iptables-save > /etc/iptables/rules.v4
SSH端口的源IP限制是关键安全措施。如果管理终端的IP固定,直接限制为单一IP最安全:-s 203.0.113.50/32。如果管理终端IP不固定但属于特定运营商网段,限制为该网段。如果完全无法限制IP,则必须配合Fail2ban做暴力破解防护。
iptables规则的连接数限制与端口扫描防护
对单IP的并发连接数和新建连接速率做限制,可有效防御端口扫描和CC攻击:
# 限制单IP并发连接数为50
iptables -A INPUT -p tcp --syn -m connlimit --connlimit-above 50 -j DROP
# 限制单IP每秒新建连接数不超过10个
iptables -A INPUT -p tcp --syn -m recent --name CONNLIMIT --set
iptables -A INPUT -p tcp --syn -m recent --name CONNLIMIT --update --seconds 1 --hitcount 10 -j DROP
# 防止端口扫描:单IP在60秒内连接超过20个不同端口则封禁
iptables -A INPUT -p tcp --tcp-flags SYN,ACK,FIN,RST SYN -m recent --name PORTSCAN --set
iptables -A INPUT -p tcp --tcp-flags SYN,ACK,FIN,RST SYN -m recent --name PORTSCAN --update --seconds 60 --hitcount 20 -j DROP
iptables -A INPUT -p tcp --tcp-flags SYN,ACK,FIN,RST SYN -m recent --name PORTSCAN --remove
connlimit模块限制并发连接数,recent模块追踪近期连接记录。两者配合形成双重防护:connlimit防止瞬时大量并发,recent防止慢速持续扫描。端口扫描防护的阈值60秒20端口是经验值,正常用户不会在1分钟内尝试连接20个不同端口。
Fail2ban入侵防护部署
Fail2ban通过监控服务日志,自动封禁触发规则的IP地址。与iptables配合使用,形成动态防护:
# 安装
apt install fail2ban -y
# 创建本地配置(不直接修改jail.conf)
cat > /etc/fail2ban/jail.local << 'EOF'
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
banaction = iptables-multiport
[sshd]
enabled = true
port = 22022
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 86400
[nginx-limit-req]
enabled = true
port = http,https
filter = nginx-limit-req
logpath = /var/log/nginx/error.log
maxretry = 5
bantime = 7200
EOF
systemctl enable fail2ban
systemctl start fail2ban
fail2ban-client status sshd
SSH的封禁时间设为24小时(86400秒),比默认的1小时更严格。原因是SSH暴力破解通常来自僵尸网络,攻击者会在被封禁后长时间不再重试,延长封禁时间可减少iptables规则膨胀。
Nginx的limit-req防护对CC攻击有效。需要配合Nginx配置限制单个IP的请求速率:
# nginx.conf中添加
http {
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
server {
location / {
limit_req zone=req_limit burst=20 nodelay;
limit_req_status 429;
}
}
}
burst=20允许短时间突发请求(20个),超出后返回429状态码。Fail2ban的nginx-limit-req过滤器监控Nginx错误日志中触发限流的记录,同一IP触发5次限流即封禁2小时。
加固效果验证与安全审计
加固完成后,使用以下方法验证防护效果:
# 使用nmap扫描验证端口暴露
nmap -sS -p 1-65535 server_ip
# 预期输出:只有22022、80、443端口为open
# 模拟暴力破解测试Fail2ban
for i in $(seq 1 5); do
sshpass -p "wrong_pass_$i" ssh -p 22022 fakeuser@server_ip 2>/dev/null
done
# 检查Fail2ban是否自动封禁
fail2ban-client status sshd
# 预期:Banned IP list中包含测试IP
# 查看iptables规则
iptables -L -n -v --line-numbers
安全加固不是一次性工作。定期审查/var/log/auth.log中的登录失败记录,根据攻击模式调整Fail2ban阈值。每月执行一次nmap外部扫描验证端口暴露情况,确保没有意外开放的服务端口。SSH端口的变更需同步更新监控告警系统,避免安全加固导致正常的运维监控被误判为攻击行为。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/linux-fu-wu-qi-an-quan-jia-gu-shi-cao-ssh-pei-zhi-yu/