[SSH] Use SSH to remotely connect to the server in VScode remote development

Reposted from cpolar's article: [Vscode Remote Development] Use SSH to remotely connect to the server "Intranet Penetration"

foreword

There are many remote connection server tools, such as XShell, putty, etc. You can remotely connect to the server through ssh, but this is not convenient for writing code. It may be necessary to write the code locally and then transfer the source code to the server. The pictures of can't be viewed directly...

And vscode can solve these problems very well. Its core components are all running in a remote environment. The local development machine does not need to have the source code of the remote development environment at all. Writing code on vscode can be directly updated to the server synchronously. You bring a silky-smooth remote development experience.

So in this article tutorial, we will realize remote development through vscode, and do intranet penetration to realize remote connection in the public network environment. You can also remotely connect to the server anywhere outside to develop and write code.

video tutorial

[VS Code remote development] The public network uses SSH to remotely connect to the server to develop and write code

1. Install OpenSSH

Open the Windows start page, search directly PowerShell, open the first one Windows PowerShell, and click Run as administrator

insert image description here

After opening, enter the following command:

Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'

The result at this time is that OpenSSH is not installed in the computer:
insert image description here

If OpenSSH is already installed, skip this step, if not, enter the following command

Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

insert image description here

The final result is as follows, which means that the OpenSSH installation is successful:

insert image description here

After the successful installation of OpenSSH is displayed, enter the command ssh after opening the local cmd and entering

ssh

If the result is displayed like this, then it is correct:

insert image description here

2. vscode configures ssh

Install vscode on windows, and open it after successful installation.

Click the extension in the left toolbar, search for "ssh", and select the first one Remote - SSHto install (I have already installed it, and all the displays are disabled at this time)

insert image description here

Click the "Settings" button in the lower left corner, followed by "Settings"

insert image description here

According to the prompt in the screenshot, find Show Login Terminaland check this option

insert image description here

Click the button in the left toolbar 远程to view the remote connection, and click SSH TARGETSthe button next to the server shown below to connect

insert image description here

3. LAN test to connect to remote server

Take ssh connection to ubuntu as an example, after opening vscode, first try to use the LAN address ssh remote ubuntu

Enter the command into the dialog box and press Enter

ssh  username@ip

insert image description here

choose a profile

insert image description here

After the input is completed, the newly added ssh connection will be displayed on the right, click the --> icon to connect, and enter the password, just enter the password

insert image description here

A green sign appears to indicate a successful connection

insert image description here

4. Remote connection via public network

At present, we can only perform remote control within the local area network, which has certain limitations. However, we use intranet penetration to achieve remote connections in the public network environment. Here we use the tool cpolar intranet penetration to achieve it, no need for public network IP, no need to set up routers, and the operation is simple.

cpolar official website: https://www.cpolar.com/

4.1 ubuntu installation cpolar intranet penetration

In this tutorial, we use the ubuntu graphical system. We need to install and configure cpolar intranet penetration on ubuntu, which supports one-click automatic installation scripts.

  • Domestic installation
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
  • Or cpolar short link installation method: (for foreign use)
curl -sL https://git.io/cpolar | sudo bash
  • Check the version number, if it is displayed normally, the installation is successful
cpolar version
  • token authentication

Log in to the background of the cpolar official website, click the verification on the left to view your authentication token, and then paste the token in the command line

cpolar authtoken xxxxxxx

insert image description here

  • Simple Penetration Test
cpolar http 8080

If the corresponding public network address is generated normally, press ctrl+c to exit

  • Add a service to the system
sudo systemctl enable cpolar
  • Start the cpolar service
sudo systemctl start cpolar
  • Check the status of the service, if it is displayed as shown in the figure below, activeit is in the started state
sudo systemctl status cpolar

insert image description here

After the installation and configuration of cpolar is complete, visit the local port 9200 on the browser, and use the cpolar email account to log in to the cpolar web UI management interface

insert image description here

After cpolar is successfully installed, 2 sample tunnels will be installed by default, which can be edited or deleted by yourself:

  • ssh tunnel: point to local port 22, TCP protocol
  • website tunnel: point to local port 8080, HTTP protocol

4.2 Create a tunnel map

We can directly use the sample tunnel ssh, or re-create a tunnel, using the tcp protocol, pointing to port 22. Click Tunnel Management on the left dashboard - Create Tunnel

  • Tunnel name: customizable, be careful not to repeat
  • protocol: tcp
  • Local address: 22
  • Port Type: Random ephemeral TCP port
  • Region: China VIP

click创建
insert image description here

After the tunnel is successfully created, click on the status on the left - online tunnel list, you can see that the tunnel just created has generated a corresponding public network address, copy it

insert image description here

4.3 Test public network remote connection

Open vscode on windows, and use the copied public network address to remote SSH.

Click SSH TARGETSthe button next to the server shown below to connect
insert image description here

If the added connection does not appear on the left, click the button below to refresh

insert image description here

Then click the small arrow --> connect, select ssh type, select linux

insert image description here

insert image description here

Enter the parameters and password to succeed remotely, which is convenient and quick.

insert image description here

insert image description here

The following information appears to indicate that the remote connection is successful

insert image description here

5. Configure a fixed TCP port address

Since the tunnel created above selects a random temporary TCP port, the generated public network address will change randomly within 24 hours, which is inconvenient for users who need long-term remote access. However, we can configure a fixed address for remote connection, and the subsequent remote does not need to check the random public network address before remote.

Note: The function of configuring a fixed TCP port address needs to be upgraded to the professional version package or above to support it.

5.1 Reserve a fixed TCP port address

Open the cpolar official website, log in to the official website background https://dashboard.cpolar.com/reserved , click on the reservation on the left, let's reserve a fixed TCP port address:

  • Region: select China
  • Description: It is a note, which can be customized

After the address is successfully reserved, the system will generate a corresponding fixed public network address and copy it down

insert image description here

5.2 Configure fixed TCP port address

Open a browser on the ubuntu system to access the local port 9200, and log in to the cpolar web UI management interface. Click Tunnel Management on the left - Tunnel List, find the ssh tunnel, click Edit on the right, let's modify the tunnel information, and configure the fixed tcp port address into the tunnel

insert image description here

Modify the tunnel information and configure the successfully reserved fixed tcp address into the tunnel

  • Port type: modified to fixed tcp port
  • Reserved tcp address: fill in the successfully reserved address

click update

insert image description here

After the tunnel is successfully updated, click the status on the left dashboard - the list of online tunnels, find the SSH tunnel, and you can see that the public network address has been updated to a fixed tcp address.

insert image description here

5.3 Test fixed public network address remote

Open vscode and choose to add an ssh connection

insert image description here

Connect using a reserved tcp address

insert image description here

insert image description here

refresh

insert image description here

Refresh the address, click the small arrow on the right "–>" to connect, the following prompt appears, enter yes

insert image description here

A green message appears, indicating that the connection is successful.

insert image description here

Guess you like

Origin blog.csdn.net/qq_62464995/article/details/131029280