您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Linux下如何强化SSH安全
## 引言
SSH(Secure Shell)是Linux系统中最重要的远程管理协议之一,它通过加密通道为系统管理员提供安全的远程访问。然而,默认配置的SSH服务往往存在诸多安全隐患,可能成为攻击者的突破口。本文将深入探讨16项关键措施,帮助您全面强化Linux服务器的SSH安全防护。
---
## 一、基础安全配置
### 1. 禁用SSH协议v1
```bash
# 编辑SSH配置文件
sudo nano /etc/ssh/sshd_config
# 确保以下配置
Protocol 2
原理说明:
SSHv1存在CRC32漏洞等严重缺陷,而SSHv2采用更安全的Diffie-Hellman密钥交换和强完整性检查。
Port 58222 # 建议使用1024-65535之间的端口
操作建议:
- 同时更新防火墙规则(如iptables/ufw)
- 在客户端使用~/.ssh/config
文件保存自定义端口
PermitRootLogin no
替代方案:
# 创建特权用户
sudo useradd -m -s /bin/bash adminuser
sudo usermod -aG sudo adminuser
PubkeyAuthentication yes
PasswordAuthentication no
密钥生成示例:
ssh-keygen -t ed25519 -a 100 # 推荐ed25519算法
ssh-copy-id -p 58222 user@host
# 安装PAM模块
sudo apt install libpam-pwquality
# 配置密码策略
sudo nano /etc/security/pwquality.conf
minlen = 12
dcredit = -1
ucredit = -1
ocredit = -1
lcredit = -1
# 安装Google Authenticator
sudo apt install libpam-google-authenticator
# 编辑PAM配置
auth required pam_google_authenticator.so
# 使用UFW示例
sudo ufw allow from 192.168.1.0/24 to any port 58222
sudo ufw deny 22/tcp
高级方案:
# 使用iptables限制连接频率
iptables -A INPUT -p tcp --dport 58222 -m conntrack --ctstate NEW -m recent --set
iptables -A INPUT -p tcp --dport 58222 -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 5 -j DROP
# 安装配置
sudo apt install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
定制配置:
[sshd]
enabled = true
port = 58222
maxretry = 3
bantime = 1h
findtime = 600
# 在sshd_config中添加
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org
检测工具:
ssh-audit localhost -p 58222
HostKeyAlgorithms ssh-ed25519,ssh-rsa-sha2-256
# 安装knockd
sudo apt install knockd
# 示例配置
[options]
UseSyslog
[openSSH]
sequence = 7000,8000,9000
seq_timeout = 10
command = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 58222 -j ACCEPT
# 使用Cowrie蜜罐
docker run -p 2222:2222 cowrie/cowrie
# sshd_config配置
LogLevel VERBOSE
PrintLastLog yes
日志分析示例:
# 统计失败登录
grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c
# 使用sshwatch
pip install sshwatch
sshwatch --port 58222 --interface eth0
# 强制密钥更新脚本
#!/bin/bash
for user in /home/*/.ssh/authorized_keys; do
sed -i '/ssh-rsa AAAAB3NzaC1yc2EAAAADAQAB/d' $user
done
# 自动化更新检查
apt install unattended-upgrades
dpkg-reconfigure -plow unattended-upgrades
通过实施以上16项安全措施,您的SSH服务将获得企业级的安全防护。安全是一个持续的过程,建议:
最终检查清单:
sshd -t # 测试配置有效性
systemctl restart sshd # 应用更改
通过分层防御策略,即使某个防护层被突破,其他安全措施仍能提供保护。记住,最安全的系统是那些假设自己已被入侵的系统。 “`
注:本文约2900字,实际字数可能因Markdown渲染方式略有差异。所有配置建议在测试环境验证后再应用于生产系统。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。