Using WSL to get a better terminal and run Linux programs under Windows

Published:  24/04/2020 11:50

Introduction

We tend to work with the terminal a lot and some of our workstations run Windows.

In the past your Windows SSH client of choice would probably have been Putty.

Nowadays a command line SSH client can be installed as a feature in Windows, and before that there were versions of it compiled for Windows anyway.

Shows the Windows SSH client as an installed feature

It's true that Powershell offers a unified environment of powerful commandlets for all of your administrative tasks but sometimes you'd just rather have the likes of grep or sed available.

Git for Windows and Cmder

One nice alternative command line comes with Git for Windows which not only comes with Git as you would expect, but also more GNU tools (including grep) and a bash environments

There's a terminal emulator called Cmder that not only fits really well with Git for Windows but actually provides a package with Git for Windows in it (they call it the "Full" version).

It's simple, looks good, provides tabbed consoles and allows for splitting panels. See it in action here:

The terminal emulator Cmder, with split console tabs

If you installed the full package you should have immediate access to various GNU programs ported to Windows.

For the sake of completeness, let's quickly present another popular terminal emulator: Hyper — Electron-based so quite memory-hungry but with GPU acceleration, tons of options and pretty fast overall.

The Windows Subsystem for Linux (WSL)

Git for Windows sets you up with bash but what if you want to use zsh or install packages as if you were really running Linux?

This is possible through a recent feature called WSL which basically allows binaries compiled for Linux to run on Windows. It's kind of a reverse Wine, if you will.

The general performance of binaries is pretty much native, but all the IO system calls have to be translated first and depending on what you're doing the slowdown can be significant although overall I have to say it's quite impressive.

A few possible use cases for WSL

WSL makes legacy solutions like Cygwin completely obsolete.

With it you could easily:

  • Run a fully featured zsh terminal with Ohmyzsh;
  • Install the Linux version of Docker, which is much faster and predictable than the current version intended for Windows;
  • Install and run classic Linux services on demand such as MySQL, PostgreSQL, Redis, Apache+PHP, ... (Though you may run into compatibility issues for some very specific use cases);
  • Make use of a package manager with the entire chosen distribution library of packages available for you to install — For instance a lot of infosec tools usually run on Linux;
  • Start an SSH server on any Windows machine for remote access (WSL does not have Windows administrative access, though);
  • Pipe Linux binaries output into Windows binaries or vice versa, all of the executables from your Windows PATH environment variable are also available in WSL;
  • Run GUI applications from Linux without using a virtual machine and X redirections inside SSH;
  • Use Dev. environment like Node or PHP without their Windows quirks - For instance the differing syntaxes to delcare and use environment variables.

How to install WSL

It has to be enabled as a Windows feature first.

I usually just type "feature" in the start menu to find the Windows Features list.

Find and install WSL from it then reboot when asked:

Accessing Windows Features from the start menu

Enable WSL in the Windows Features list

When that's done you have to install one of the Linux distributions that are available from the Windows Store.

As we're most familiar with the Debian world we like to pick and install Ubuntu:

Installing Ubuntu from the Windows Store

Getting a WSL terminal from Cmder

Cmder should have a new possible "Task" in your settings. For instance you could modify the "startup" task to always get into a WSL terminal:

Choosing the WSL task from the Cmder settings

Or you could start a new tab (with Ctrl+T) and pick the WSL task for that tab.

Using the Windows Terminal (preview)

There is a promising terminal emulator available through the Windows Store (for free of course) called "Windows Terminal (preview)".

I think the strange title might indicate they could make it preinstalled with Windows in the future as a replacement for the old cmd.

Let's find and download it from the store:

Installing the Windows Terminal (preview) from the Windows Store

Starting the program should create a tab with a Powershell session.

From there, you can click the "+" icon and start a "Ubuntu" terminal.

To make it the default session being started, you can edit the app settings:

The Windows Terminal (preview) app settings location

That should open a text editor showing you the config file in JSON format.

All the available terminal types are in the "profiles" array. Just find the one that matches Ubuntu, and copy its guid:

Finding the Ubuntu profile GUID in the config file

Paste the guid as the value for the "defaultProfile" key which should be somewhere near the top of the file.

There are many more options you can add in the specific profile section for Ubuntu to customize the terminal.

I'm currently using these settings:

{
  "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
  "hidden": false,
  "name": "Ubuntu",
  "source": "Windows.Terminal.Wsl",
  "useAcrylic": true,
  "acrylicOpacity": 0.8,
  "fontFace": "Fira Code",
  "fontSize": 14,
  "historySize": 8000,
  "background": "#333333",
  "colorScheme": "Campbell"
}

Which requires installing the Fira Code font first. Just put in "Consolas" or remove the line altogether for the default font.

The useAcrylic setting creates a background with an effect in the style of Windows Aero graphics.

There are a few pre-existing color schemes. E.g. you should be able to try out these ones:

  • Campbell
  • One Half Dark
  • One Half Light
  • Solarized Dark
  • Solarized Light

Accessing your Windows files and apps

All the Windows drives should be mounted in /mnt:

Shows the output of the df Linux command on WSL

As noted before, WSL may not have access to places that require Windows administrative privileges, whether you use sudo or not.

To start a Windows program that's in the Windows PATH from WSL, you can just type the exectuable name and add ".exe" and it should work.

For instance, to open a file explorer in the current directory from WSL, you can just type:

explorer.exe .

Your bash profile will use a PATH variable that is the concatenation of the Ubuntu and Windows PATHs:

Shows the value of the PATH variable in WSL - Is the bash PATH combined with the Windows one

Running Linux services

From an Ubuntu terminal session you are now able to install packages using apt.

You will have to use sudo with the password you provided during the installation of the Ubuntu package from the Windows store.

For instance you could install a MariaDB server like so:

sudo apt update
sudo apt install mariadb-server-10.1

There is no easy way to make it start with a Windows session, you will have to manually start it using /etc/init.d or:

sudo service mysql start

Any running Linux program will show up in the Windows task manager alongside all of your Windows processes.

From WSL itself you can only see the Linux processes:

Output of the top command in WSL

One of the really interesting use cases mentioned earlier was Docker.

You could follow the procedure from here and get access to even more services through Docker.

Conclusion

The possibilities offered by WSL are huge. There are downsides of course but it's certainly an incredible tool for anyone working with Windows on a daily basis.

The performance of binaries that aren't specifically IO bound is also quasi native, which is really nice.

Comments

Loading...