Self Hosting VS Code Server on Ubuntu

Visual Studio Code is a free and open-source code editor, by Microsoft, which provides an integrated development environment to edit, analyze, and manage code of any programming language. VS Code is an absolutely amazing code editor, it is minimalistic yet ships with tons of developer-friendly …

Visual Studio Code is a free and open-source code editor, by Microsoft, which provides an integrated development environment to edit, analyze, and manage code of any programming language.

VS Code is an absolutely amazing code editor, it is minimalistic yet ships with tons of developer-friendly features like Intellisense, and can be easily extended with hundreds of free extensions.

If you’re using a different code editor, give it a try, it was ranked the most popular developer environment tool in the Stack Overflow 2021 Developer Survey.

Self-hosting a VS Code server can help you have your entire development ecosystem on the cloud, enabling you to code on any device with a consistent development environment; leveraging the power of cloud servers to speed up tests, compilations, downloads, and even help you preserve battery life when you’re on the go.

Alright, with all of that out of the way, let’s get into the good stuff.

Step 1: Creating a Virtual Machine

You’ll need a Linux virtual machine with at least these minimum specs for proper functioning:

  • 1 GB of RAM
  • 2 CPU cores

Pick a server location that’s closest to you for low latency. I’ll be using an Ubuntu VM on Linode, you can use other distros, it should work just fine.

Step 2: Configuring the Virtual Machine for VS Code Server

Now, let’s configure our server:

Step 2.1: Connect to the Server via SSH

Open up the terminal on your device, and run this command:

ssh root@123.45.67.89

If you chose a username while creating the virtual machine, use that instead of root, and replace 123.45.67.89 with the IP address of your VM.

You’ll be prompted with “The authenticity of host…”, just type yes, and then enter the password.

Step 2.2: Configure Automatic Updates

Let’s update packages and configure automatic updates so that our server gets patched automatically.

# Update packages
sudo apt update && apt upgrade

# Install unattended-upgrades
sudo apt install unattended-upgrades

# Configure unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades

# Test unattended-upgrades
sudo unattended-upgrades --dry-run --debug

Step 2.3: Creating a “sudo” user

If your cloud provider didn’t ask you to choose a username while creating it, you are given root access to your server; it is recommended to not use the “root” user, which has unlimited privileges and can execute any command, even ones that could potentially disrupt your server.

Let’s create a new user on a server that can use “sudo” to do day-to-day administration tasks.

# Create a new user
adduser username

# Add user to the "sudo" group
usermod -aG sudo username

# Check user's group
groups username

# Switching users
su - username
su - root

Step 2.4: Configure SSH Keys

Using SSH keys instead of passwords provides you with better security, as SSH keys are far more long and complex than any password could ever be; you can also add an extra password to the SSH keys, requiring both the SSH key and the password to access the server.

Log out of the server or just open up a new terminal on your computer to create SSH keys:

# Create ssh keys
ssh-keygen -b 4096 

# View ssh keys
ls -l ~/.ssh

# Add public key to server
ssh-copy-id -i ~/.ssh/keyname.pub username@123.45.67.89

# Switch ssh keys on client
ssh-add ~/.ssh/keyname

During the ssh-keygen process, you’ll be prompted for file location, use the default one or give a new location by typing in /home/username/.ssh/keyname, and enter a strong password for the SSH key.

In the .ssh folder, there’ll be two files, the one with “.pub” extension is your public key, the other one is your private key, never share the private key with anyone.

You might get a message like Could not open a connection to your authentication agent when switching SSH keys, you’ll need to start ssh-agent first using:

eval `ssh-agent`

Once done, you can log in to the server by just using the ssh username@123.45.67.89 command without entering the user password, although you will need to enter the password of your SSH key.

Step 2.5: Disable root login

Now that we have a new user with limited privileges that can run “sudo” commands and can access the server via SSH keys; let’s lock down our root user, as it is usually the most targeted account by hackers.

To do so, type in sudo nano /etc/ssh/sshd_config, and update PermitRootLogin to no and add AllowUsers username as shown below:

Disable root login via SSH

Optionally, you can also go ahead and disable password-based login via SSH for all users, including the new user account we just created, by updating these values in the same sshd_config file:

# Disable password-based login via ssh for all users [optional]
PasswordAuthentication no
ChallengeResponseAuthentication no

