[vscode remote development] Use intranet penetration to achieve remote access in a public network environment

Article directory

  • Preface
  • 1. Install OpenSSH
  • 2. vscode configure ssh
  • 3. LAN test connection to remote server
  • 4. Public network remote connection
    • 4.1 Install cpolar intranet penetration on ubuntu
    • 4.2 Create tunnel mapping
    • 4.3 Test public network remote connection
  • 5. Configure a fixed TCP port address
    • 5.1 Reserve a fixed TCP port address
    • 5.2 Configure fixed TCP port address
    • 5.3 Test fixed public network address remotely

Foreword

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

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

So in this article tutorial, we will implement remote development through vscode, and implement remote connection in the public network environment through cpolar intranet penetration, so that you can remotely connect to the server to develop and write code anywhere you go.

1. Install OpenSSH

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

After opening it, enter the following command:

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

The result at this time is that OpenSSH is not installed on the computer:

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

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

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

After it shows that OpenSSH is successfully installed, open the local cmd and enter the command ssh.

ssh

If the result displayed is this, the explanation is correct:

2. vscode configuration ssh

Install vscode on windows and open it after successful installation.

Click on the extension in the left toolbar, search for “ssh”, select the first Remote - SSH to install (I have already installed it, and everything is disabled at this time)

Click the “Settings” button in the lower left corner, then click “Settings”

According to the prompts in the screenshot, find Show Login Terminal and check this option

Click the Remote button in the left toolbar to view the remote connection. Click the button next to the server displayed below SSH TARGETS to perform the connection operation.

3. LAN test connection to remote server

Take ssh to connect to ubuntu as an example. After opening vscode, first try to use the LAN address to ssh to remote ubuntu.

Enter the command into the dialog box and press Enter

ssh username@ip

Choose a profile

After the input is completed, the ssh connection just added will be displayed on the right. Click the –> icon to connect. When the password appears, enter the password.

A green sign indicates successful connection

4. Public network remote connection

At present, we can only perform remote operations within the LAN, which has certain limitations. However, we use intranet penetration to achieve remote connection in the public network environment. Here we use the tool cpolar to achieve intranet penetration. There is no need for a public IP address or a router. The operation is simple.

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

4.1 Ubuntu installation cpolar intranet penetration

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

  • 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 displays normally, the installation is successful.
cpolar version
  • token authentication

Log in to the cpolar official website backend, click Verify on the left, check your authentication token, and then paste the token in the command line

cpolar authtoken xxxxxxx

  • Simple penetration test
cpolar http 8080

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

  • Add services to the system
sudo systemctl enable cpolar
  • Start cpolar service
sudo systemctl start cpolar
  • Check the service status. As shown in the figure below, active means it is started.
sudo systemctl status cpolar

After cpolar is installed and configured, access the local port 9200 on the browser, and use the cpolar email account to log in to the cpolar web UI management interface.

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

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

4.2 Create tunnel mapping

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

  • Tunnel name: Customizable, be careful not to repeat it
  • Protocol: tcp
  • Local address: 22
  • Port type: Random temporary TCP port
  • Region: China vip

Click Create

After the tunnel is successfully created, click Status on the left – Online Tunnel List. You can see that the tunnel just created has generated a corresponding public network address. Copy it.

4.3 Test public network remote connection

Open vscode on Windows and use the copied public address to remotely SSH.

Click the button next to the server shown below SSH TARGETS to connect

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

Then click the small arrow –> Connect, select the ssh type and select linux.

Enter the parameters and password to achieve remote success, which is convenient and fast.

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

5. Configure 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 to be remote for a long time. However, we can configure a fixed address for remote connection. There is no need to check the random public address before remote connection.

Note: The function of configuring a fixed TCP port address needs to be upgraded to the Professional Edition package or above to be supported.

5.1 Reserve a fixed TCP port address

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

  • Region: select china
  • Description: Notes, customizable

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

5.2 Configure fixed TCP port address

Open the browser on the ubuntu system, access the local port 9200, and log in to the cpolar web UI management interface. Click Tunnel Management – Tunnel List on the left, 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

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 to update

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

5.3 Test fixed public network address remotely

Open vscode and choose to add an ssh connection

Connect using reserved tcp address

refresh

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

A green message appears, indicating a successful connection.