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.
-
Open PowerShell on your Windows 10 machine.
-
Run the following command to generate a modern, secure ED25519 key pair:
powershellssh-keygen -t ed25519 -
Press Enter to accept the default file location (
C:\Users\YourUsername\.ssh\id_ed25519). -
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.
-
In PowerShell, read the content of your public key:
powershellGet-Content "$env:USERPROFILE\.ssh\id_ed25519.pub" -
Copy the outputted text (it should start with
ssh-ed25519and end with your Windows username/host). -
Connect to your Ubuntu machine one last time using your password (either via VS Code terminal or a standard terminal).
-
On the Ubuntu machine, open or create the
authorized_keysfile:bashmkdir -p ~/.ssh nano ~/.ssh/authorized_keys -
Paste your copied public key onto a new line in this file.
-
Save and exit (in
nano, pressCtrl+O,Enter, thenCtrl+X). -
Set the correct permissions on the Ubuntu machine to ensure SSH accepts the key:
bashchmod 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:
-
In PowerShell on Windows, open or create your local SSH config file:
powershellnotepad "$env:USERPROFILE\.ssh\config" -
Add the following configuration, replacing the placeholders with your actual remote details:
textHost my-ubuntu-server HostName 192.168.1.100 User your_ubuntu_username IdentityFile ~/.ssh/id_ed25519(Replace
my-ubuntu-serverwith any nickname you prefer,192.168.1.100with your Ubuntu IP address, andyour_ubuntu_usernamewith your actual username on Ubuntu). -
Save and close the file.
Step 4: Connect via VS Code
- Open VS Code on your Windows machine.
- Ensure you have the Remote - SSH extension installed.
- Click the Remote Window icon in the bottom-left corner of VS Code (the green or blue
><icon), or pressCtrl + Shift + Pand typeRemote-SSH: Connect to Host.... - Select
my-ubuntu-server(the nickname you set in Step 3) from the list. - VS Code should open a new window and connect to your Ubuntu machine automatically without prompting for a password.