在SSH密钥认证体系中,公钥的复制方向是:将客户端(MacBook)的公钥复制到服务器(Ubuntu)。以下是关键原理和操作步骤的澄清:
id_rsa_tencent
)。id_rsa_tencent.pub
)在 ~/.ssh/authorized_keys
文件中。~/.ssh/known_hosts
文件中,用于验证服务器身份(防止中间人攻击)。这与用户身份认证的公钥无关。ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_tencent |
id_rsa_tencent
(保密!勿泄露!)id_rsa_tencent.pub
(需上传到服务器)方法一:使用ssh-copy-id
(推荐)
ssh-copy-id -i ~/.ssh/id_rsa_tencent.pub username@your_server_ip |
输入服务器密码后,公钥会自动添加到 ~/.ssh/authorized_keys
。
方法二:手动追加公钥
cat ~/.ssh/id_rsa_tencent.pub | ssh username@your_server_ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" |
# 登录服务器,检查authorized_keys内容 |
|
cat ~/.ssh/authorized_keys |
|
# 应包含MacBook公钥内容(以ssh-rsa开头,[email protected]结尾) |
在MacBook的 ~/.ssh/config
中添加:
Host tencent-ubuntu |
|
HostName your_server_ip |
|
User ubuntu |
|
Port 22 |
|
IdentityFile ~/.ssh/id_rsa_tencent # 指定MacBook的私钥路径 |
|
IdentitiesOnly yes |
连接时提示“Agent admitted failure to sign”
确保SSH代理正在运行并加载了私钥:
eval "$(ssh-agent -s)" |
|
ssh-add ~/.ssh/id_rsa_tencent |
公钥已上传但仍要求密码
检查服务器SSH配置:
sudo vi /etc/ssh/sshd_config |
|
# 确保以下参数为yes |
|
PubkeyAuthentication yes |
|
AuthorizedKeysFile .ssh/authorized_keys |
|
# 重启SSH服务 |
|
sudo systemctl restart sshd |
authorized_keys
。id_rsa_tencent
)仅保存在本地,不可泄露。known_hosts
,无需手动操作。按照此流程配置后,VSCode即可通过SSH密钥无缝连接腾讯云Ubuntu服务器。