服务器安全加固不是装个防火墙就完事的工作。一次完整的加固覆盖SSH访问控制、系统服务裁剪、文件权限收紧、内核参数调优、入侵检测等多个层面。这篇文章按操作顺序给出每一步的具体命令和配置。
SSH访问控制:关闭密码登录,启用密钥+2FA
SSH是服务器最常见的攻击入口。加固步骤:
1. 生成密钥对(在本地执行):
ssh-keygen -t ed25519 -C "prod-server-access"
# 将公钥上传到服务器
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@your-server
2. 修改sshd配置/etc/ssh/sshd_config:
# 禁用密码认证
PasswordAuthentication no
ChallengeResponseAuthentication no
# 禁用root密码登录,只允许密钥
PermitRootLogin prohibit-password
# 限制可登录用户
AllowUsers deploy admin
# 修改默认端口(减少扫描)
Port 2222
# 登录超时
LoginGraceTime 30
MaxAuthTries 3
3. 安装并配置Google Authenticator实现2FA:
apt install libpam-google-authenticator -y
google-authenticator # 每个用户执行,生成TOTP密钥
在/etc/pam.d/sshd添加:
auth required pam_google_authenticator.so nullok
重启sshd前务必保留一个已登录的会话,防止配置错误锁死自己。
系统服务裁剪:关掉不必要的服务
每个多余的服务都是攻击面。检查并关闭:
# 查看所有监听端口
ss -tlnp
# 常见需要关闭的服务
systemctl disable --now avahi-daemon # 除非用mDNS
systemctl disable --now cups # 除非是打印服务器
systemctl disable --now bluetooth # 服务器不需要蓝牙
systemctl disable --now rpcbind # 除非用NFS
# 检查还有哪些服务在跑
systemctl list-units --type=service --state=running
防火墙配置:只开必要端口
用iptables或nftables做白名单策略:
# nftables配置示例 /etc/nftables.conf
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
# 允许回环
iif lo accept
# 已建立连接
ct state established,related accept
# SSH(修改过的端口)
tcp dport 2222 accept
# HTTP/HTTPS
tcp dport { 80, 443 } accept
# 限ICMP
icmp type echo-request limit rate 5/second accept
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
systemctl enable --now nftables
内核安全参数调优
在/etc/sysctl.d/99-security.conf中添加:
# 禁用IP转发(除非是路由器)
net.ipv4.ip_forward = 0
# 防SYN Flood
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
# 禁止响应ICMP广播
net.ipv4.icmp_echo_ignore_broadcasts = 1
# 禁止源路由
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
# 重定向防护
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
# 内核指针保护
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
# 限制core dump
fs.suid_dumpable = 0
执行sysctl --system生效。
文件权限与审计
关键目录权限收紧:
# /tmp 设置noexec,nosuid
mount -o remount,noexec,nosuid /tmp
# 限制crontab访问
echo "root" > /etc/cron.allow
chmod 600 /etc/cron.allow
# 敏感文件权限
chmod 700 /root
chmod 600 /etc/shadow
chmod 600 /etc/gshadow
安装审计系统:
apt install auditd -y
# 监控关键文件变更
auditctl -w /etc/passwd -p wa -k passwd_changes
auditctl -w /etc/ssh/sshd_config -p wa -k sshd_config
auditctl -w /etc/sudoers -p wa -k sudoers_changes
# 监控sudo使用
auditctl -a always,exit -F arch=b64 -C euid=0 -F auid>=1000 -F auid!=4294967295 -k sudo_actions
入侵检测与自动告警
安装Fail2Ban防暴力破解:
apt install fail2ban -y
# /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
findtime = 600
bantime = 3600
文件完整性检测用AIDE:
apt install aide -y
aideinit # 初始化数据库
aide --update # 更新数据库(定期cron执行)
aide --check # 检查变更
问题排查手册
Q: SSH配置改完连不上了?
先别关当前session,另开终端测试。最常见的问题是密钥权限不对:chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys。
Q: nftables配完连不上?
确保SSH端口在白名单里。如果锁死了,通过云厂商的VNC/控制台登录修复。
Q: sysctl参数不生效?
检查sysctl -a | grep 参数名确认值。某些参数需要重启才生效,sysctl --system只是加载配置。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/linux-fu-wu-qi-an-quan-jia-gu-shi-zhan-cong-ssh-dao-nei-he/