Vscode uses ssh for remote development (remote-ssh)

introduce

Visual studio code remote - ssh can connect to remote hosts and virtual machines through ssh, open remote folders, and use the plug-in advantages of vscode for remote development and debugging.
file

step

1. Configuration environment

Because the ssh connection of remote-ssh is implemented based on openssh, and we need to use git to generate ssh keys later, we need to prepare:

  • OpenSSH
  • Git version not lower than 1.9

If you have already installed the above environment, please move to the next step.

1. git installation

You can refer to this blog: Git detailed installation tutorial (detailed explanation of each step of the Git installation process)
During the git installation process, it will appear whether OpenSSH needs to be bundled, and you can choose according to your own usage habits.

2. OpenSSH installation

Please ignore the bundle selected above.

You can install it through this Microsoft document: Install OpenSSH
But I will also repeat the steps here:

#用管理员身份运行PowerShell
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
# 安装OpenSSH客户端 (这里我们只需要客户端)
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

3. Confirm the installation

When you have successfully installed, you can confirm whether it is installed in the console

git
file

ssh
file

2. Add remote ssh plugin in vscode

Select the extension on the left column, and search for remote ssh,
it looks like this, click install
file

After the installation is successful, a remote connection icon will appear in the left column, click and select the plus sign next to ssh to connect.
file

3. Connection

After selecting the connection, the following content will appear.
file
Enter to ssh 主机用户名@主机地址 -Aconnect to the remote host (the input -Awill be generated in the configuration file. ForwardAgent yesThe purpose is to use the key in the local computer to log in, and do not want to send this key to the bastion host) for configuration.

At this time, the option to connect or open the configuration file will be displayed in the lower right corner. You can connect directly or open the configuration file. (For the first time, it seems that you will be asked to choose one and generate a configuration file. Directly select user/username/.ssh just down)

If you open the configuration file, the following content will appear
file

Select password authentication

There is no direct connection, just click the refresh button next to the remote.
Select the host connection, select the platform, enter the password, and then you can remotely connect to the remote host
(if the platform selection is wrong, you can enter it in the vscode settings remote.SSH.remotePlatformto modify it)
file

After selecting a connection, a new empty window will be generated, you can always refer to the bottom left corner to view your connection status
file

After the connection is successful, you can open your project file in the resource manager.
file

However, if you use password authentication, you have to re-enter the password every time you connect, which is very troublesome. You can choose to use the ssh key authentication, so you don’t need to enter it repeatedly.

Select ssh key authentication

1. Generate a key on the local terminal

Two methods are provided here.
Method 1: Direct input ssh-keygen, follow the guidance of the program to select

ssh-keygen

Method 2: Specify parameters in advance, please make sure you have installed git, and the version is greater than 1.9

# -t 加密类型 -b 指定指定要创建的密钥的位数
ssh-keygen -t rsa -b 4096

More parameter settings: oracle manual----ssh-keygen

Here I demonstrate the second.
The password is empty by default, and you can choose to use a password to protect the private key file. If you don't want to enter the password used to protect the private key file every time you use the SSH protocol to access the warehouse, you can enter an empty password when creating the key, that is, press Enter directly .
file

Here the private key is stored in and C:\Users\20447/.ssh/id_rsa
the public key is stored inC:\Users\20447/.ssh/id_rsa.pub

2. Copy the public key to the remote host and install

Copy the public key to the ~/.ssh file on the remote host.
You can use FTP or SFTP to transfer to the host. Here I use FileZilla as a file transfer tool (if you need to use FileZilla to manage files, it is recommended to use root user to log in, because some files cannot be operated due to insufficient permissions)
file

Install the public key on the remote host side

cd .ssh
cat id_rsa.pub >> authorized_keys #安装公钥

Set file permissions to improve security

chmod 600 authorized_keys  #所有者可读写
chmod 700 ~/.ssh           #所有者可读可写可执行

3. Go back to vscode and add it to the previous configuration file. SelectIdentityFile $私钥路径$
the setting next to SSH to quickly move to configthe file
file

file

After saving and reconnecting, there is no need to use password authentication all the time.

Fourth, about the configuration file

As mentioned earlier, remote-ssh is based on the openssh plug-in, so the configuration file follows the openssh SSH configuration file format (sshd_config), and can be configured in the ssh format.

Here we only list the commonly used configuration formats. If you want to know or have more needs, please refer to sshd_config(5) — Linux manual page

Host $远程主机名$   #可以自定义,目的是知道自己用什么主机
  HostName $远程主机IP$
  User $用户名$
  Port $ssh端口$   #不写默认22
  IdentityFile $本机SSH私钥路径$
  ForwardAgent yes $希望使用本地电脑里的密钥登录,且不想把这个密钥发送到堡垒机,之前添加 -A生成$

If you need to connect to multiple hosts, in the configuration file, add multiple this according to the above configuration

Host $远程主机名1$
  HostName $远程主机IP1$
...

Host $远程主机名2$
 HostName $远程主机IP2$
...

Host n...

5. Install the plug-in to the remote host

After connecting to the remote host, you can actively choose the installation location on the installation page.
file

Install, uninstall or disable plug-ins in the remote host in vscode according to your own needs, which facilitates remote development.
file

6. About the possible problems that cause the connection to fail

Because there is no problem when I follow the above operations, regarding the problem, I suggest that when you make an error, use openssh as the keyword as the starting point when searching, and pay attention to the output window in the lower corner to determine the type of error.
Some error issues, you can 参考1solve the details in the following

Here is a solution to the error

  • If you reset the system or reset ssh for some reason, and you cannot connect,
    it may be because the original host key is inconsistent with the current host key, you can try to delete the file
    in c:\user\username.ssh , the The method is a bit rough, if you have new insights, please leave a message in the comment area.known_hosts

7. Other connection methods

In addition to the ssh connection method, there are also WSL, Dev Containers and other connection methods. VS code has extensions, and you can install your own development habits to choose
file

reference:

1. VSCode uses Remote SSH to connect to remote servers
2. Use SSH for remote development

Guess you like

Origin blog.csdn.net/weixin_51278158/article/details/130186395