VS Code 远程开发环境配置

316 阅读2分钟

1 Remote Development

1.1 创建ssh免密登录密钥

  1. SSH公钥known_hosts 创建密钥
# -f 可指定目录  D:\Home\.ssh\my_ed25519
ssh-keygen -t ed25519 -f my_company_ed25519 -C "email@example.com"
  1. 上传上一步创建密钥中的公钥 到远程电脑

    • 远程电脑个人目录下的.ssh目录,没有则创建 mkdir .ssh

    • 使用 sftp 或者其它工具,把本机创建的公钥.pub文件上传到远程目录.ssh

    • 没有则创建authorized_keys 文件

      # 修改`authorized_keys`权限为`600`
      chmod 600 authorized_keys
      # 或者 
      chmod u+rw authorized_keys
      
    • .pub公钥内容追加至authorized_keys

      cat my_company_ed25519.pub >> authorized_keys
      
      # 在 /etc/ssh/sshd_config 下,确保这两配置
      PubkeyAuthentication yes
      AuthorizedKeysFile      .ssh/authorized_keys
      
    • 修改.ssh/权限为700

      chmod 700 .ssh
      
    • 检测服务器的ssh设置

      # 在 /etc/ssh/sshd_config 下,确保这两配置
      PubkeyAuthentication yes
      AuthorizedKeysFile      .ssh/authorized_keys
      
  2. 配置本机私钥, 本机用户目录 %USERPROFILE%/.ssh/config, 没有 config 文件则创建

    # VS code - Remote Development
    Host Develop_231
    HostName 192.168.18.231
    Port 22
    User remote_username
    PreferredAuthentications publickey
    IdentityFile D:/Home/.ssh/ssh_231_ed25519
    

    🔔 NB: 一定要修改远程 authorized_keys.ssh 两者的权限, 切记 authorized_keys.png

    💖 Windows 推荐做法(不用手动上传.pub到远程服务器),在 PowerShell 中执行

    Get-Content D:\Home\.ssh\ssh_231_ed25519.pub | ssh username@192.168.18.231 "chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
    

    如果 .pub 公钥文件在 用户 默认目录 .ssh

    Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | ssh <user>@<hostname> "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" 
    

1.2 VS Code 配置操作

  1. 在 VS Code的扩展里搜索并安装Remote Development remote_dev_plugin.png
  2. 选择 linux , connection 连接, 查看左下角的连接状态,参照下图 ssh_connection.png
  3. 切换到 VS Code 的 左上Explorer资源管理器里, 选择打开文件夹或者通过Clone Repository remote_window.png

参考:

  1. 非root用户配置VS Code Remote-SSH 免密登录远程Linux服务器