服务器安全加固全流程:SSH防护、内核调优与入侵检测实战配置

服务器安全加固的核心思路

服务器上线后不做安全加固,等于裸奔。SSH端口暴露22、root远程登录开放、防火墙全通——这种配置在公网环境下存活时间通常不超过24小时。安全加固不是装个杀毒软件就完事,需要从网络层、系统层、应用层三道防线逐层收紧。以下方案基于CentOS 8/Ubuntu 22.04环境验证。

SSH防护:堵住最常被暴力破解的入口

SSH是服务器被攻击的第一道口子。加固分三步走:

第一步:禁用root远程登录 + 禁用密码认证

# /etc/ssh/sshd_config 修改以下配置
Port 29722                          # 改掉默认端口
PermitRootLogin no                  # 禁止root远程登录
PasswordAuthentication no           # 禁用密码认证
PubkeyAuthentication yes            # 仅允许密钥认证
MaxAuthTries 3                      # 最大尝试次数
LoginGraceTime 30                   # 登录超时30秒
AllowUsers deploy ops               # 白名单用户

# 生成密钥对(在本地执行)
ssh-keygen -t ed25519 -C "deploy@prod-server" -f ~/.ssh/deploy_key

# 将公钥传到服务器
ssh-copy-id -i ~/.ssh/deploy_key.pub -p 22 deploy@your-server

改完配置别急着断开当前连接,新开一个终端测试密钥登录成功后再关闭旧会话,否则配置出错会把你自己锁在外面。

第二步:部署fail2ban自动封禁暴力IP

# 安装fail2ban
apt install fail2ban -y    # Ubuntu
yum install fail2ban -y    # CentOS

# /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 29722
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
findtime = 600
bantime = 86400
action = iptables-multiport[name=sshd, port="29722", protocol=tcp]

# 启动服务
systemctl enable fail2ban && systemctl start fail2ban

# 查看封禁状态
fail2ban-client status sshd

第三步:双因素认证叠加(可选但推荐)

对管理级账户启用Google Authenticator TOTP,密钥+动态码双重验证:

apt install libpam-google-authenticator -y
google-authenticator   # 按引导生成密钥和备用码

# /etc/pam.d/sshd 添加
auth required pam_google_authenticator.so nullok

# /etc/ssh/sshd_config 添加
AuthenticationMethods publickey,keyboard-interactive
ChallengeResponseAuthentication yes

内核级安全调优:关闭不必要的攻击面

内核参数sysctl调优,以下配置写入/etc/sysctl.d/99-hardening.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.ipv4.conf.all.send_redirects = 0

# SYN Flood防护
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_synack_retries = 2

# 禁止绑定非本机IP
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

# 内核核心转储限制
fs.suid_dumpable = 0

# 生效
sysctl -p /etc/sysctl.d/99-hardening.conf

同时禁用不需要的服务和内核模块:

# 禁用不必要的服务
systemctl disable avahi-daemon cups bluetooth rpcbind
systemctl stop avahi-daemon cups bluetooth rpcbind

# 禁用不必要的内核模块 /etc/modprobe.d/blacklist.conf
install dccp /bin/true
install sctp /bin/true
install rds /bin/true
install tipc /bin/true

防火墙策略:最小暴露原则

UFW(Ubuntu)或firewalld(CentOS)配置,只开放业务必需端口:

# UFW配置示例
ufw default deny incoming
ufw default allow outgoing
ufw allow 29722/tcp comment 'SSH'
ufw allow 80/tcp comment 'HTTP'
ufw allow 443/tcp comment 'HTTPS'
ufw allow from 10.0.0.0/8 to any port 9090 comment 'Prometheus内网'

# 限制SSH来源IP(更安全的方案)
ufw delete allow 29722/tcp
ufw allow from 203.0.113.0/24 to any port 29722 comment 'SSH信任网段'

ufw enable
ufw status verbose

生产环境强烈建议SSH端口仅对跳板机IP段开放,而不是对全网开放改端口后的SSH。

入侵检测:文件完整性监控与异常告警

安装AIDE(Advanced Intrusion Detection Environment)监控关键文件变更:

apt install aide -y

# 初始化数据库
aideinit
mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db

# 每日定时检查,结果发送到告警
# /etc/cron.daily/aide-check
#!/bin/bash
aide --check | mail -s "AIDE Report $(hostname) $(date +%Y-%m-%d)" admin@yourcompany.com

# chmod +x /etc/cron.daily/aide-check

结合OSSEC或Wazuh做实时日志分析,检测异常登录、提权操作、可疑进程,比纯文件完整性检查覆盖面更广。

安全加固检查清单

  • SSH默认端口已修改、root远程登录已禁止、密码认证已关闭
  • fail2ban已部署且封禁策略生效
  • sysctl内核参数已调优,SYN Flood防护已开启
  • 防火墙仅开放业务端口,SSH来源IP已限制
  • AIDE文件完整性监控已部署
  • 所有服务账户已禁用shell登录
  • 内核和系统包已更新到安全补丁版本

安全加固是持续过程,不是一次性操作。每月复查防火墙规则、检查fail2ban日志、更新AIDE基线,才能维持服务器的安全水位。

原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/fu-wu-qi-an-quan-jia-gu-quan-liu-cheng-ssh-fang-hu-nei-he/

(0)
小编小编
上一篇 17小时前
下一篇 17小时前

相关推荐

发表回复

登录后才能评论