Skip to content

Instantly share code, notes, and snippets.

@morganwdavis
Last active April 17, 2024 08:05
Show Gist options
  • Save morganwdavis/6c1880d49102c69c8d9b3ca63f41528b to your computer and use it in GitHub Desktop.
Save morganwdavis/6c1880d49102c69c8d9b3ca63f41528b to your computer and use it in GitHub Desktop.
VS Code Remote SSH on FreeBSD

VS Code Remote SSH on FreeBSD

This is old and has been replaced with WHAT YOU REALLY WANT

Inspiration from Configure FreeBSD to work with VScode's remote ssh extension

Steps for VSCode 1.86 and newer

This method works for FreeBSD 13.2 and FreeBSD 14. It uses the debootstrap method to install the latest stable release of Debian 12.4, codename "Bookworm".

Tip: If you previously installed the linux_base-c7 package just for VS Code, you may want to remove it with service linux stop && service linux disable && pkg delete linux_base-c7. You can now reboot in order to remove /compat/linux.

Now follow these steps (explanations):

# pkg install debootstrap
# service linux enable && service linux start
# sysctl compat.linux.emul_path=/compat/debian
# echo "compat.linux.emul_path=/compat/debian" >> /etc/sysctl.conf
# debootstrap bookworm /compat/debian
# ln -sf ../lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 /compat/debian/lib64/ld-linux-x86-64.so.2
# ln -s /compat/debian/usr/bin/mawk /compat/debian/usr/local/bin/awk

Create a .bash_debian file in your $HOME that looks like this:

PATH="/compat/debian/usr/local/sbin:/compat/debian/usr/local/bin:/compat/debian/usr/sbin:/compat/debian/usr/bin:/compat/debian/sbin:/compat/debian/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

If you haven't already done this, allow sshd to accept BASH_ENV references during the connection by adding this to /etc/rc.conf:

sshd_flags="-o AcceptEnv=BASH_ENV"

... and then restart sshd with service sshd restart.

Lastly, make sure you have these two entries in /etc/fstab:

# Linux Compat Device   Mountpoint              FStype          Options         Dump    Pass#
devfs                   /compat/debian/dev      devfs           rw,late         0       0
/home                   /compat/debian/home     nullfs          rw,late         0       0

Edit your ~/ssh/config file on your VS Code machine and add a special host entry just for VS Code connections:

Host vscode-server
	Hostname	my.vscode.server.com
	SetEnv BASH_ENV=".bash_debian"

From Windows, a convenient way to open VS Code onto your remote SSH connection and start in a project workspace is to create a shortcut icon on the desktop and set the target property to something like this:

"C:\Program Files\Microsoft VS Code\Code.exe" --folder-uri vscode-remote://ssh-remote+vscode-server/compat/debian/home/username/path/to/project

... where vscode-server matches the Host identifier in the ssh config entry and everything else after that matches the path to your project. The first time you make the connection, VS Code will ask you to specify your remote platform. Choose "Linux".

Note: The Windows shortcut target specifies /compat/debian/home rather than just /home. While the latter will work for general editing, any use of the built in Source Control management (git) will fail with complaints about your files being outside of the repository.

Steps for VS Code versions prior to 1.86

Summary of steps to enable Visual Studio Code's Remote SSH extension for remotely connecting to a FreeBSD server for editing files. This works only by enabling Linux services on FreeBSD because Microsoft has not created a Unix agnostic remote interface for VS Code.

Enable linuxulator and install linux userland:

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

Test it:

# /compat/linux/usr/bin/uname -a
Linux dev-vm.space.lan 3.17.0 FreeBSD 13.1-RELEASE-p3 GENERIC x86_64 x86_64 x86_64 GNU/Linux

On the remote FreeBSD host:

... create a ~/.bash_linux file with this line:

PATH="/compat/linux/usr/sbin:/compat/linux/usr/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"

... edit /etc/rc.conf and add this line:

sshd_flags="-o AcceptEnv=BASH_ENV"      # Allows VS Code Remote SSH connections

... restart sshd with:

# service sshd restart

On the local host (e.g Windows), create a $HOME/.ssh/config entry

SetEnv BASH_ENV=".bash_linux"

This can be added to a specific Host entry if desired. Example:

Host dev-vm
  SetEnv BASH_ENV=".bash_linux"
  HostName dev-vm
  User mdavis

Next, in VS Code, after installing the "Remote - SSH" extension, add a new remote SSH host via the command pallete by selecting Remote-SSH: Add New SSH Host... and following the prompts. Then connect to the new host via the Open a remote window icon in the lower left or the command pallete by selecting Remote-SSH: Connect to Host.... The first time you do this you need to specify the host platform, and in this case will be Linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment