Remote development in VS Code via remote-SSH

In ancient times, not only the world’s talent, but also the indomitable ambition – Su Shi

Writing in the front

Developing in Linux is already an essential skill in today’s development environment, but the high cost of learning Linux discourages many beginners.

Since the release of VS Code 1.35.0, the Remote plug-in has been officially launched. We can use the remote-SSH plug-in to write the server-side Code locally in VS Code, thus reducing a large number of server-side operations.

Begin the text.

@[toc]

The premise to prepare

1. The SSH command

This plug-in is based on the SSH command, which is necessary to use this plug-in. However, with the popularity of Windows 10, the default PowerShell is usually equipped with SSH command, and you can enter SSH to know whether to install it or not.

If it is not installed, please install it on baidu.

2. Install the remote-SSH extension

The extension can be installed directly from the VS Code extension store,

Here I have installed, not installed children’s shoes by themselves

After the installation, the Remote Resource Manager will appear in the active bar. If it does not appear, right-click the active bar and select the Remote Resource Manager

(Optional) Configure a private key.

If the private key is configured, you do not need to use the password for each connection. If the private key is not configured, you need to re-enter the server password for each connection.

First PowerShell and then type the following command

cd ~/.ssh
Copy the code

And then type the

ssh-keygen
Copy the code

You can simply press Enter to generate the default private key.

The default private key name is ID_RSA

You can also run the following command

ssh-keygen -t rsa -f remote-ssh -C "vs code remote-ssh key"
Copy the code

Add parameters to generate a key. The parameters are described as follows:

-t type: specifies the key type to be generated. The key types include RSA1 (SSH1), DSA (SSH2), ECdSA (SSH2), and RSA (SSH2). The most common key type is RSA. For RSA keys, the minimum length is 768bits, and the default length is 2048bits. The DSA key must be 1024bits. -f filename: specifies the name of the generated key fileCopy the code

Once generated, you can view the result by typing the ls command

[10:38:07] ~\. SSH ❯ ls Directory: C:\Users\Administrator\.ssh Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2021/4/1 10:33 1675 id_rsa -a--- 2021/4/1 10:33 396 id_rsa.pub -a--- 2021/3/29 10:36 883 known_hosts -a--- 2021/4/1 10:19 1679 remote-ssh -a--- 2021/4/1 10:19 405 remote-ssh.pubCopy the code

I’m going to generate two private keys here.

Finally, upload the generated id_rsa.pub file to the. SSH /authorized_keys folder in the remote root directory

Run the following command:

SCP id_rsa. Pub [email protected]: ~ /. SSH/authorized_keysCopy the code

Command interpretation

SCP private key pub File server username @server IP address: server directoryCopy the code

In this case, we do not need a password to connect to the server through SSH command.

Use of remote-SSH

Click the small button of Settings, and then select the second step to select the first file or the second file for configuration. If you cannot automatically create files, you can manually create files according to this directory.

The configuration is as follows:

# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host 120.77.178.156
    HostName 120.77.178.156
    User root
    IdentityFile C:\Users\Administrator\.ssh\id_rsa
Copy the code

The optional parameters are as follows:

  • Host: specifies the name of the connected Host

  • Hostname: indicates the IP address of the remote host

  • User: User name for logging in to the remote host

  • Port: indicates the Port for logging in to the remote host

  • IdentityFile: indicates the path of the local private key

The multiple configurations are as follows:

# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host 120.77.178.156
    HostName 120.77.178.156
    User root
    IdentityFile C:\Users\Administrator\.ssh\id_rsa

#Second configuration
Host 120.77.178.155
    HostName 120.77.178.155
    User root
    IdentityFile C:\Users\Administrator\.ssh\id_rsa
Copy the code

Click when the configuration is complete

A new VS Code window opens. The first time you open it, the following window may pop up

Select this parameter based on the operating system.

After successful connection, you can open the folder directly as local operation,

Open the directory you want to edit.

Error logging

During the configuration process, when THE SSH configuration file was configured, the following problem occurred

There are two solutions here

  1. To modify the basic permissions of the folder, perform the following steps: right mouse button -> Properties -> Security -> Advanced -> Full Control.

  2. Download the OpenSsh-Portable project on GitHub and type the following command when complete

     cd .\openssh-portable\contrib\win32\openssh\
    Copy the code

    Then perform

    .\FixHostFilePermissions.ps1  -Confirm:$false
    Copy the code

    If a message is displayed indicating that the FixUserFil ePermissions. Ps1 file cannot be loaded because script errors are forbidden on the system, run the following command and press Y to confirm and then run the command again (after the command is executed, run the following command again and enter N to restore the default configuration) :

    Set-ExecutionPolicy RemoteSigned
    Copy the code

Write in the last

This is the whole process of writing server Code in VS Code, which is much more comfortable than writing Code directly on a Linux server.