vscode (windows) how to connect to linux (centos 7) in a virtual machine

Hits: 0

[When we use linux in a virtual machine] under windows, we will not be used to some operations of linux. vscode is a powerful code editor (maybe a better text editor). Let us return to the feelings of windows for linux. Let’s see how to use vscode to remotely connect to linux in a virtual machine

Article directory

1, linux configuration network

The linux version I use is centos 7, other versions are basically the same, there may be differences in the commands.

Everyone should know how to configure the network in Linux. If not, you can check the following blog

linux network configuration

[2. Configuration of ssh] service of virtual machine linux

Our [vscode] is connected to the linux of the virtual machine through ssh. First enter root under linux and obtain root privileges

  1. Enter root, we need to install openssh-server, enter the following command

[root@localhost ~]$ yum list installed | grep openssh-server

  1. Enter the /etc/ssh directory to edit the sshd_config file

[root@localhost ~]$ cd /etc/ssh
[root@localhost ssh]$ vim sshd_config

  1. Press the i key to edit the file, find the following paragraphs, remove the # comment in front, save and exit after modification.

Listening port and listening address
Enable remote login
Enable Use username and password for connection authentication
4. Start the sshd service

[root@localhost ~]$ sudo service sshd start

Check if the sshd service is enabled,

[root@localhost ~]$ ps -e | grep sshd

You can enter the following command to add sshd to self-start

[root@localhost ~]$ systemctl enable sshd.service

  1. Check the ip address of linux

[root@localhost ~]$ ifconfig

The ip address at this time is 192.168.86.10

  1. Check the ip address of VMnet8 of Windows

C:\Users\SweiJ> ipconfig

The ip at this time is 192.168.86.1

  1. Ping the address of linux in windows

C:\Users\SweiJ> ping 192.168.86.10

  1. If the ping fails, open the network configuration and select the properties of the TCP/IPv4 of the network adapter VMnet8

Set ip to the same network segment of your virtual machine’s ip, my linux address is 192.168.86.10 so ip is set to 192.168.86.1

3. Configure vscode

If there is no download, click vscode to install

  1. Open the vscode search extension [C:\Users\SweiJ> ssh-keygen]

[② In linux, we also need to have the same operation, and also generate a .ssh folder in the current directory, which stores the id_rsa and id_rsa.hub files, then we create an authorized_keys file in the .ssh folder and edit the file . You can also use the command]

[[root@localhost swei] # ssh-keygen 
[root@localhost swei] # cd .ssh 
[root@localhost swei] # touch authorized_keys 
[root@localhost swei] # vim authorized_keys](https://marketplace.visualstudio.com/items?item>Remote - SSH</a> installation</p><p></p><p>2. ssh-keygen installation</p><p></p><p>① Enter the ssh-keygen command in windows cmd and press Enter all the way, <code>C:\Users\SweiJ</code>a .ssh folder will be generated under the path, which will store the id_rsa and id_rsa.hub files</p><p></p><pre data-index=)

Remember the id_rsa.pub file we had in the .ssh folder generated by windows?
Use Notepad to open id_rsa.pub and copy the contents to the authorized_keys file of linux, then save and exit.

3. Configuration

[Open vscode and press F1, search for ssh, find and click Remote-SSH: Settints, enter settings, and Remote.SSH: Show Login Terminaltick.
After completion, press F1 to search for ssh, find and click Remote-SSH: Connect to Host..., enter the following operation, clickConfigure SSH Hosts...]

Click the newly generated .ssh folder
to enter the editing interface of the config file, modify and save](https://marketplace.visualstudio.com/items?item>Remote – SSH installation

2. ssh-keygen installation

① Enter the ssh-keygen command in windows cmd and press Enter all the way, C:\Users\SweiJa .ssh folder will be generated under the path, which will store the id_rsa and id_rsa.hub files

<pre data-index=)

hostAny name
Hostnameis the ip address of the linux host, the
Portport number is 22 by default at the beginning
IdentityFile, and the path of the id_rsa file

[Host Swei
    HostName 192.168.86.10
    User swei
    Port 22
    IdentityFile "C:\Users\SweiJ\.ssh\id_rsa"](https://marketplace.visualstudio.com/items?item>Remote - SSH</a> installation</p><p></p><p>2. ssh-keygen installation</p><p></p><p>① Enter the ssh-keygen command in windows cmd and press Enter all the way, <code>C:\Users\SweiJ</code>a .ssh folder will be generated under the path, which will store the id_rsa and id_rsa.hub files</p><p></p><pre data-index=)

4. Connection

Click to connect to linux
and finally you can operate happily! !

You may also like...

Leave a Reply

Your email address will not be published.