[Complete process] vscode connects to a remote Linux server and logs in without password

Written at the beginning
Doing deep learning scientific research will inevitably require a large computing power. At this time, you need to run your own program on the GPU server. Before, I uploaded the code to the server through the scp command, connected to the server through ssh in the terminal, and operated the dataset and model through a series of Linux commands. In fact, the commands cd ls are still very convenient, but when I need to view or modify the source code, editing with vim can't always give me a comfortable experience. Even, vim on the server sometimes has typesetting or writing bugs (Note: I am not saying that vim is inconvenient, vim is actually a powerful tool for Linux systems, but recently I encountered some bugs in vim on the server ~ understandable For my desire to survive hahaha).
Therefore, for a better experience, here is a record of the basic operations of vscode connecting to a remote Linux server and logging in without secrets.

Step 1: Generate the public key and private key locally
Enter in cmd:

ssh-keygen -t rsa -C “your_email@xxx.com”

Therefore, the public and private key files can be found in the following directories:
insert image description here
among them, id_rsa.pub is the public key, and id_rsa is the private key. If the public and private key files already exist before the command is executed, they will be regenerated and overwritten.

Step 2: Install Remote-SSH in vscode
to enter the vscode plug-in, search and install it:
insert image description here

Step 3: Add the private key to authorized_keys on the server side
Upload the rsa_id.pub public key generated locally to the server, and then append (cat command) to the authorized_keys file in the ~/.ssh directory:

cat id_rsa.pub >> authorized_keys

insert image description here
Why append? Because there may be public keys of other users, do not overwrite and write!
If there is no authorized_keys file in the ~/.ssh directory, we need to create one manually:

touch authorized_keys

Then use the cat command to write the public key to the file.

Step 4: Configure the vscode file on this machine
First, enter vscode and click "Remote Explorer" on the left, and click "+":
insert image description here
Then, enter the ssh command you want to connect (username, ip address, port number) at the top of the interface, for example As follows:
insert image description here
When selecting the ssh configuration file, please select the .ssh/config file, as shown in the figure:
insert image description here
In this file, all the remote connection information we have configured is stored:
insert image description here
When the pop-up window selects the operating system, select "Linux" Can.

Step 5: Select the server folder to get started

insert image description here

Therefore, you can perform visual file operations similar to Windows systems.
insert image description here
Practical function! ! ! Drag and drop to upload files to the server
Drag the files of this machine directly into the directory bar of vscode to upload the files, without the need for cumbersome scp commands to upload.
insert image description here

insert image description here
insert image description here
However, if you want to download files from the server to this machine, drag and drop download is not supported yet. However, right-clicking on the file and then clicking Download can also achieve a quick download function.

Other practical operations: delete, move, and rename files.
Manage files like a local computer and it will be OK.
One thing to note is that try to avoid file operations on large files on vscode, because it may cause downtime. For large files, it is safer to perform operations such as rm cp on the command line.

Permissions issue!
If both the server-side and the local vscode are configured, but the password is still required, the issue of server-side permissions must be considered, and the following commands are usually used to solve it:

cd ~
chmod 700 .ssh
cd .ssh
chmod 644 authorized_keys 
chmod 644 id_rsa.pub
chmod 600 id_rsa

Remember the following permissions principles:

1)authorized_keys的权限必须是600或者644
2).ssh目录的权限必须是700
3)/home/user目录必须是755

Guess you like

Origin blog.csdn.net/qq_16763983/article/details/126254636