Announcement

How To Enable Windows Subsystem for Linux on Windows Server 2016


This guide help you become a Windows Insider, download Windows Server, and get Linux running on the Windows Subsystem for Linux.







Prerequisites

Windows Server 2016 (build 16215+). Right now, that means a Windows Insider's build.

Join Windows Insiders and Download Windows Server

  1. Join a Windows Insiders program:
  2. Download Windows Server 2016 - Insider Preview.
    Make sure you're logged in, images are only available to Windows Insiders.


Set up Windows Server

Now that you have a Windows Server image (.iso), install Windows Server on a physical computer or a virtual machine. I'm going to use a virtual machine on Hyper-V.

During installation, Windows Server will ask for a product key. The following keys are available for unlimited activation of Windows Server (pre-release builds only). These keys may be used throughout the pre-release cycle. Read more here.
  • Server Datacenter Core: B69WH-PRNHK-BXVK3-P9XF7-XD84W
  • Server Standard Core: V6N4W-86M3X-J77X3-JF6XW-D9PRV
When you're done, you should see something like this:

Check your build number

To find your Windows Server build number, run the following in PowerShell:
systeminfo | Select-String "^OS Name","^OS Version"
You can also confirm that your build has the Windows Subsystem for Linux by running the following in PowerShell:
PowerShell
Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Enable the Windows Subsystem for Linux

Enable the "Windows Subsystem for Linux" optional feature and reboot.
  1. Open PowerShell as Administrator and run:
    PowerShell
    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
    
  2. Restart your computer when prompted.

Install a Linux distribution

  1. Download the appx for your favorite Linux distribution.
    Here are links directly to the apps available through the store:
    • Ubuntu
    • OpenSUSE
    • You can download the distributions to Windows Server with Invoke-WebRequest cmdlet. Here's a sample instruction to download OpenSUSE.
      PowerShell
      Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1604 -OutFile ~/Ubuntu.zip -UseBasicParsing
      
      Tip: If the download is taking a long time, turn off the progress bar by setting $ProgressPreference = 'SilentlyContinue'
  2. Unzip the file
    Expand-Archive ~/Ubuntu.zip ~/Ubuntu
    
    Make sure your target directory (~/Ubuntu in this example) is on your system drive. Usually this is your C: drive.
    Example: C:\Distros\Ubuntu
    The contents should look like this:

3. Run the installer, named .exe
For example: ubuntu.exefedora.exe, etc.
Troubleshooting: Installation failed with error 0x8007007e
This error occurs when your system doesn't support Linux from the store. Make sure that:
4. Create a UNIX user

The first time you install the Windows Subsystem for Linux, you will be prompted to create a UNIX username and password.
This UNIX username and password can be different from, and has no relationship to, your Windows username and password. 

5. Run distro's preferred update/upgrade.
sudo apt-get update
sudo apt-get upgrade
You're done! Go use your new Linux environment!







Windows Interoperability

Starting in Windows 10 Anniversary Update, the Windows Subsystem for Linux can invoke native Windows binaries from the Linux console and Linux binaries from a Windows console.

This interoperability functionality delivers a seamless experience between Windows and WSL.

Invoking WSL from the Windows Command Line

Linux binaries can be invoked from the Windows Command Prompt or from PowerShell. Binaries invoked in this way have the following properties:
  1. Use the same working directory as the CMD or PowerShell prompt.
  2. Run as the WSL default user.
  3. Have the same Windows administrative rights as the calling process and terminal.
Example:
C:\temp>bash -c "ls -la"
total 916
drwxrwxrwx 2 root root      0 Sep 28 08:45 .
drwxrwxrwx 2 root root      0 Sep 27 07:33 ..
-rwxrwxrwx 1 root root     14 Sep 27 14:26 foo.bat
Linux commands called in this way are handled like any other Windows application. Things such as input, piping, and file redirection work as expected.
Examples:
C:\temp>bash -c "sudo apt-get update"
[sudo] password for username:
Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB]
C:\temp>bash -c "ls -la" | findstr foo
-rwxrwxrwx 1 root root     14 Sep 27 14:26 foo.bat
C:\temp>dir | bash -c "grep foo"
09/27/2016  02:26 PM                14 foo.bat
C:\temp>bash -c "ls -la" > out.txt
The WSL commands passed into bash -c are forwarded to the WSL process without modification. File paths must be specified in the WSL format and care must be taken to escape relevant characters. Example:
C:\temp>bash -c "ls -la /proc/cpuinfo"
-r--r--r-- 1 root root 0 Sep 28 11:28 /proc/cpuinfo

C:\temp>bash -c "ls -la \"/mnt/c/Program Files\"”
<- -="" c:="" contents="" files="" of="" rogram="">

Invoking Windows binaries from WSL

The Windows Subsystem for Linux can invoke Windows binaries directly from the WSL command line. Applications run this way have the following properties:
  1. Retain the working directory as the WSL command prompt except in the scenario explained below.
  2. Have the same permission rights as the bash.exe process.
  3. Run as the active Windows user.
  4. Appear in the Windows Task Manager as if directly executed from the CMD prompt.
Example:
$/mnt/c/Windows/System32/notepad.exe
In WSL, these executables are handled similar to native Linux executables. This means adding directories to the Linux path and piping between commands works as expected. Examples:
$ export PATH=$PATH:/mnt/c/Windows/System32
$ notepad.exe
$ ipconfig.exe | grep IPv4 | cut -d: -f2
$ ls -la | findstr.exe foo.txt
$ cmd.exe /c dir
The Windows binary must include the file extension, match the file case, and be executable. Non-executables including batch scripts and command like dir can be run with /mnt/c/Windows/System32/cmd.exe /C command. Examples:
$ /mnt/c/Windows/System32/cmd.exe /C dir
$ /mnt/c/Windows/System32/PING.EXE www.microsoft.com
Parameters are passed to the Windows binary unmodified.
As an example, the following commands will open C:\temp\foo.txt in notepad.exe:
$notepad.exe “C:\temp\foo.txt”
$notepad.exe C:\\temp\\foo.txt
Modifying files located on VolFs (files not under /mnt/) with a Windows application is not supported. By default, WSL attempts to keep the working directory of the Windows binary as the current WSL directory, but will fall back on the instance creation directory if the working directory is on VolFs.
As an example; bash.exe is initially launched from C:\temp and the current WSL directory is changed to the user’s home. When notepad.exe is called from the user’s home directory, WSL automatically reverts to C:\temp as the notepad.exe working directory:
C:\temp>bash
/mnt/c/temp/$ cd ~
~$ notepad.exe foo.txt
~$ ls | grep foo.txt
~$ exit
exit
C:\temp>dir | findstr foo.txt
09/27/2016  02:15 PM                14 foo.txt
Users may disable the ability to run Windows binaries for a single WLS session by running the following command as root:
$ echo 0 > /proc/sys/fs/binfmt_misc/WSLInterop
To reenable Windows binaries either exit all WSL sessions and re-run bash.exe or run the following command as root:
$ echo 1 > /proc/sys/fs/binfmt_misc/WSLInterop
Note that disabling interop will not persist between WSL sessions.