- Interact with Azure Cosmos DB with PowerShell - Tue, Sep 14 2021
- Azure health services: Track Microsoft cloud outages and maintenance - Wed, Sep 8 2021
- Powerline: Customize your PowerShell console - Tue, Aug 31 2021
I remember the days (not so long ago) when Windows and Linux were completely separate ecosystems. Many of the Linux-based users I supported dreaded anything to do with Windows, and, for the most part, vice-versa.
Welcome to the 21st century, DevOps methodology, and a new Microsoft that embraces open-source software and cross-platform integration. Microsoft knows that Windows-stack developers work from Windows 8.1- or Windows 10-based workstations; this is one of the reasons why client Hyper-V exists.
To that point, Microsoft also wants to give both developers and systems administrators greater reach into running (and potentially developing and maintaining) *NIX-based software. Hence, Windows 10 includes a brand-new optional feature called Windows Subsystem for Linux (WSL).
4sysops authors Anil Erduran and Michael Pietroforte have both written quite a bit on the Bash environment in pre-release Windows 10 builds. Here are some links if you're interested:
- Is Bash the Next Big Thing After PowerShell?
- Install Bash on Windows 10
- Working with Windows 10 Bash
- Five Fun Things You Can Do with Windows 10 Bash
Today, I'd like to focus on how the WSL feature works in the "latest and greatest" Windows 10 release, build 14393, also known as the Anniversary Update Edition.
Pop open an administrative Windows PowerShell console and run the following command to check your operating system version and build number:
[Environment]::OSVersion.Version Major Minor Build Revision ----- ----- ----- -------- 10 0 14393 0
Install Bash on Windows ^
From your elevated PowerShell session, run the following command to check the WSL status:
Get-WindowsOptionalFeature -Online | Where-Object -Property FeatureName -like *linux* FeatureName : Microsoft-Windows-Subsystem-Linux State : Disabled
Because Bash on Windows is aimed primarily at open-source application developers, it makes sense that we need to enable developer mode. See Anil's article for instructions on how to do that.
Like the effective PowerShell scripters we are, let's now use the related Enable-WindowsOptionalFeature cmdlet to perform the installation:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -All
You'll be prompted to restart the system. Re-open an elevated PowerShell console when you're back online.
If you've played with the WSL feature in pre-release Windows 10 builds, you'll notice that we're no longer required to enable developer mode or join the Windows Insider program.
To get started, simply type bash and press ENTER. You'll see the following prompt:
PS C:\> bash -- Beta feature -- This will install Ubuntu on Windows, distributed by Canonical and licensed under its terms available here: https://aka.ms/uowterms Type "y" to continue:
Make sure your Windows 10 system is connected to the Internet because bash.exe will immediately connect to the Windows Store, of all places, and download Ubuntu on Linux, distributed by Canonical.
Ubuntu is one of the leading Linux distributions for both commercial and residential use, and Canonical is its developer. Microsoft and Canonical are now BFFs, evidently.
After the environment is installed, you'll be prompted to create an administrative UNIX user account. Note that this is a completely separate identity from your Windows user accounts.
Working in the Bash/Windows environment ^
Judging from Microsoft's WSL FAQ on MSDN, the Bash for Windows development team added significant functionality to what was available in pre-release Windows 10. For instance, we can now use the Advanced Packaging Tool (apt) to manage Linux software.
Let's begin by viewing which package repositories the WSL environment is set to use by default:
Next, let's make sure that our local package database is current:
sudo apt-get update
Of course, we use sudo to run a Linux command in an administrative context. The default UNIX user you created earlier in this process has the ability to act as the root, the Linux superuser.
Let's install the full git source code control toolset in our environment. We'll begin by searching our local package cache for the package:
apt-cache search git-all
Then we'll install, letting apt resolve any necessary package dependencies:
sudo apt-get install git-all
Cool. Let's verify the git client version:
tim@win10box:/mnt/c$ git --version git version 1.9.1
The Ubuntu file system starts you in the /mnt directory, from which you can path out and browse the hardware host's file system. For instance, the host's C: drive is mounted as /mnt/c$.
In the following procedure, we (a) run a directory listing, (b) create a new folder, (c) enter that folder, and (d) create a new text file in our new folder.
ls mkdir linuxfiles cd linuxfile nano new.txt
You'll find yourself in the nano text editor. Full disclosure: I adore nano. It's my favorite *NIX text editor. Here's a screenshot:
Running the nano editor under Linux under Windows. What a great world we live in!
Add some text and then press CTRL+X, y, and ENTER to save and close the file.
You can browse the Ubuntu subsystem's native file system by browsing to the following path:
To exit the Bash environment and return to your PowerShell console session, simply type exit. That was easy!
Parting thoughts ^
To remove the Bash environment, run the following PowerShell one-liner:
Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -Remove
You may be reading this and thinking, "Okay Tim, I get it—you can now run native Linux software on Windows. But why not simply use client-Hyper-V or another hypervisor and install a full Ubuntu virtual machine (VM)?"
Fair enough. According to Microsoft, WSL is a much lighter, more agile approach than using a full VM. In my testing, I've found that WSL uses about 50MB of RAM by default and negligible CPU and network.
In conclusion, I couldn't be happier that we have, for example, the ability to run tools like ssh instead of relying upon the clunky PuTTY. If you examine the Windows Optional Features dialog box, you'll see the Windows Subsystem for Linux feature listed as beta software.
Subscribe to 4sysops newsletter!
Admittedly, it's weird seeing beta software natively available on a release to manufacturing (RTM) operating system. Just keep the feature's status in mind as you test and work in your development and production environments.