DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Windows Subsystem for Linux - explained by a Linux user
Rico Sta. Cruz
Rico Sta. Cruz

Posted on

Windows Subsystem for Linux - explained by a Linux user

You can now run Linux apps on Windows thanks to a feature called Windows Subsystem for Linux ("WSL" for short). But what is it, exactly? Is it an emulation layer like Wine? Is it a VM like VirtualBox? Is it dual-booting like Boot Camp? It's none of the aboveβ€”it's something pretty unique and hard to explain.

πŸ‘‡

Next: Let me try to explain it anyway!




Ubuntu as a Windows app

To get Linux working on Windows, you first need to install Linux. Not from an ISO... but from the Microsoft Store (think: Windows's version of the Mac App Store). A full guide to installing WSL is available from Microsoft.

Linux distributions are available as apps. To be specific, they're command-line apps, just like git or vim.

Many devs use Ubuntu as their Linux distribution in WSL.

πŸ‘‡

Next: To understand WSL, let's first look at how the Windows command line works.




Command-line apps

Windows has support command line interface (CLI) apps, just like Linux. In fact, many built-in system utilities are CLI apps.

πŸ’‘ Try this: press Win + R, then run ping 8.8.8.8. It will open a new window.

ping.exe is a built-in command line application.

πŸ‘‡ Next: Let's see what makes this window work.




Windows console host

CLI apps are ran inside the Windows console host. The console host, or conhost.exe, is a GUI app that draws a terminal inteface in a graphical window.

πŸ’‘Try this: open the task manager (Ctrl + Shift + Esc). Now run ping -t 8.8.8.8. You'll see a new application in the task manager called TCP/IP Ping Command. Under this group are 2 processes:

  • ping.exeβ€”the command you just ran; and
  • conhost.exeβ€”also known as Console Window Host, the GUI that shows ping's output.
Windows Task Manager showing two processes: conhost.exe and ping.exe

ping.exe is running alongside the conhost.exe GUI.

In Linux terms: Think of conhost.exe as a terminal emulator, like XTerm might be for Linux. It's a graphical UI that shows terminal output. ping.exe runs on conhost.exe in the same way /usr/bin/ping runs inside Xterm.

πŸ‘‡ Next: Let's see how the Windows Command Prompt works.




Cmd.exe on Conhost

All CLI apps open in conhost.exe. This includes the Windows Command Prompt, or cmd.exeβ€”which is a CLI app, just like ping.exe!

πŸ’‘ Try this: Go to Win + R and run cmd.exe. You'll see this open in the same Windows console host window that ping.exe opened.

cmd.exe opens in a Conhost window.

In Linux terms: Think of cmd.exe like the Bash shell. A terminal window in Linux will run Bash, and will close the window when Bash exits. cmd.exe (the shell) and conhost.exe (the terminal GUI) works the same way.

πŸ‘‡

Next: This may be the case for the Command Prompt, but why does Powershell's window look different?




Powershell's blue window

If you open Powershell, you'll notice that the window is blue. It's not a different app, it's still the same conhost.exe with different settings! You'll notice it has the same menus as cmd.exe.

Powershell.exe is a CLI app, tooβ€”just like cmd.exe. Conhost simply lets you choose different settings per app. It simply comes with different color settings for Powershell by default.

Powershell's Properties window lets you configure conhost settings.

In Linux terms: Most terminal emulators on Linux would support different profiles, and you may be assigning different profiles per command. Windows's conhost.exe has a different profile for Powershell that has its Screen Background set to blue.

πŸ‘‡

Next: Is WSL just like cmd.exe and Powershell then?




The WSL launcher: wsl.exe

Yes! You can think of WSL as another CLI application just like cmd.exe and powershell.exe. Just like other CLI apps, you can run wsl.exe. This is the WSL Launcher, which boots up the WSL platform and drops you into the bash (your user's shell by default).

πŸ’‘ Try this: Try running wsl.exe via Win + R. This should get you a Linux bash prompt running inside a conhost.exe window. Seems very similar to Cmd and Powershell, doesn't it?

Running wsl gets you a conhost window, just like cmd.exe.

πŸ‘‡

Next: Let's try mixing all these together.




Shells inside shells

Windows's cmd.exe and powershell.exe can run command line applications. You can use it to run ping.exe, chkdsk.exe, orβ€”wouldn't you know itβ€”even wsl.exe.

πŸ’‘ Try this: Try running wsl.exe or bash.exe from within a Command Prompt (cmd.exe). In fact, try running wsl.exe inside powershell.exe inside cmd.exe, or any combination above! Typing exit will bring you one level out.

In Linux terms: Think of them just like shells. Windows's Cmd might be like Bash, and Powershell might be Fish or Zsh. wsl.exe works the same way.

πŸ‘‡

Next: Why do the backslashes (\) in Windows appear as forward slashes (/) in WSL?




The WSL filesystem

WSL has its own filesystem! If you do ls / inside WSL, you'll find /usr, /bin and the other usual suspects in a Linux filesystem. This is a full POSIX-compliant filesystem managed by WSL, away from the limitations of your Windows's NTFS.

πŸ’‘ Try this: Go to your Linux's home directory with cd. Create two files with the same filename, but different case: touch xyz.txt XYZ.TXT. These two files can't co-exist in Windows's case-insensitive filesystem, can it? They will live happily inside WSL just fine.

In Linux terms: Think of WSL as a separate Linux systemβ€”a bit like a VirtualBox VM. It has its own filesystem, processes, /dev devices. Running wsl.exe will tunnel you into this Linux subsystem.

πŸ‘‡

Next: So is it a virtual machine?




Linux in a box

Not only does WSL have its own filesystem, it's a full Linux system that runs as an app in Windows! It may be easy to think it's like VirtualBox, but it's not a VM.

Why is it not a VM? There's no CPU or hardware emulation involved like in a VM. A whole Linux system runs like any other app in Windows.

πŸ’‘ Try this: Run htop inside WSL (you may need to install it first), and view it as a tree (shift-T). It has a whole process tree, even with an /init process! You'll see your machine's full CPU and memory capacityβ€”it's not pre-allocated, unlike a VM. It shares these hardware resources just as any other Windows app would.

In Linux terms: On a typical system, Linux's entry point would be in /boot/vmlinuz-linux, which is fired by a bootloader like GRUB. Think of WSL as a version of this packaged as a wsl.exe file. The whole Linux subsystem will be running as a process inside Windows.

Note about WSL2: There are two versions of WSL. The new experimental version, WSL2, actually works as a Virtual Machine.

πŸ‘‡

Next: If WSL is a separate subsystem from your Windows environment, how do they share files?




Windows files inside WSL

Your Windows files are visible inside WSL under /mnt/c.

πŸ’‘ Try these: Here are some experiments you can try inside WSL:

  • Case insensitive files: In WSL, try cd /mnt/c/Windows and cd /mnt/c/WINDOWS. They'll both work! The Windows filesystem you're accessing in /mnt/c is case-insensitive.

  • Check mount points: Run findmnt. You'll see /mnt/c is mounted as drvfs, just as how you would mount a drive in Linux.

  • Two home directories: Look at /mnt/c/Users. You'll see you have 2 home directories: your WSL/Linux one in /home, and your Windows one in /mnt/c/Users.

  • Permissions won't work: Try to make a file in /mnt/c and run chmod to change its permissions. It won't workβ€”the filesystem these files live on is NTFS, which doesn't support UNIX-style permissions.

findmnt will tell you where /mnt/c comes from.

πŸ‘‡

Next: We can see Windows files in WSL. Can we see WSL files in Windows?




WSL files from Windows

WSL exposes your files as a network resource in \\wsl$. (The actual files live somewhere in C:\Windows, though it's not a good idea to use them directly.)

πŸ’‘ Try this: Open Windows Explorer. In the location bar, type in \\wsl$\Ubuntu. This should get you access to the WSL filesystem as a network folder.

In Linux terms: \\wsl$\Ubuntu is called a UNC path, which is Windows's way of accessing network resources. Think of it like smb://wsl/Ubuntu.




Running Windows apps in WSL

You can run Windows *.exe programs inside WSL. It will simply open them in Windows, just as you would have ran those programs from Windows itself.

πŸ’‘ Try this: In WSL, type notepad.exe. In fact, you can even try running cmd.exeβ€”this should get you a Windows command prompt! To see how this works, try which notepad.exe, which shows that it's simply running /mnt/c/WINDOWS/system32/notepad.exe.

user@desktop ~$ notepad.exe

In Linux terms: Linux can run ELF binaries (ie, Linux's native executable format). Once it finds a PE32+ binary (ie, Windows's format), it'll simply tell Windows to open it outside of WSL. Also, Windows's %PATH% carries over into Bash's $PATH, making the system .exe files available anywhere in WSL.




Running WSL apps in Windows

Linux apps can be ran from Windows using wsl.exe, the WSL launcher. This starts up the WSL subsystem (if it hasn't been yet) and runs a command from within it.

Try this: Open a command prompt (Win+R to run cmd.exe), then type: wsl ls /

Microsoft Windows [Version 10.0.18985.1]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\>wsl ls /
bin/   dev/  home/  lib/    lost+found/  mnt/  proc/  run/
boot/  etc/  init*  lib64/  media/       opt/  root/  sbin/
...

πŸ‘‡

Next: Can we replace conhost?



Alternatives to conhost

You don't need to use conhost.exe to run CLI apps. You can run them in third party terminal emulators. There's wsltty (based on Cygwin's mintty), Terminus (an Electron-basde terminal), Conemu (supports tabs and panes), and a few more.

Any of these apps can run any CLI application. They often usually come with built-in shortcuts for cmd, Powershell, WSL, and possibly a few others.

In Linux terms: Just like XTerm has many alternatives, conhost has its own alternatives as well.

Windows Terminal is an alternative to conhost. It supports WSL, Powershell, cmd, and more.

πŸ‘‡

Next: Let's recap what we know so far.




Recap

  • Ubuntu is a command-line app. It's installed from the Microsoft Store.

  • Windows provides wsl.exe that launches an installed Linux distribution (Ubuntu in this case). This is a command-line program that brings you to a Bash prompt. There's also bash.exe, which does the same thing (it's like an alias for "wsl.exe bash").

  • CLI apps: are ran inside conhost.exe, the built-in Windows terminal. There are alternatives to this that developers might like better.

  • Environments: cmd.exe, powershell.exe, and wsl.exe

  • WSL launcher: wsl.exe runs the WSL subsystem and drops you into a Bash prompt. It's just a CLI app.

Top comments (0)

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.