Once done, save the file using Ctrl + O & Ctrl + X, and restart the sshd service using this command:

sudo systemctl restart sshd

Now, your server is ready to install the VS Code Server, let’s get into it.

Step 3: Installing VS Code Server

We’ll be using a script by Coder to install the VS Code Server, it takes care of all the configuration, and you’ll have your VS Code server, up and running in minutes.

Needless to say, you shouldn’t go around executing random scripts you downloaded from the internet, so if you know some bash, read the script, it’s open-source and is hosted on GitHub to make sure there’s nothing fishy in there.

We’ll need curl to download the script, install it if it doesn’t come with your installation:

# Install curl
sudo apt install curl

Once that’s done, you can download and install the VS Code server script using:

curl -fsSL https://code-server.dev/install.sh | sh

Installation should take less than a minute, and you’ll be greeted with something like this:

Ubuntu 20.04.3 LTS
Installing v4.0.1 of the amd64 deb package from GitHub.

+ mkdir -p ~/.cache/code-server
+ curl -#fL -o ~/.cache/code-server/code-server_4.0.1_amd64.deb.incomplete -C - https://github.com/coder/code-server/releases/download/v4.0.1/code-server_4.0.1_amd64.deb
######################################################################## 100.0% ######################################################################## 100.0%
+ mv ~/.cache/code-server/code-server_4.0.1_amd64.deb.incomplete ~/.cache/code-server/code-server_4.0.1_amd64.deb
+ sudo dpkg -i ~/.cache/code-server/code-server_4.0.1_amd64.deb
[sudo] password for username:
Selecting previously unselected package code-server.
(Reading database ... 108221 files and directories currently installed.)
Preparing to unpack .../code-server_4.0.1_amd64.deb ...
Unpacking code-server (4.0.1) ...
Setting up code-server (4.0.1) ...

deb package has been installed.

To have systemd start code-server now and restart on boot:
  sudo systemctl enable --now code-server@$USER
Or, if you don't want/need a background service you can run:
  code-server

Step 3.1: Configuring VS Code Server to run on boot

Now, we need to configure the VS Code server to run on boot:

sudo systemctl enable --now code-server@$USER
#Output
Created symlink /etc/systemd/system/default.target.wants/code-server@username.service → /lib/systemd/system/code-server@.service.

Step 3.2: Check VS Code Server Status

Next up, let’s check VS Code server status:

sudo systemctl enable --now code-server@$USER

If the server is up and running, you should see something like this:

● code-server@username.service - code-server
     Loaded: loaded (/lib/systemd/system/code-server@.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-01-22 11:21:08 UTC; 19min ago
   Main PID: 965 (node)
      Tasks: 22 (limit: 4612)
     Memory: 39.5M
     CGroup: /system.slice/system-code\x2dserver.slice/code-server@username.service
             ├─965 /usr/lib/code-server/lib/node /usr/lib/code-server
             └─992 /usr/lib/code-server/lib/node /usr/lib/code-server

Step 4: Accessing the VS Code Server

You can access the VS Code Server either via Web browser

To access the VS Code server in the browser, you’ll need to set up a tunnel between your device and the VS Code server so that the remote traffic on port 8080 is forwarded to your machine and vice-versa.

ssh username@123.45.67.89 -L 8080:localhost:8080

It’ll ask for your password and log you in, creating a two-way tunnel between your local device and the VS Code server.

Now, open up your browser, and go to this URL to access VS Code:

localhost:8080

You’ll be greeted with a login screen like this:

VS Code login page
Login page of VS Code Server

Step 4.2: Password for VS Code Server

You can find the password for your code server in the config.yaml file:

cat .config/code-server/config.yaml
# Output
bind-addr: 127.0.0.1:8080
auth: password
password: XXXXXXXXXXXXXXXX
cert: false

Use that password to login using your browser, you can also remove password by editing the config.yaml file:

# Editing config.yaml file
.config/code-server/config.yaml

Just change the auth to none:

bind-addr: 127.0.0.1:8080
auth: none
password: XXXXXXXXXXXXXXXX
cert: false

You’ll still need to set up SSH tunneling between your device and the SSH server, so your VS Code server will still be protected via the SSH password or your SSH key.

That’s all folks!

I’ll update this tutorial soon with guides on accessing that VS Code server online without SSH tunneling.

Photo by Caspar Camille Rubin on Unsplash

Leave a Comment