auto-connect remote ssh server

auto connect remote ssh server

环境条件

本地: windows10

远程: ubuntu

本地vscode通过remote ssh方式连接到远程Ubuntu。

需求

vscode通过remote ssh连接到远程Ubuntu时,自动登录,无需手动输入密码

solution

To automatically connect to your Ubuntu 22.04 remote machine from VS Code on Windows 10 without entering your password every time, you can set up SSH key-based authentication and configure an SSH config file.

Here is the step-by-step process using PowerShell.


Step 1: Generate an SSH Key Pair on Windows 10

If you do not already have an SSH key, you can generate one using PowerShell.

  1. Open PowerShell on your Windows 10 machine.

  2. Run the following command to generate a modern, secure ED25519 key pair:

    powershell 复制代码
    ssh-keygen -t ed25519
  3. Press Enter to accept the default file location (C:\Users\YourUsername\.ssh\id_ed25519).

  4. When prompted for a passphrase, press Enter twice to leave it empty. (Leaving it empty allows for fully automatic login, though it means anyone with access to your Windows user account can connect to the remote machine).


Step 2: Copy the Public Key to your Ubuntu Remote

You need to add your Windows public key to the remote Ubuntu machine's authorized list.

  1. In PowerShell, read the content of your public key:

    powershell 复制代码
    Get-Content "$env:USERPROFILE\.ssh\id_ed25519.pub"
  2. Copy the outputted text (it should start with ssh-ed25519 and end with your Windows username/host).

  3. Connect to your Ubuntu machine one last time using your password (either via VS Code terminal or a standard terminal).

  4. On the Ubuntu machine, open or create the authorized_keys file:

    bash 复制代码
    mkdir -p ~/.ssh
    nano ~/.ssh/authorized_keys
  5. Paste your copied public key onto a new line in this file.

  6. Save and exit (in nano, press Ctrl+O, Enter, then Ctrl+X).

  7. Set the correct permissions on the Ubuntu machine to ensure SSH accepts the key:

    bash 复制代码
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys

Step 3: Configure the SSH Config File on Windows

To save your connection details (like IP, username, and key path) so VS Code remembers them:

  1. In PowerShell on Windows, open or create your local SSH config file:

    powershell 复制代码
    notepad "$env:USERPROFILE\.ssh\config"
  2. Add the following configuration, replacing the placeholders with your actual remote details:

    text 复制代码
    Host my-ubuntu-server
        HostName 192.168.1.100
        User your_ubuntu_username
        IdentityFile ~/.ssh/id_ed25519

    (Replace my-ubuntu-server with any nickname you prefer, 192.168.1.100 with your Ubuntu IP address, and your_ubuntu_username with your actual username on Ubuntu).

  3. Save and close the file.


Step 4: Connect via VS Code

  1. Open VS Code on your Windows machine.
  2. Ensure you have the Remote - SSH extension installed.
  3. Click the Remote Window icon in the bottom-left corner of VS Code (the green or blue >< icon), or press Ctrl + Shift + P and type Remote-SSH: Connect to Host....
  4. Select my-ubuntu-server (the nickname you set in Step 3) from the list.
  5. VS Code should open a new window and connect to your Ubuntu machine automatically without prompting for a password.
相关推荐
咯哦哦哦哦8 分钟前
linux so库(arm)对比分析
linux·运维·arm开发
星野川崎2065 小时前
电商多店运维:云机24小时挂机频繁掉线、账号无故风控深度原因分析及解决方案
大数据·运维·服务器·云计算·电商
不灭的程序员阿澄7 小时前
在飞牛 NAS 上用 Docker 运行 DeepSeek Harness
运维·docker·容器
Elastic 中国社区官方博客8 小时前
让大模型思考,让小模型执行:在 Elastic Workflows 中拆分 LLM 成本
大数据·运维·数据库·人工智能·elasticsearch·ai
艾伦_耶格宇8 小时前
【AI】-4 OpenCode Go 接入 Obsidian 完整指南
运维·开发语言·人工智能·agent·opencode
SXkehuirongsheng9 小时前
APP开发定制和模板开发哪个更实用?
大数据·运维·人工智能
fb_1234510 小时前
Linux三剑客超全精讲(grep+sed+awk)零基础入门|正则+实战面试题
linux·运维·服务器
HiDev_10 小时前
【非标自动化】2、认识元器件(节流阀)
运维·自动化
火车叼位11 小时前
Bash 实现 IDE 式补全的组件与配置
linux·运维
晓晓_za89866811 小时前
Geo 优化服务 CI/CD 流水线搭建:源码自动构建、测试与灰度发布
运维·服务器·tcp/ip·spring·缓存·ci/cd