Skip to main content

VSCode + WSL: the SSH way

The initial idea

early 2017, Rich Turner made a demo where Visual Studio was used to develop C++ code, however the debugging environment targeted was WSL!
With some magic, he showed something that he had forbidden from the initial release of WSL: writting directly to the WSL filesystem (still a big no-no actually).

Of course, this triggered a lot of curiosity and the WSL Corsair had to loot from it too!

SSH to save the day

the biggest challenge was to find a way that not only could be easy to setup on both ends, and also would be approved by the WSL team. 
TLDR: if you use the Windows Path, You Die! #ThereBeDragons
Another way, more Linux-oriented, needed to be used and hopefully a solution already existed and was simply waiting on being used: SSH🔐

The setup

the setup is done in 2 distinct parts:
  1. Setup the SSH server on WSL
  2. Setup the SSH client on VSCode 
While the SSH server is a standard procedure in Linux, finding the right plugin for VSCode was a bit more challenging.

Still I’m glad to say that I finally found THE plugin that not only provides SSH support but also provides a more complete “developer environment experience”.

Prerequesites

Before we start, there is 2 prerequisites that helped me a lot and I would like to share it:
  1. For the OpenSSH server in your WSL environment, the guides in Internet are legion, but so far the “full solution” that is working all the time for me is the answer of this post: <https://superuser.com/questions/1111591/how-can-i-ssh-into-bash-on-ubuntu-on-windows-10>
  2. VSCode as an integrated console which is Powershell by default. If you want to avoid going back and forth to the WSL console, you can setup the VSCode console to be your WSL shell.
  3. You can change your user-settings (CTRL+,) with the following line for Ubuntu flavour:
    “terminal.integrated.shell.windows”: “C:\\Users\\<Your Username>\\AppData\\Local\\Microsoft\\WindowsApps\\ubuntu.exe”

Configuration

Once OpenSSH server is configured in WSL, the next step is to configure the VSCode plugin (of course, the help file is to read!):
  1. Install VSCode and/or VSCode-Insiders (this version allows you to use multi root directories in the same workspace)
  2. Install the plugin “FTP-Simple”
  3. Open the command bar (CTRL+SHIFT+P) and search for ftp-simple: config
  4. Create a first basic configuration just to ensure everything is working fine
  5. Open the command bar (CTRL+SHIFT+P) and search for ftp-simple: Remote directory to Workspace
  6. Choose your connection according to the name you provided in step 4
  7. [Optional] If you didn’t insert the password, type the password to the WSL account
  8. Choose from where the initial path should be open
  9. The workspace should be now open with the files and directories from the path you provided in step 4

Finally, let's play

Now that everything is installed and configured, open a WSL terminal (in or out of VSCode depending on your setup), do some changes in your files from VSCode and see the changes being applied to your WSL files.

Last (random) comments

  • This solution does not work with a separated terminal opened. The Workspace of VSCode keeps the SFTP connection alive, that’s what allow us to directly modify the files without having a new VSCode window being open
  • Your initial directory is the new ‘/’. You can think of it as a ‘chroot’. This means that if you try to open a file that is ‘below’ your initial directory, it will not work
  • Microsoft is working hard on bringing full interopability between WSL and Windows applications (filesystem included). So consider this post to be obsolete in the (near?) future.
  • The SSH solution is not my idea and it’s just an adaptation of Microsoft own solution for C++ and WSL <https://blogs.msdn.microsoft.com/vcblog/2017/02/08/targeting-windows-subsystem-for-linux-from-visual-studio/>.
Happy coding in your brand new WSL-VSCode environment.


>>> Nunix Out <<<

Comments

Popular posts from this blog

Docker + WSL: Get 2 daemon for the price of 1

Introduction almost two years ago, Docker announced the capability of switching between the Linux and Windows containers "mode" (far from the right click that we have today). At that time, I wrote a blog post on how to run both daemons at the same time ( http://darthnunix.blogspot.ch/2016/10/docker-for-windows-2-daemons-enter-in.html ) Fast forward to 2018, and while we were blogging on how to get the TLS connection from WSL docker client with  Rory McCune  ( https://raesene.github.io/blog/2018/03/29/WSL-And-Docker/ ), another blog post, by Stefan Stranger drew my attention (read: blew my mind) as I was trying to reproduce the same: how could I "bind" the docker socket in WSL with the Docker for Windows Linux mode socket ( https://blogs.technet.microsoft.com/stefan_stranger/2018/04/02/access-my-docker-for-windows-kubernetes-cluster-from-debian-wsl/ ) From 1 to 2 daemon: DemonHunter mode achieved Now that we have all the required setup resources, let's b...

WSL: One Home to host them all

Introduction This blog post will explain how a single home mount can be shared accross all  the WSL instances. It was quite fun to find the idea and then make it happen. It will use all the tools currently available from WSL, so please don't expect a "Linux only" as Interopability will be heavily used. And before I start, here is my point of view about WSL: It's not only  Linux! It's different, just like GNU is different from Unix (yes I said it). We have finally the strength of both worlds combined. And while I do understand the "dev" aspect is meant to be reproducible in a Linux prod environment, the "ops" side is meant to take advantages of everything that can make the full environment feeling Home. Setup requirements In order to really enjoy this solution, I do recommend having 2 or more WSL distros installed or, if you fell like a real WSLCorsair, try @bketelsen crazy setup (I love it)! While the distros are downlo...