1. 在本地生成密钥对
在本地电脑上执行(Windows/Mac/Linux 都可以):
ssh-keygen -t ed25519 -C "1111222@qq.com"
按提示操作:
- 保存位置:直接回车(默认
~/.ssh/id_ed25519) - 密码:可以设置密码增加安全性,或直接回车留空
- Enter file in which to save the key (C:\Users\Administrator/.ssh/id_ed25519):
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
旧系统兼容性:如果服务器不支持 ed25519,改用 RSA:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
2. 将公钥复制到服务器
先查看本地公钥内容:
在 C:\Users\Administrator.ssh\id_ed25519.pub
登录服务器,将公钥追加到授权文件:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "粘贴你的公钥内容" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
3. 配置 SSH 服务(增强安全性)
sudo vim /etc/ssh/sshd_config
# 禁用密码登录(确认能用密钥登录后再设置!)
PasswordAuthentication no
# 禁用 root 密码登录(如需允许 root 密钥登录,设为 prohibit-password)
PermitRootLogin prohibit-password
# 禁用空密码
PermitEmptyPasswords no
# 启用公钥认证
PubkeyAuthentication yes
注意:保存后,一定要先验证配置是否正确(非常重要!):
sudo sshd -t
如果没有错误输出,重启 SSH 服务:
sudo systemctl restart sshd
4 客户端 FinalShell 登录 选择私钥 位置
C:\Users\Administrator.ssh\id_ed25519