在 CentOS(7/8/Stream) 上加固 SSH 安全,可以从 端口、认证、协议、访问控制、审计 等多个层面进行。下面是一套生产环境常见且实用的加固方案,按优先级整理。
vim /etc/ssh/sshd_config
Port 22222
然后重启:
systemctl restart sshd
✅ 注意:
PermitRootLogin no
✅ 建议:
sudoPasswordAuthentication no
PubkeyAuthentication yes
ssh-keygen -t ed25519
ssh-copy-id user@server
AllowUsers user1 user2
# 或
DenyUsers root test
Protocol 2
Ciphers aes128-ctr,aes192-ctr,aes256-ctr
MACs hmac-sha2-256,hmac-sha2-512
KexAlgorithms curve25519-sha256,diffie-hellman-group16-sha512
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
yum install -y epel-release
yum install -y fail2ban
配置:
vim /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 22222
logpath = /var/log/secure
maxretry = 3
bantime = 3600
启动:
systemctl enable fail2ban
systemctl start fail2ban
firewall-cmd --permanent --add-port=22222/tcp
firewall-cmd --reload
✅ 建议:仅允许指定 IP
firewall-cmd --permanent --add-rich-rule='
rule family="ipv4" source address="1.2.3.4" port protocol="tcp" port="22222" accept'
getenforce
应为:
Enforcing
journalctl -u sshd
# 或
/var/log/secure
vim /etc/ssh/sshd_config
PrintLastLog yes
[Service]
LimitNOFILE=10000
Port 22222
Protocol 2
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
AllowUsers deploy admin
| 项目 | 是否完成 |
|---|---|
| 修改 SSH 端口 | ✅ |
| 禁用 root 登录 | ✅ |
| 使用密钥认证 | ✅ |
| fail2ban 启用 | ✅ |
| 防火墙限制 | ✅ |
| SELinux 开启 | ✅ |
如果你愿意,我可以:
只要告诉我你的 CentOS 版本 + 是否公网服务器 即可。