























本文面向Linux运维初学者,系统讲解SSH安全登录的核心概念与生产环境最佳实践,帮助你筑牢服务器的第一道防线。内容由AI整理,注意辨别正确性。
生产环境中最主流、最有效的安全登录做法,可以总结为:
结论:除非你有必须连接仅支持RSA算法的超老旧系统,否则应优先选择 Ed25519。
Windows 10/11 的 PowerShell 或 CMD 已原生支持OpenSSH客户端。
ssh-keygen -t ed25519 -C "your_email@example.com"
-t ed25519:指定密钥类型为Ed25519。如果你想生成RSA 4096位密钥,可以使用 ssh-keygen -t rsa -b 4096 -C "your_email@example.com"。-C:添加一个注释,方便识别这个密钥的用途。C:\Users\你的用户名\.ssh\id_ed25519),并务必设置一个强密码。id_ed25519.pub) 和私钥文件 (id_ed25519) 将保存在你指定的目录下。如果你习惯使用图形界面工具,也可以使用 PuTTYgen,选择“EdDSA”和“Ed25519”生成密钥。
ssh-keygen -t ed25519 -C "your_email@example.com"
~/.ssh/ 目录下。生成密钥后,你需要将 公钥 (id_ed25519.pub) 的内容添加到服务器的 ~/.ssh/authorized_keys 文件中。最方便和安全的方法是使用 ssh-copy-id 命令。
使用 ssh-copy-id 命令(推荐)
在你本地的终端或PowerShell中运行:
ssh-copy-id -i ~/.ssh/id_ed25519.pub 你的用户名@你的服务器IP
输入服务器上该用户的密码后,公钥就会被自动追加到远程服务器的 ~/.ssh/authorized_keys 文件中。
手动添加
如果 ssh-copy-id 不可用,你也可以手动操作。首先,在本地查看公钥内容并复制:
cat ~/.ssh/id_ed25519.pub
然后,登录到你的服务器,并执行以下命令:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "这里粘贴你的公钥内容" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
mkdir -p ~/.ssh 用于确保 .ssh 目录存在,chmod 700 和 chmod 600 用于设置正确的权限,这是SSH安全所必需的。
~/.ssh/authorized_keys 是一个位于用户家目录下的文件,用于存放允许通过SSH公钥登录该用户的所有公钥。authorized_keys 是针对当前用户的。例如:
/home/alice/.ssh/authorized_keys 控制着哪些公钥可以登录 alice 账户。/root/.ssh/authorized_keys 控制着哪些公钥可以登录 root 账户。ssh-ed25519 AAA...1 Alice's laptop
ssh-ed25519 AAA...2 Bob's workstation
ssh-rsa AAA...3 CI/CD server
/etc/ssh/sshd_config 中的 AuthorizedKeysFile 指令,改变默认的公钥文件位置或指定一个全局文件。例如,可以配置 AuthorizedKeysFile .ssh/authorized_keys /etc/ssh/authorized_keys/%u,让sshd在检查用户目录后,再去全局目录下检查。SSHD(SSH Daemon)是运行在Linux服务器上的一个后台服务进程,它持续监听来自客户端的SSH连接请求,负责处理用户认证、建立加密会话、执行用户命令等所有与SSH相关的服务端工作。几乎所有Linux发行版都预装了OpenSSH,其中就包含了 sshd。
sshd 服务是否存在:which sshd
# 或
systemctl status sshd
sudo yum install openssh-server -y
sudo dnf install openssh-server -y
sudo apt update
sudo apt install openssh-server -y
sudo systemctl start sshd
sudo systemctl enable sshd
你可以通过 sudo systemctl status sshd 来确认服务是否正常运行。/etc/ssh/sshd_config)SSH服务器的主要配置文件是 /etc/ssh/sshd_config。在修改它之前,强烈建议先进行一次备份:sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak。
仅使用 SSH 协议 2:SSHv1存在设计缺陷,必须禁用。在配置文件中确保设置:
Protocol 2
如果该行被注释或不存在,说明只支持协议2,但显式声明是良好实践。
限制认证尝试次数:设置 MaxAuthTries 3~6,减少暴力破解窗口。
启用详细日志:设置 LogLevel VERBOSE,可以记录更详细的日志,包括指纹信息。
配置指令:PermitRootLogin
最佳实践:强烈建议设为 no,完全禁止Root用户通过SSH登录。
PermitRootLogin no
作为替代方案,日常使用一个普通用户登录,然后通过 sudo 执行需要管理员权限的命令。
请注意:如果你的系统是云服务器,部分镜像可能默认禁用了密码登录,但允许Root密钥登录。即使如此,在生产环境中,为了审计和最小权限原则,仍建议禁用Root登录。
备选方案(不推荐):如果确需临时保留密钥登录能力,可设为 PermitRootLogin prohibit-password,但更推荐完全禁用。
在禁用root登录之前,必须先创建一个可以执行管理命令的普通用户:
sudo useradd -m -s /bin/bash yourusername
sudo passwd yourusername
sudo usermod -aG wheel yourusername # CentOS/RHEL 使用 wheel 组
# 或 sudo usermod -aG sudo yourusername # Ubuntu/Debian 使用 sudo 组
sudo userdel -r zhangsan #删除用户
这两项通常是配合使用的。标准流程是:先用密码登录,配置好公钥,并测试通过后,再关闭密码登录。
yes。PubkeyAuthentication yes
no。PasswordAuthentication no
no。PermitEmptyPasswords no
AllowUsers:白名单机制,只允许指定的用户登录。这是最精确的访问控制。AllowUsers alice bob
# 更安全的是,结合IP地址进行限制
AllowUsers alice@192.168.1.100 bob@10.0.0.0/24
DenyUsers:黑名单机制,拒绝特定用户登录。DenyUsers badguy hacker
AllowGroups / DenyGroups:与用户组配合使用。例如,只允许wheel组的用户登录,这是一种常见做法。优先级:
DenyUsers>AllowUsers>DenyGroups>AllowGroups。
ChallengeResponseAuthentication no # 关闭质询-响应认证
GSSAPIAuthentication no # 关闭 GSSAPI 认证(如不使用 Kerberos)
KerberosAuthentication no # 关闭 Kerberos 认证
默认22端口是攻击者的首要目标,修改为1024-65535之间的未使用端口,可以降低自动化扫描风险:
Port 2222 # 或 Port 2025
⚠️ 修改端口前的回退策略:建议先保留Port 22与新端口并存,完成连通性与登录测试后再移除22,避免失联。
systemctl status firewalld #查看防火墙状态
sudo systemctl start firewalld #开启防火墙
sudo systemctl stop firewalld #关闭防火墙
sudo systemctl enable firewalld #加入开机自启
sudo systemctl disable firewalld #禁止开机自启
firewall-cmd --list-ports #查看防火墙已开放的端口
sudo firewall-cmd --permanent --add-port=28630/tcp #防火墙添加端口
sudo firewall-cmd --permanent --remove-port=28630/tcp #防火墙移除端口
修改端口后,需要同步开放防火墙端口:
使用 firewalld(CentOS/RHEL 7+):
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload
使用 iptables:
sudo iptables -A INPUT -p tcp --dport 2222 -j ACCEPT
sudo service iptables save
使用 ufw(Ubuntu/Debian):
sudo ufw allow 2222/tcp
SELinux = Linux 内核里的一套超级严格的安全管家
# 查看启用状态
getenforce
如果系统启用了SELinux,新增端口还需执行SELinux配置,否则sshd可能无法绑定新端口:
sudo semanage port -a -t ssh_port_t -p tcp 2222
除了防火墙端口开放外,还应在边界防火墙或云安全组层面仅允许可信IP/网段访问SSH端口。
使用 firewalld 限制来源IP:
# 先拒绝所有 IP 访问 2222
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" port port="2222" protocol="tcp" reject'
# 只允许 192.168.1.100 访问 SSH 端口
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="2222" accept'
sudo firewall-cmd --reload
使用 iptables 限制来源IP:
sudo iptables -A INPUT -p tcp --dport 2222 -s 192.168.1.100 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 2222 -j DROP
为了通过安全合规扫描并防御已知漏洞,应禁用弱加密算法,仅使用强加密算法。
CBC模式密码存在安全漏洞,应禁用并改用CTR模式。在 /etc/ssh/sshd_config 中设置:
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
禁用基于SHA-1的HMAC算法:
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512,hmac-sha2-256
在之前讨论过的 KexAlgorithms 基础上,移除不安全的算法:
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256
设置 ClientAliveInterval 300 与 ClientAliveCountMax 3,在连续15分钟无响应后自动断开,降低会话劫持风险:
ClientAliveInterval 300
ClientAliveCountMax 3
AllowTcpForwarding no
X11Forwarding no
使用 MaxStartups 控制并发连接的数量,达到最大连接数后sshd将拒绝新的连接请求并记录到日志中:
MaxStartups 10:30:100
格式为 start:rate:full,表示连接数达到10时开始以30%的概率拒绝新连接,达到100时完全拒绝。
手动查看日志并封禁IP是滞后的。Fail2ban 是一个强大的入侵防御工具,可以自动化这个过程。
安装 Fail2ban:
sudo yum install epel-release -y 然后 sudo yum install fail2ban -ysudo apt install fail2ban -y配置 Fail2ban:创建配置文件 /etc/fail2ban/jail.local,内容如下:
[sshd]
enabled = true
port = 2222 # 如果你修改了SSH端口,这里也要改
filter = sshd
logpath = /var/log/secure # Ubuntu/Debian 系统请改为 /var/log/auth.log
maxretry = 3 # 最多失败3次
bantime = 3600 # 封禁1小时 (单位: 秒)
启动 Fail2ban:
sudo systemctl start fail2ban
sudo systemctl enable fail2ban
查看封禁状态:
sudo fail2ban-client status sshd
解除封禁状态:
sudo fail2ban-client set sshd unbanip 192.168.1.100 #解除单个IP
sudo fail2ban-client unban --all #解除所有IP
为关键账户增加第二层校验,即使私钥泄露,攻击者也无法登录。可以使用Google Authenticator实现TOTP双因素认证。
Linux系统的认证相关日志通常记录在 /var/log/secure(CentOS/RHEL)或 /var/log/auth.log(Ubuntu/Debian)文件中。
sudo grep "Failed password" /var/log/secure
sudo grep "Failed password for invalid user root" /var/log/secure
sudo grep "106.38.205.224" /var/log/secure
journalctl -u sshd -f
sudo grep "Failed password" /var/log/secure | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr
除了手动查看日志,还可以使用专业的SSH审计工具进行全面扫描:
ssh-audit your_server_ip
srvaudit scan root@your-server.com
last
last -f /var/log/wtmp
定期检查 ~/.ssh/authorized_keys 文件,移除不再使用的或未知的公钥。对于多服务器环境,建议使用集中化的密钥管理方案。
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.baksudo systemctl restart sshdsudo systemctl status sshdss -tnlp | grep :2222 或 netstat -ntpl | grep 2222/var/log/secure,关注失败登录与异常来源sudo提权。authorized_keys 文件中移除对应的公钥。auditd 监控对 sshd_config 和 authorized_keys 文件的访问和修改。/etc/ssh/sshd_config)你可以参考以下配置块,构建你自己的安全SSH服务。再次强调,修改前备份,并谨慎测试。
# ========== 基础设置 ==========
Port 2222 # 修改为高位端口
Protocol 2 # 禁用老的 SSH 协议
ListenAddress 0.0.0.0 # 监听所有网络接口(可按需修改)
# ========== 认证与访问控制 ==========
PermitRootLogin no # 1. 禁止 root 登录
PubkeyAuthentication yes # 2. 启用公钥登录
PasswordAuthentication no # 3. 禁用密码登录
PermitEmptyPasswords no # 4. 禁止空密码
MaxAuthTries 3 # 5. 限制认证尝试次数
ChallengeResponseAuthentication no
GSSAPIAuthentication no
KerberosAuthentication no
# 6. 只允许特定用户或用户组登录 (强烈推荐)
AllowUsers alice bob
# 或者只允许 wheel 组的用户登录: AllowGroups wheel
# ========== 加密算法强化 ==========
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512,hmac-sha2-256
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256
# ========== 会话安全 ==========
ClientAliveInterval 300 # 7. 设置空闲超时(秒)
ClientAliveCountMax 3 # 超时前最多发送3次心跳
MaxStartups 10:30:100 # 控制并发连接数
# ========== 日志 ==========
SyslogFacility AUTHPRIV
LogLevel VERBOSE # 记录更详细的日志,如指纹信息
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。