Vscode configures ssh to remotely connect to the virtual machine for development

virtual machine configuration

Network Configuration

Take virtualbox as an example, add a new network card to the network, and then select the connection mode as the host network only,
insert image description here
then enter the virtual machine to view the network

$ ifconfig

insert image description here
Compare the network here with the network of the host VirtualBox Host-Only Network graphics card. If the first three digits are the same, it can be used. If the first three digits are different, manually change the network (in the network settings of ubuntu, change the network to manual, and then modify it). Here, our ip address meets the requirements and there is no need to modify it. Next, ping this address in the host computer to see the connectivity. It can be pinged, and the network settings are complete
insert image description here
here
insert image description here
.

Virtual machine configuration ssh

First, install ssh

$ sudo apt-get install openssh-server # 安装ssh
$ sudo /etc/init.d/ssh start # 启动ssh
$ sudo systemctl enable ssh # 自动启动ssh

Then configure ssh

$ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup # 备份
$ sudo vim /etc/ssh/sshd_config # 编辑

Add the following configuration in the blank space

# SSH服务器配置文件的一部分
# 下面是每个选项的解释:
# 默认值为22。
Port 22
# SSH可以用于IPv4和IPv6地址族。
AddressFamily any
# SSH服务将侦听所有可用IP地址的连接请求。
ListenAddress 0.0.0.0
# SSH服务将侦听所有可用IPV6地址的连接请求。
ListenAddress ::
# 这将禁用Privilege Separation功能,该功能允许SSH以减少潜在漏洞的特权级别运行。
UsePrivilegeSeparation no
# 允许用户使用密码进行SSH远程登录。
PasswordAuthentication yes
# 允许root用户通过SSH远程登录。
PermitRootLogin yes
# 允许用户名为'xxx'的用户通过SSH远程登录。
AllowUsers xxx # 要把用户名修改为自己的用户名
# 启用SSH使用RSA算法进行身份验证。
RSAAuthentication yes
# 启用SSH使用公钥身份验证。
PubKeyAuthentication yes

Restart the service after modification

$ sudo service ssh --full-restart

Test the ssh connectivity in windows interrupt
$ ssh -p <端口号> <用户名>@<ip地址>
as shown below, where the user name and ip address should be written as configured above

$ ssh -p 22 [email protected]

The interface shown in the figure appears, indicating that the ssh configuration is successful.
insert image description here
So far, the virtual machine ssh configuration is complete

Configure Vscode

First, you need to install the Remote-ssh plug-in in Vscode. Just
install the plug-in here . This plug-in will install other required plug-ins . Then press + + to select the following optionsRemote-Development
insert image description here
ctrlshiftp
insert image description here

Then add ssh host
insert image description here
and fill it in according to the following format. Here, the host name can be filled in casually, mainly to distinguish different remote connections.
insert image description here
After filling, enter the interface just now and try to connect.
insert image description here

Configure password-free connection

The above connection also needs to manually enter the password. We can use the following method to connect without secrets in the form of a secret key.
First, ssh-keygengenerate a public key and a private key.

$ ssh-keygen -t rsa -C "[email protected]"

Then in c:\users\<用户名>\.sshthe directory, find the id_rsa.pub file, open it with Notepad, copy the content inside, and then paste it into ~/.ssh/authorized_keys, so that you don’t need to enter a password every time you connect

common problem


If there is no problem with the ssh connection of the terminal in windows, and then the connection cannot be made in vscode, it is the problem of the vscode plug-in. At this time, you need to check the configuration process of vscode, plug-ins, etc., you can try to delete and reinstall the plug-in or restore the default configuration of the plug-in
insert image description here
.
insert image description here

Guess you like

Origin blog.csdn.net/x646602196/article/details/129685159