macbook配置vscode连接腾讯云服务器

在SSH密钥认证体系中,公钥的复制方向是:将客户端(MacBook)的公钥复制到服务器(Ubuntu)。以下是关键原理和操作步骤的澄清:


SSH公钥认证原理

  1. 客户端(MacBook) 持有 私钥(如 id_rsa_tencent)。
  2. 服务器(Ubuntu) 保存 客户端的公钥(如 id_rsa_tencent.pub)在 ~/.ssh/authorized_keys 文件中。
  3. 连接时,客户端用私钥生成签名,服务器用公钥验证签名,从而确认客户端身份。

❌ 常见误解:是否需要服务器公钥?

  • 不需要将Ubuntu服务器的公钥复制到MacBook
    服务器的主机公钥会在第一次连接时自动缓存在MacBook的 ~/.ssh/known_hosts 文件中,用于验证服务器身份(防止中间人攻击)。这与用户身份认证的公钥无关。

✅ 正确操作步骤(再次确认)

1. 在MacBook生成密钥对(若未生成)
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_tencent
  • 生成 私钥 id_rsa_tencent(保密!勿泄露!)
  • 生成 公钥 id_rsa_tencent.pub(需上传到服务器)
2. 将MacBook的公钥复制到Ubuntu服务器
  • 方法一:使用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"
3. 验证服务器配置
# 登录服务器,检查authorized_keys内容
cat ~/.ssh/authorized_keys
# 应包含MacBook公钥内容(以ssh-rsa开头,[email protected]结尾)
4. 配置VSCode的SSH连接

在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

总结

  • 公钥流向:MacBook的公钥 → Ubuntu服务器的 authorized_keys
  • 私钥保密:MacBook的私钥(id_rsa_tencent)仅保存在本地,不可泄露。
  • 主机验证:服务器公钥会自动缓存在MacBook的 known_hosts,无需手动操作。

按照此流程配置后,VSCode即可通过SSH密钥无缝连接腾讯云Ubuntu服务器。

你可能感兴趣的:(vscode,腾讯云,服务器)