Develop and test on a remote server - XLRIT/gears GitHub Wiki
- 1. Table of content
- 2. Introduction
- 3. What the VPS administrator has to do for you
- 4. Prepare safe passwordless connection
- 5. Install and configure VS code extension: Remote - SSH
- 6. Connect to the remote server
- 7. Install VS Code extensions on remote server
- 8. Develop and debug on remote server
In VS Code it is remarkably easy to develop debug on a remote server (e.g. the Linux servers from our cloud vendor Masero or Contabo). And weirdly enough you can do that all directly from Windows or any operating system for that matter. This page describes how:
On the remote server login (as root) and execute the following commands (using user edwin
as an example):
adduser edwin # add user. The system will ask some user details
adduser edwin sudo # add this user to sudo group
One of the safest and also easiest ways connect to server is to have a passwordless connection based on something that is called a "private public key pair". This sounds really technical but it is simply the equivalent of a key and a lock. The key is the part you keep yourself and is therefore private and the lock public. You can give this to anyone that has a door which they can put this lock on. That is exactly what we are going to do for user edwin
:
- On your local machine run command:
ssh-keygen
and keep entering until it is done.
By default this will create 2 files in our .ssh
folder (e.g. c:\Users\edwin\.ssh
).
-
id_rsa
(the private key; the key in our metaphor) -
id_rsa.pub
(the public key; the lock in our metaphor)
These files may already exist. DO NOT OVERWRITE THEM. If they already exist you can simply do it again but give another name and make sure that the generated files are in your
.ssh
folder.
- give more logical names. For instance if you want to contact to a contabo server called
contabo_test1
you can rename them to
old name | new name |
---|---|
id_rsa |
contabo_test1.priv |
id_rsa.pub |
contabo_test1.pub |
- Add the content of the pub file on the server in in file
/home/edwin/.ssh/authorized_keys
(just copy paste it as an extra line in this file). This way only you can open a connection to that server because only you have the right key to open the lock. Besides that you never have to retype a password again to connect. Tip: if you cannot do this you have to ask the administrator of the VPS to do this for you.
Private keys are like passwords! So make sure nobody can get to them. Encrypting the drive of your computer and locking your computer when you are away as well is mandatory at XLRIT. If you have not done so, do that now. Besides this it may be even more secure to sometimes replace your public private key pairs with new ones.
- Install the VS code extension called Remote - SSH.
- Press F1 -> Remote SSH: Open Configuration File.
- Select the 1st (default) option. Which should be
<your user folder>\.ssh\config
(e.g.C:\Users\edwin\.ssh\config
). - Add the following (example) text in it (and replace to match your needs)
Host contabo_test1
HostName 2.58.82.174
User edwin
IdentityFile C:\Users\edwin\.ssh\contabo_test1.priv
Example value | Replacement value |
---|---|
contabo_test1 |
The name you want to give to this connection |
2.58.82.174 |
the IP address of the remote server |
edwin |
your username |
C:\Users\edwin\.ssh\contabo.priv |
the path to the file that contains the private key |
- Press F1 -> Remote-SSH: Connect to Host
- Choose the name you just gave to the connection (e.g.
contabo_test1
). This will open up a new VS Code windows that looks like this:
In the left lower corner you see that this VS Code Window is connected via SSH to the remote server. In the lower right you also see a terminal opened on the server. In the left pane you see the options to Open Folder and even Clone Repository.
- Click Open Folder
- Enter
/
(the root folder) and click OK.
If you open the Explorer view in the left pane of the VS Code window, you will see all folders and files on this server. For example:
Good to know: you can browse through them and even drag and from files from a normal Windows Explorer to the VS Code Explorer View and vice versa.
In this new VS Code Window that is connected to a remote server you can install all sorts of VS Code extensions. For instance Git Graph or Extension Pack for Java so you can develop and debug Java projects on this remote server (how cool is that 🙂).
Not all extension can be installed but those that can will tell you with the special Install in ... button and a warning icon next to it:
Let's assume you want to develop and debug Java on the remote server, then this is what you need to do:
- Install the VS code Extension Pack for Java in SSH ... (on the remote server).
- If you have not yet installed java on the remote server run the following commands:
sudo apt update
sudo apt install openjdk-17-jre
- To go back to the initial state of the first connection simply F1 -> Remote: Close Remote Connection and F1 -> Remote-SSH: Connect Current Window to Host.
- In the Explorer View click Clone Repository.
- You can choose any Java repository but in this example we will choose an available hello world from GitHub:
- Choose Clone from GitHub
- Type
HoseinGhanbari/vscode-java-debug-hello-world
and press ENTER
- As a location choose a git folder on your remote server. Most use a folder called git in their user folder. In my case that would be
/home/edwin/git/
. - Press OK and choose Open
- Open the file
.../src/main/java/com/example/App.java
and click on the left to line which saysSystem.out.println("Hello World!");
to create a red dot (a breakpoint):
- Press F5. This will start the application in debugging mode and will stop at the breakpoint. It will look a bit like this:
- Press F5 again will resume the application until it finds another breakpoint. In this example it will not find one which means the app finishes. It should show
Hello World!
in the Terminal console.
In short VS code makes it possible to do development directly on the remote server without having to leave your own computer. This is a big plus for anyone that wants to develop and debug something on that server and know for sure it will work on there.