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.
相关推荐
Lumbrologist1 小时前
【零基础部署】Docker 部署 AutoGen 多 Agent 对话框架保姆级教程
运维·docker·容器
feng14562 小时前
OpenSREClaw - AI 本体论思维
运维·人工智能
LIZHUOLONG13 小时前
linux 设备初始化
linux·运维·服务器
遇印记3 小时前
软考知识点(局域网基础)
运维·服务器·局域网
lulu12165440783 小时前
Codex Computer Use 深度分析:AI桌面自动化的技术突破与行业影响
java·运维·人工智能·自动化·ai编程
雪霁清寒3 小时前
麒麟V10用MobaXterm远程连接SSH偶尔卡顿的问题
linux·ssh
ylscode3 小时前
Linux CIFSwitch 内核新漏洞允许攻击者获得 root 权限
linux·运维·服务器
Plastic garden4 小时前
Docker compose ruoyi示例
运维·docker·容器
qq_452396234 小时前
第十四篇:《Docker Swarm 生产实践:堆栈部署与配置管理》
运维·docker·容器