Use SSH for remote development in VScode

foreword

This article is a sharing about remote development in the Jetson_Nano unmanned vehicle development series.

In Linux development, in most cases, due to the limited space of the device, the motherboard cannot carry the display, so we need to use a remote method for development. Remote software can be divided into two categories according to whether it has a desktop or not. There are many remote development tools with desktop, such as: TeamViewer, Nomachine, MobaXterm, etc. However, the large amount of data transmitted with a desktop will cause delays, which is very uncomfortable to use. There are also many without a desktop, such as: putty, Xshell, etc. Today I mainly share the use of vscode and ssh for development, so that there are both command lines and editors, and the efficiency of development will be significantly improved. Of course, if you can use vim, emacs and other command line editors proficiently, the development efficiency is the highest, but the configuration of vim plug-ins is simply a headache, especially the auto-completion plug-in YouCompletMe, which is difficult to install. Therefore, it is a good choice to combine vscode and ssh for remote development.


1. Install ssh

Win10 generally comes with its own ssh tool, no special download is required. Just install on nano.

1. Install ssh on nano

sudo apt update
sudo apt install openssh-server

2. Open the ssh service

sudo service sshd start

3. Confirm whether to open

Enter the following command in the terminal, if it is started, it will print: [+] ssh

service --status-all | grep ssh

Two, ssh common commands

scp transfer command, if the transfer is a folder, just add -r to the command

scp [PC文件路径] [nano账号]@[nanoip]:[nano路径]    #PC发送给nano

scp [nano账号]@[nanoip]:[nano路径] [PC文件路径]    #nano发送给PC

3. Password-free login

When using ssh to log in, we need to enter the password in the command line. It is a bit troublesome to enter it every time. In fact, it is mainly lazy. We can add a key, so that we can log in between two commonly used machines without entering the password.

Prerequisite: Both host A and host B have installed and opened the ssh service, and are in the same local area network.

Host A and host B can both be Linux systems, or one can be Windows and the other Linux. Here, host A is my laptop win10, and host B is Nano. Perform the following operations in sequence to save the password when logging in to host B from host A. In order to facilitate future login, it is best to configure a static IP for host B.

1. Generate an rsa key on host A:

Open cmd and enter the following command:

ssh-keygen -t rsa

2. Copy the public key generated by host A to host B

Execute the following in the cmd terminal first:

function ssh-copy-id([string]$userAtMachine, $args)
{ 
    $publicKey = "$ENV:USERPROFILE" + "/.ssh/id_rsa.pub" 
    if (!(Test-Path "$publicKey"))
        { Write-Error "ERROR: failed to open ID file '$publicKey': No such file" }
    else 
        { & cat "$publicKey" | ssh $args $userAtMachine "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1" } 
}

Execute again:

ssh-copy-id [nano账号]@[nanoip]

After configuration, the login effect is as follows:

 Figure 3.1: Effect diagram of password-free login

4. Use ssh in vscode

Note: remote-ssh must not install the latest version! ! !

Install Vscode on Windows, and download the plug-in remote-ssh in the plug-in management place. 

                                        Figure 4.1: Download steps

After the installation is complete, icons will appear on the left sidebar and in the lower left corner:

 Figure 4.2: remote-ssh usage entry

Just click on one of the above two entries, and then follow the prompts of vscode to add the machine to log in step by step.

Note again: Be sure not to install the latest version of the remote-ssh plugin! ! !

After adding the machine, check the config configuration file in the .ssh folder under the user directory. The correct format is as follows:

Host 别名
    HostName 配置好的Nano静态IP
    User Nano用户名

Figure 4.3: Configuration file format

After the configuration is complete, the login effect is as follows. This is the icon of successfully logging into Nano and opening the .bashrc file in the Nano home directory on vscode. The green part in the lower left corner shows the connection status.

Figure 4.4: Effect display

remote-ssh also supports the use of tmux, you can see it by clicking the plus sign on the right side of the terminal.

Figure 4.5: Effect display


 

 

Summarize:

        This article introduces how to install ssh and some common commands and how to use ssh in vscode.

        Due to the limited level of the author, mistakes are inevitable, and you are welcome to communicate, criticize and correct me at any time. 

        This article is an original blog, if you need to reprint it, please contact me and indicate the source.

Guess you like

Origin blog.csdn.net/weixin_63268005/article/details/128231438