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.
相关推荐
Avan_菜菜9 小时前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
SelectDB1 天前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
XIAOHEZIcode3 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220703 天前
如何搭建本地yum源(上)
运维
大树886 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠6 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质6 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
开发者联盟league6 天前
安装pnpm
ssh
Inhand陈工6 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智6 天前
ARP代理--工作原理
运维·网络·arp·arp代理