Author: Jeannnette

Introduction: First of all, in daily use of Linux, I must encounter a lot of scenarios that need to remotely edit files on Linux. Although IT is very convenient to use SVN server/Git server, I still want to directly use VScode to edit files for more convenient use sometimes

Let’s Go, Get it!

The finished image is as follows: (can be edited online) Remote Linux system file, perfect for the need to change the remote server in time

Basic configuration

Complete the following steps

  1. To install OpenSSH, go to the official website. I provide the following path:
The first (website) : http://www.openssh.com/portable.html#httpThe second (other) : https://openssh.en.softonic.com/Copy the code
  1. When the installation is complete – if it is Win10 need to add the current installation directory to the system environment variable
2. For example: F: devlop-openssh bin 3. Add the \bin directory to environment Variables 4. On my computer right click Properties - Advanced System Settings - Advanced - Environment VariablesCopy the code

  1. This is probably what you need to add to install the plug-ins needed by vscode

  1. After the installation is complete, you will find a small icon in vscode

  1. Click the Settings icon to go to the configuration file. The default configuration file is installed where the Windows system generates the key%USERPROFILE%\.ssh\

HostName: indicates the IP address of the Linux server

User: the User name

IdentityFile: ~/.ssh/id_rsa-remote-ssh #

This is what the second configuration method needs to add, if the first method does not need to add the specified file

  1. After the addition, the public and private keys are generated in PowerShell administrator mode

The first implementation:

  1. Configuring SSH Keys

    Check whether an SSH key (asymmetric encryption) exists on the local computer. The window public key is usually stored in the %USERPROFILE%\.ssh\ path. If the id_rsa.pub file is not found, the SSH key has not been generated locally. Run the following command:

    ssh-keygen -t rsa -b 4096
    # just press enter to generate to %USERPROFILE%\.ssh\
    Copy the code
  2. On Windows, run the following command at the local command prompt to replace REMOTEHOST’s two values [your-user-name-on-host] and [host-fqdn-or-ip-goes-here]

    SET REMOTEHOST=[your-user-name-on-host]@[host-fqdn-or-ip-goes-here]
    
    scp %USERPROFILE%\.ssh\id_rsa.pub %REMOTEHOST%:~/tmp.pub
    ssh %REMOTEHOST% "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat ~/tmp.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && rm -f ~/tmp.pub"
    
    # PS: 
    # your-user-name-on-host: specifies your host username
    # host-fqdn-or-ip-goes-here: specifies your host IP
    
    Copy the code

    If the preceding command fails, manually copy the public key contents of the directory %USERPROFILE%\.ssh\id_rsa.pub to /[username]/.ssh/authorized_keys

The second way to achieve: than a safe and safe way

While it can be convenient to use a single SSH key on all SSH hosts, if someone has access to your private key, they can also access all hosts. You can prevent this by creating a separate SSH key for the development host. Just follow these steps

  1. Generate a separate SSH key in a separate file.

    ssh-keygen -t rsa -b 4096 -f %USERPROFILE%\.ssh\id_rsa-remote-ssh
    Copy the code
  2. In VS Code, run remote-ssh: Open configuration file… From the command palette (F1), select the SSH configuration file and add (or modify) the host entry as shown below.

    Host name-of-ssh-host-here
        User your-user-name-on-host
        HostName host-fqdn-or-ip-goes-here
        IdentityFile ~/.ssh/id_rsa-remote-ssh   
    Copy the code
    1. On Windows, run the following command at the local command prompt to replace name-of-ssh-host-here with the host name in the SSH configuration file in Step 2.
    SET REMOTEHOST=name-of-ssh-host-here
    SET PATHOFIDENTITYFILE=%USERPROFILE%\.ssh\id_rsa-remote-ssh.pub
    
    scp %PATHOFIDENTITYFILE% %REMOTEHOST%:~/tmp.pub
    ssh %REMOTEHOST% "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat ~/tmp.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && rm -f ~/tmp.pub"
    Copy the code

Check whether the connection is successful

You can then add a small directory to a specified Linux file and use SSH to edit code or documents online

Other errors reported

error: An SSH installation couldn't be found

This error is usually caused by vscode not specifying the appropriate Path. You need to go to Settings –> remote.ssh. Path to specify the OpenSSH Path