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.
相关推荐
Championship.23.246 小时前
Linux 3.0 锁机制与故障排查详解
linux·运维·服务器
风123456789~8 小时前
【Linux专栏】ls 排除某个文件
linux·运维·服务器
其实防守也摸鱼9 小时前
运维--学习阶段问题解答(1)(自测)
linux·运维·服务器·数据库·学习·自动化·命令模式
Darkwanderor10 小时前
对Linux的进程控制的研究
linux·运维·c++
爱网络爱Linux13 小时前
Linux proc 目录安全加固
linux·运维·rhce·rhca·红帽认证·proc 目录安全加固
MindUp13 小时前
企业网盘权限模型解析:多层级访问控制与审计能力选型指南
java·linux·运维
持力行14 小时前
D-BUS会话总线
运维·网络
kdxiaojie14 小时前
Linux 驱动研究 —— I2C (5)
linux·运维·笔记·学习
独自归家的兔14 小时前
2026年7月2日 Docker 容器化部署 PaddleOCR-VL 实战(OpenAI 协议兼容 HTTP 调用)
运维·docker·容器
ZXXstarstar15 小时前
2026年电池安全使用年限的量化决策:什么时间到期最需要替换?
linux·运维·服务器·电池