Scott Hanselman

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

November 13, 2019 Comment on this post [31] 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
Hosting By
Hosted in an Azure App Service
November 15, 2019 8:49
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"?
November 15, 2019 12:59
@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)
November 15, 2019 13:00
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
November 15, 2019 16:05
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?
November 15, 2019 16:28
Looks like the "X Server like X410" in the post is broken.
November 15, 2019 19:45
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.
Guy
November 17, 2019 23:49
Learned !!
November 18, 2019 21:34
It is getting better every time!

You can remove the Google+ icon / integration 😇
November 19, 2019 23:36
It was a little hard for me
October 15, 2020 4:45
Today, I went to the beachfront with my kids. I found a
sea shell and gave it to my 4 year old daughter and said
"You can hear the ocean if you put this to your ear." She put the shell to her ear
and screamed. There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is completely off topic but I had
to tell someone!
October 17, 2020 0:51
Awesome article.
October 18, 2020 11:22
Very good article. I certainly appreciate this website. Thanks!
October 21, 2020 1:19
Hey I am so excited I found your blog, I really found you by accident, while I was looking on Aol for
something else, Anyhow I am here now and would just
like to say many thanks for a tremendous post and a all round thrilling blog (I also love the theme/design), I don't have time to look over
it all at the minute but I have saved it and also included
your RSS feeds, so when I have time I will be back to
read a lot more, Please do keep up the awesome work.
October 21, 2020 8:06
If some one desires to be updated with newest technologies therefore
he must be visit this site and be up to date everyday.
October 21, 2020 17:07
My developer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the costs.

But he's tryiong none the less. I've been using WordPress on a number of websites for
about a year and am worried about switching to another platform.
I have heard fantastic things about blogengine.net.
Is there a way I can transfer all my wordpress posts
into it? Any help would be greatly appreciated!
October 22, 2020 6:30
I visited various blogs but the audio feature for audio songs present at this web site
is truly superb.
October 24, 2020 9:40
Liposuction is the removal of fat cells from the body by suction-assisted surgery.


With liposuction, the surgeon restores the harmonious shape of the patient’s body.
Removing stubborn fat deposits can improve the cosmetic appearance of any
individual.
October 25, 2020 23:51
Spot on with this write-up, I seriously think this site needs much
more attention. I'll probably be returning to see more,
thanks for the advice!
October 27, 2020 9:51
Woah! I'm really enjoying the template/theme of this blog.
It's simple, yet effective. A lot of times it's very difficult to get that "perfect balance" between user friendliness and appearance.

I must say you have done a awesome job with this. Additionally, the blog loads extremely quick for me on Safari.
Exceptional Blog!
October 30, 2020 5:47
If you consider to have earnings, get started immediately.
You will earn all the stuff For example, So, As, I would say about toyota,
house, baks, rich stuff. reached a process to create
thatm quick Slottica Registration; Https://Slottica-Com.Com/Signup, makes to lead the game everywhere.
In advance advise to see a few sited to know. People could have rough
going, although its kindly. And piece of luck
or mind will help https://slottica-com.com/signup/
October 31, 2020 6:52
This paragraph is truly a nice one it helps new web users,
who are wishing for blogging.
October 31, 2020 20:20
Hello to every , for the reason that I am genuinely eager of reading this blog's post to be updated regularly.

It contains nice data.
November 01, 2020 19:34
Thank you for sharing your thoughts. I truly appreciate your efforts and I will be waiting for your next post thank you once again.
November 01, 2020 20:32
Hello, after reading this awesome article i am also happy
to share my knowledge here with mates.
November 01, 2020 23:15
Hello Dear, are you really visiting this site on a regular basis, if so afterward you
will absolutely obtain nice knowledge.
November 02, 2020 19:10
What's up everyone, it's my first visit at this web site, and paragraph
is really fruitful in favor of me, keep up posting these articles or reviews.
November 02, 2020 19:59
We stumbled over here coming from a different web address and
thought I might check things out. I like what I see so i am just following you.
Look forward to finding out about your web page yet again.
November 04, 2020 1:43
It's difficult to find experienced people on this subject, but you seem like you know what
you're talking about! Thanks
November 04, 2020 6:59
This piece of writing is in fact a nice one it helps new net
people, who are wishing in favor of blogging.
November 04, 2020 10:51
Post writing is also a excitement, if you know then you can write otherwise it is
complicated to write.
November 05, 2020 13:22
Hi there, You've done a great job. I'll definitely digg it and personally recommend to my friends.
I am confident they'll be benefited from this web site.

Comments are closed.

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