Scott Hanselman

Cool WSL (Windows Subsystem for Linux) tips and tricks you (or I) didn't know were possible

November 13, '19 Comments [9] Posted in Linux | Win10
Sponsored By

It's no secret I dig WSL (Windows Subsystem for Linux) and now that WSL2 is available in Windows Insiders Slow it's a great time to really explore the options that are available. What I'm finding is so interesting about WSL and how it relates to the Windows system around it is how you can cleanly move data between worlds. This isn't an experience you can easily have with full virtual machines, and it speaks to the tight integration of Linux and Windows.

Look at all this cool stuff you can do when you mix your peanut butter and chocolate!

Run Windows Explorer from Linux and access your distro's files

When you're at the WSL/bash command line and you want to access your files visually, you can run "explorer.exe ." where . is the current directory, and you'll get a Windows Explorer window with your Linux files served to you over a local network plan9 server.

Accessing WSL files from Explorer

Use Real Linux commands (not Cgywin) from Windows

I've blogged this before, but there are now aliases for PowerShell functions that allow you to use real Linux commands from within Windows.

You can call any Linux command directly from DOS/Windows/whatever by just putting it after WSL.exe, like this!

C:\temp> wsl ls -la | findstr "foo"
-rwxrwxrwx 1 root root 14 Sep 27 14:26 foo.bat

C:\temp> dir | wsl grep foo
09/27/2016 02:26 PM 14 foo.bat

C:\temp> wsl ls -la > out.txt

C:\temp> wsl ls -la /proc/cpuinfo
-r--r--r-- 1 root root 0 Sep 28 11:28 /proc/cpuinfo

C:\temp> wsl ls -la "/mnt/c/Program Files"
...contents of C:\Program Files...

Use Real Windows commands (not Wine) from Linux

Windows executables are callable/runnable from WSL/Linux because the the Windows Path is in the $PATH until Windows. All you have to do is call it with .exe at the end, explicitly. That's how "Explorer.exe ." works above. You can also notepad.exe, or whatever.exe!

Run Visual Studio Code and access (and build!) your Linux apps natively on Windows

You can run "code ." when you're in a folder within WSL and you'll get prompted to install the VS Remote extensions. That effectively splits Visual Studio Code in half and runs the headless VS Code Server inside Linux with the VS Code client in the Windows world.

You'll also need to install Visual Studio Code and the Remote - WSL extension. Optionally, check out the beta Windows Terminal for the best possible terminal experience on Windows.

Here's a great series from the Windows Command LIne blog:

You can find the full series here:

Here's the benefits of WSL 2

  • Virtual machines are resource intensive and create a very disconnected experience.
  • The original WSL was very connected, but had fairly poor performance compared to a VM.
  • WSL 2 brings a hybrid approach with a lightweight VM, a completely connected experience, and high performance.

Again, now available on Windows 10 Insiders Slow.

Run multiple Linuxes in seconds, side by side

Here I'm running "wsl --list --all" and I have three Linuxes already on my system.

C:\Users\scott>wsl --list --all
Windows Subsystem for Linux Distributions:
Ubuntu-18.04 (Default)
Ubuntu-16.04
Pengwin

I can easily run them, and also assign a profile to each so they appear in my Windows Terminal dropdown.

Run an X Windows Server under Windows using Pengwin

Pengwin is a custom WSL-specific Linux distro that's worth the money. You can get it at the Windows Store. Combine Pengwin with an X Server like X410 and  you've got a very cool integrated system.

Easily move WSL Distros between Windows systems

Ana Betts points out this great technique where you can easily move your perfect WSL2 distro from one machine to n machines.

wsl --export MyDistro ./distro.tar

# put it somewhere, dropbox, onedrive, elsewhere

mkdir ~/AppData/Local/MyDistro
wsl --import MyDistro ~/AppData/Local/MyDistro ./distro.tar --version 2

That's it. Get your ideal Linux setup sync'ed on all your systems.

Use the Windows Git Credential Provider within WSL

All of these things culminate in this lovely blog post by Ana Betts where she integrates the Windows Git Credential Provider in WSL by making /usr/bin/git-credential-manager into a shell script that calls the Windows git creds manager. Genius. This would only be possible given this clean and tight integration.

Now, go out there, install WSL, Windows Terminal, and make yourself a shiny Linux Environment on Windows.


Sponsor: Like C#? We do too! That’s why we've developed a fast, smart, cross-platform .NET IDE which gives you even more coding power. Clever code analysis, rich code completion, instant search and navigation, an advanced debugger... With JetBrains Rider, everything you need is at your fingertips. Code C# at the speed of thought on Linux, Mac, or Windows. Try JetBrains Rider today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Friday, November 15, 2019 4:49:55 AM UTC
I'm really digging WSL, even if I only use it to run a local redis while developing on windows.

Some things though that I haven't been able to determine:

  1. Do existing WSL installs get the benefit of WSL2? I have an ubuntu install that was created some time ago

  2. If the answer is no, can you "upgrade" from WSL1 to WSL2. Does that even make sense in the world of "WSL"?
Brendan Green
Friday, November 15, 2019 8:59:42 AM UTC
@Brendan you can convert WSL1 distros to WSL2. This needs to be done to get the benefits of WSL2 (AFAIK)

wsl --set-version <Distro> <Version>
Use this command to convert a distro to use the WSL 2 architecture or use the WSL 1 architecture.

: the specific Linux distro (e.g. “Ubuntu”)

: 1 or 2 (for WSL 1 or 2)
Stuart Luscombe
Friday, November 15, 2019 9:00:22 AM UTC
Brendan Green:

"Set a distro to be backed by WSL 2 using the command line

In PowerShell run:

wsl --set-version <Distro> 2


and make sure to replace <Distro> with the actual name of your distro. (You can find these with the command:
wsl -l
). You can change back to WSL 1 at anytime by running the same command as above but replacing the '2' with a '1'.

Additionally, if you want to make WSL 2 your default architecture you can do so with this command:

wsl --set-default-version 2

"
Installation Instructions for WSL 2
Joben
Friday, November 15, 2019 12:05:48 PM UTC
I haven't used this yet but will WSL 2 mean Docker Desktop will loose the 2GB VM?

I ask this because one sound bite, early on, implied the linux kernal is baked into windows so why would docker need that VM?
David Moor
Friday, November 15, 2019 12:28:09 PM UTC
Looks like the "X Server like X410" in the post is broken.
Friday, November 15, 2019 3:45:58 PM UTC
Will there be a Remote Development for Visual Studio?

Will there be Visual Studio extensions for SSH, WSL, and containers?

That is all great that it can be done with VS Code, but my main development is Visual Studio. For which I do deployment to Linux and Containers. Having Remote Development with WSL with Visual Studio would be great.
Sunday, November 17, 2019 7:49:02 PM UTC
Learned !!
Monday, November 18, 2019 5:34:32 PM UTC
It is getting better every time!

You can remove the Google+ icon / integration 😇
Bas van der Veeken
Tuesday, November 19, 2019 7:36:16 PM UTC
It was a little hard for me
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.