Connect Visual Studio Code to FreeBSD remote servers

Update Feb 2, 2024: Recent changes to Visual Studio Code require versions of glibc that aren't provided by the Linux compatibility packages in FreeBSD. This change completely broke my workflow yesterday – a Monday morning and major launch day – resulting in the error The remote host may not meet VS Code Server's prerequisites for glibc and libstdc++.

After some substantial and understandable public outcry, they've decided to walk back the changes for 12 months.

Seeing the writing on the wall, I can assume that support for connecting to FreeBSD remote machines will not be a priority for the team. I want to limit my future risk, so I’ve decided to change my process over to using the SSH FS plugin by Kelvin Schoofs.

The good news is there won’t be any need to install Linux compatibility binaries on FreeBSD machines, so the steps below aren’t necessary. It always felt a bit too clunky of a solution, anyway. You can still connect to remotes, edit files, and even launch remote terminals.

But, the bad news is that some advanced VSCode functionality for remote file systems might not be accessible (Git file status indicators, etc.) Those are nice-to-have features, but not critical for my day-to-day work.

Visual Studio Code’s native SSH remote explorer connection is more than just an SSH tunnel — it needs to be able to execute some Linux binaries on the remote server in order for all of the fancy functionality to work.

When connecting to a FreeBSD server, VSCode may fail to connect and give you errors like:

[] > Unsupported platform: FreeBSD
> exitCode==35==
> osReleaseId==freebsd==
[] Failed to parse remote port from server output
[] Terminating local server
[] > local-server-1> ssh child died, shutting down

Luckily, getting VSCode connecting to FreeBSD isn’t tough.

How to connect VSCode to a FreeBSD remote server

First, on the FreeBSD server, enable Linux compatibility mode, and then install the Linux base core.

sudo sysrc linux_enable="YES"
sudo service linux start
sudo pkg install linux_base-c7

Then, on the machine you’re using VSCode on, edit .ssh/config and add these lines for the FreeBSD server’s entry.

Host xxx
  Hostname xxx
  RemoteCommand /compat/linux/usr/bin/bash
  RequestTTY force

Finally, find the Enable Remote Command setting in VSCode and enable it.

Voila! Head on over to the remote explorer and connect to the server. You’ll now be able to interact with the FreeBSD remote just like it was a Linux host, with all the helpful functionality in VSCode that you’re used to.

It took a long time to figure this one out. Credit to Mateusz Kwiatkowski and Oliver Giersch for the helpful comments I stumbled upon which explained the missing parts of this process.