VSCode SSH to Servers
Using VSCode to connect to remote servers allows you to directly edit files on the server, avoiding the use of traditional command-line-based editing tools like Vim or Nano. Additionally, since VSCode runs code directly on the server, it effectively mitigates compatibility issues caused by differences between local and server environments.
Installing the Plugin
Open VSCode and go to the extensions market. In the search box, search for "ssh", and find the "Remote-SSH" plugin. Click on "Install."
Configuring the Plugin
After installation, you will see an icon for the Remote Explorer. Click on this icon to open the panel.
There are two ways to connect to servers: you can use an interactive menu or directly modify the configuration file.
Modifying the Configuration File
Click on the gear icon located to the right of "SSH" and select the configuration file you want to edit. Press Enter to confirm.
Enter the following configuration template:
Host router
HostName 192.168.1.1
User root
After "Host," specify the server's name, which can be any easily recognizable name. For "HostName," enter the server's IP address, and for "User," enter the username you want to connect with. If you don't specify a port, it will default to port 22.
If you need to change the SSH connection port, add a line specifying the port number like this:
Host router
HostName 192.168.1.1
User root
Port 234
After saving, the server name will appear on the left (you may need to refresh). Move your mouse over the server name, and click the ->
arrow to connect to the server.
Using the Interactive Menu
Click on the "+" icon to the right of "SSH", and VSCode will prompt you to enter an SSH connection command.
Use the following SSH connection command template:
ssh <username>@<host>
Replace <username>
with the username you want to connect with (e.g., root), and after "@" enter the server's IP address (e.g., 192.168.1.1). If you need to specify a port, add the "-p" parameter, like this:
ssh -p <port> <username>@<host>
Choose the default configuration file to update.
In the bottom right corner, a message saying "Host added" will appear. Click on it to connect.
Connecting to the Server
The first time you connect to the server, you may be asked to choose the server's operating system. Then, enter the server's password.
Once successfully connected to the server, you can find Terminal -> New Terminal
in the top menu of VSCode to open a command line.
You can also open the folder where your project is located for easier development. This may require you to re-enter the server's password.
Passwordless Connection
Passwordless connections can be achieved in two ways: when the user truly has no password, enabling a direct connection, or by using key pairs instead of passwords. The first method is only suitable for local testing and is extremely insecure for public networks, so it is not demonstrated here. Instead, the second method, using key pairs, is shown.
If you don't have key pairs on your computer, use the following command to generate a pair (use the default options):
ssh-keygen
By default, two files will be generated:
- id-rsa (your private key)
- id-rsa.pub (your public key)
Do not upload the private key to any public location, as it poses a serious security risk.
If you are using Linux or macOS, you can automatically copy the public key to the authorized_keys file on the server using the following command:
ssh-copy-id <username>@<host>
Alternatively, you can manually copy the contents of the id-rsa.pub file. On Linux/macOS, this file is typically located at ~/.ssh/id-rsa.pub
:
cat ~/.ssh/id-rsa.pub
If you are using Windows, you must manually copy the contents of the id-rsa.pub file to the authorized_keys file on the server. On Windows, the file is typically located at C:\Users\your_username\.ssh\id-rsa.pub
.
With these steps completed, you should no longer need to enter a password when connecting to the server using VSCode.
If you are still prompted for a password at this point, there may be an issue with the above steps. You should verify that your public key is stored in the authorized_keys file on the server. Additionally, you can also try to specify the path to your private key in the configuration file. Refer to the official documentation for details:
Host router
User root
HostName 192.168.1.1
IdentityFile ~/.ssh/id_rsa
Connecting via Socks5 Proxy
Sometimes, you may need to connect through a Socks5 proxy to speed up access or when dealing with servers on an internal network. Here is a configuration example:
- Replace
127.0.0.1:1080
with the address of your Socks 5 proxy server.
Linux / macOS:
Host router
HostName 192.168.1.1
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
User root
Port 234
Windows:
Host router
HostName 192.168.1.1
User root
Port 234
ProxyCommand C:\Program Files\Git\mingw64\bin\connect.exe -S 127.0.0.1:1080 %h %p
Comments are disabled.