如何在VPS上设置SSH Key?详细步骤是什么?
| 步骤 |
操作 |
工具/命令 |
| 1 |
生成SSH Key对 |
ssh-keygen -t rsa -b 4096 |
| 2 |
将公钥上传到VPS |
ssh-copy-id user@vpsip |
| 3 |
修改SSH配置文件 |
nano /etc/ssh/sshdconfig |
| 4 |
重启SSH服务 |
systemctl restart sshd |
VPS设置SSH Key的详细指南
SSH Key是一种比密码更安全的身份验证方式,适用于VPS远程管理。以下是设置SSH Key的完整步骤:
一、生成SSH Key对
- 操作说明:在本地计算机生成SSH Key对(私钥和公钥)
- 使用工具:终端(Linux/macOS)或Git Bash(Windows)
- 命令示例:
ssh-keygen -t rsa -b 4096
- 按提示选择保存位置(默认
~/.ssh/idrsa)
- 设置密码短语(可选但推荐)
二、上传公钥到VPS
- 操作说明:将公钥内容添加到VPS的
authorizedkeys文件
- 推荐方法:
ssh-copy-id user@vpsip
- 替换
user为VPS用户名
- 替换
vpsip为VPS实际IP地址
三、配置SSH服务
- 操作说明:修改VPS上的SSH配置文件
- 编辑文件:
sudo nano /etc/ssh/sshdconfig
- 关键配置项:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorizedkeys
PasswordAuthentication no # 禁用密码登录(可选)
四、重启SSH服务
sudo systemctl restart sshd
常见问题解决方案
| 问题 |
原因 |
解决方案 |
| 连接被拒绝 |
防火墙阻止 |
检查iptables或ufw设置 |
| 权限错误 |
文件权限不正确 |
执行chmod 700 ~/.ssh和chmod 600 ~/.ssh/authorizedkeys |
| 认证失败 |
公钥未正确配置 |
检查authorizedkeys文件内容格式 |
注意:禁用密码登录前,请确保SSH Key测试连接成功,避免被锁定在VPS外。
发表评论