
本文档详细说明了如何配置Linux云服务器仅允许SSH证书登录,禁止密码登录,以提高服务器安全性。
在Windows PowerShell中执行:
# 生成4096位RSA密钥对
ssh-keygen -t rsa -b 4096 -f "$env:USERPROFILE\.ssh\id_rsa_server" -C "admin@server"参数说明:
-t rsa:使用RSA算法-b 4096:密钥长度为4096位(更安全)-f:指定密钥文件路径-C:添加注释(通常是邮箱)执行后会提示:
Enter passphrase (empty for no passphrase): 建议:
如果您已经有SSH密钥,可以跳过此步骤,直接使用现有密钥。
生成后会创建两个文件:
C:\Users\YourUsername\.ssh\id_rsa_serverC:\Users\YourUsername\.ssh\id_rsa_server.pub⚠️ 重要提示:
ssh-copy-id -i ~/.ssh/id_rsa_server.pub root@服务器IPGet-Content "$env:USERPROFILE\.ssh\id_rsa_server.pub"$env:Path += ";$env:SystemRoot\System32\OpenSSH"
ssh root@服务器IPmkdir -p ~/.ssh
chmod 700 ~/.sshecho "您的公钥内容" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keyscat ~/.ssh/authorized_keys# 一次性完成公钥添加
$env:Path += ";$env:SystemRoot\System32\OpenSSH"
$publicKey = Get-Content "$env:USERPROFILE\.ssh\id_rsa_server.pub"
ssh root@服务器IP "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '$publicKey' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"ssh -i "$env:USERPROFILE\.ssh\id_rsa_server" root@服务器IP如果可以成功登录,说明公钥配置正确!
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak# 禁用密码登录
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
# 启用公钥认证
echo 'PubkeyAuthentication yes' >> /etc/ssh/sshd_configvim /etc/ssh/sshd_config找到并修改以下配置项:
# 禁用密码登录
PasswordAuthentication no
# 启用公钥认证
PubkeyAuthentication yes
# 确保以下配置(可选,提高安全性)
PermitRootLogin prohibit-password保存并退出:
Esc 键:wqEnter 键grep -E '(PasswordAuthentication|PubkeyAuthentication)' /etc/ssh/sshd_config预期输出:
PasswordAuthentication no
PubkeyAuthentication yessystemctl restart sshdsystemctl status sshd预期输出:
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since ...保持当前SSH连接不要断开!(防止配置错误导致无法登录)
打开新的终端窗口,测试证书登录:
ssh -i "$env:USERPROFILE\.ssh\id_rsa_server" root@服务器IP如果可以成功登录,说明配置正确!
ssh -o StrictHostKeyChecking=no -o PreferredAuthentications=password root@服务器IP "echo '测试'"预期结果:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic)如果看到这个错误,说明密码登录已成功禁用!
$env:Path += ";$env:SystemRoot\System32\OpenSSH"
ssh -i "C:\Users\YourUsername\.ssh\id_rsa_server" -o StrictHostKeyChecking=no root@服务器IP在Windows上创建或编辑 C:\Users\YourUsername\.ssh\config 文件:
Host server-alias
HostName 服务器IP
User root
Port 22
IdentityFile C:\Users\YourUsername\.ssh\id_rsa_server
StrictHostKeyChecking no参数说明:
Host:服务器别名(自定义)HostName:服务器IP地址User:登录用户名Port:SSH端口(默认22)IdentityFile:私钥文件路径StrictHostKeyChecking no:跳过主机密钥确认(可选)ssh server-alias编辑PowerShell配置文件 $PROFILE:
notepad $PROFILE添加以下函数:
function Connect-CloudServer {
$env:Path += ";$env:SystemRoot\System32\OpenSSH"
ssh -i "C:\Users\YourUsername\.ssh\id_rsa_server" -o StrictHostKeyChecking=no root@服务器IP
}
# 设置别名
Set-Alias ccs Connect-CloudServer使用时只需输入:
ccssystemctl status sshdcat /etc/ssh/sshd_configcat ~/.ssh/authorized_keyssystemctl restart sshd# 查看最近的SSH登录记录
journalctl -u sshd -n 50
# 实时监控SSH登录日志
journalctl -u sshd -fwhonetstat -antp | grep sshd | wc -l可能原因:
解决方法:
Test-Path "C:\Users\YourUsername\.ssh\id_rsa_server"chmod 600 ~/.ssh/id_rsa_servercat ~/.ssh/authorized_keys可能原因:
解决方法:
grep PasswordAuthentication /etc/ssh/sshd_configno:PasswordAuthentication nosystemctl restart sshd紧急恢复方法:
如果您在配置后无法登录服务器,可以通过以下方式恢复:
cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config
systemctl restart sshd可能原因:
解决方法:
systemctl status sshd# CentOS/RHEL
firewall-cmd --list-all
# Ubuntu/Debian
ufw statusnetstat -tlnp | grep 22解决方法:
Get-Content "C:\Users\YourUsername\.ssh\id_rsa_server" | Select-Object -First 3预期输出:
-----BEGIN OPENSSH PRIVATE KEY-----
或
-----BEGIN RSA PRIVATE KEY-----在生成密钥时设置密码短语,即使私钥泄露,攻击者也需要密码才能使用。
建议每6-12个月更换一次SSH密钥。
在 /etc/hosts.allow 和 /etc/hosts.deny 中配置IP白名单。
修改SSH端口,避免暴力破解:
vim /etc/ssh/sshd_config
# 修改 Port 22 为其他端口,如 Port 2222
systemctl restart sshd安装fail2ban防止暴力破解:
# CentOS/RHEL
yum install fail2ban
# Ubuntu/Debian
apt install fail2ban创建普通用户,使用sudo提权:
# 创建普通用户
adduser username
usermod -aG sudo username
# 修改SSH配置
vim /etc/ssh/sshd_config
# 修改 PermitRootLogin yes 为 PermitRootLogin no
systemctl restart sshd配置项 | 说明 | 推荐值 |
|---|---|---|
PasswordAuthentication | 是否允许密码登录 | no |
PubkeyAuthentication | 是否允许公钥认证 | yes |
PermitRootLogin | 是否允许root登录 | prohibit-password |
Port | SSH监听端口 | 22(可修改) |
PermitEmptyPasswords | 是否允许空密码 | no |
MaxAuthTries | 最大认证尝试次数 | 3 |
类型 | 密钥长度 | 安全性 | 兼容性 | 推荐度 |
|---|---|---|---|---|
RSA | 2048/4096 | 高 | 极好 | ⭐⭐⭐⭐⭐ |
ECDSA | 256/384/521 | 极高 | 好 | ⭐⭐⭐⭐ |
Ed25519 | 256 | 极高 | 较好 | ⭐⭐⭐⭐⭐ |
# 端口转发
ssh -L 本地端口:目标地址:目标端口 用户@服务器
# 文件传输
scp -i 私钥文件 本地文件 用户@服务器:/远程路径
# 远程命令执行
ssh 用户@服务器 "命令"
# SSH隧道
ssh -D 本地端口 用户@服务器通过本文档的配置,您的服务器已经实现了:
✅ 仅允许SSH证书登录 ✅ 禁用密码登录 ✅ 提高服务器安全性
⚠️ 重要提醒:
文档版本: 1.0 更新日期: 2026-01-16 适用系统: CentOS 7+, Ubuntu 18+, Debian 9+ 客户端系统: Windows 10/11, macOS, Linux
本文分享自 IT狂人日志58446291 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!