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.
相关推荐
爱说实话5 分钟前
腾讯云Ubuntu远程无法ssh登录
云计算·ssh·腾讯云
阿里云云原生42 分钟前
可用性从 99.9% 跃升至 99.995%:畅捷通如何用 AI 重塑运维底座?
运维·网络·人工智能
阿童木写作1 小时前
Python实现Temu图片批量翻译自动化教程
运维·人工智能·python·自动化
iori97king2 小时前
库存不足自动通知:第一次使用自动化
运维·自动化
@insist1232 小时前
信息系统管理工程师-信息系统运维资源、技术与智能运维核心考点解析
运维·软考·信管·软件水平考试·信息系统管理工程师
木子欢儿3 小时前
Intel 架构的 MacBook Pro 运行 Linux 发热量高解决
linux·运维·服务器
Liekkas Kono4 小时前
Codex(ChatGPT 桌面版)支持SSH连接远程服务器
chatgpt·ssh·swhl
KKKlucifer5 小时前
运营商全域安全运维支撑体系落地实践
运维·安全
EAIReport5 小时前
工业电气自动化数据治理破局:多Agent大模型赋能全景智能决策
大数据·运维·自动化
会编程的土豆6 小时前
Go 模板初识
linux·运维·服务器