
Bash Shell under Ubuntu on Windows
Explanation: Windows Subsystem for Linux
With the introduction of the Bash Shell under Windows 10 in the course of the Anniversary Update as a so-called "Windows Subsystem for Linux" (WSL) and the simultaneous announcement to transfer the PowerShell into an open source license model, Microsoft fulfilled a long-cherished dream of many professionals. Among other things, this meant that most of the tools and utilities that are of immense importance to software developers and system administrators were suddenly no longer closed proprietary, but open and cross-platform available. More importantly, with WSL, Microsoft was addressing one of the biggest paradigms in the IT landscape: while almost the entire infrastructure of the Internet runs on Linux, Windows dominates the enterprise market. Web developers have therefore long struggled with the constant jumps between platforms, and not infrequently had to adopt seemingly contradictory workflows. And it is precisely this problem that WSL is designed to address. Because by being able to run ELF64 Ubuntu binaries directly natively under Windows within a bash shell, Microsoft and Canonical – the company behind Ubuntu – allow developers to use familiar and widely used tools such as apt, ssh, grep, awk, curl, mysql, ruby, perl and many more directly under Windows. But it’s not just the availability of the programs themselves that makes WSL so enticing; the availability of a wealth of literature, expertise and developers is also worth its weight in gold.
How it differs from Cygwin?
For those who are deeper into the topic, Cygwin – a tool that provides POSIX API compatibility on Windows – should be familiar. However, for applications to run in it, they have to be recompiled. Until the announcement of WSL, this was actually the most popular way to use Linux applications under Windows. Unlike Cygwin, however, there is no need to recompile the software for use with WSL – the binaries can be run directly.
It is better than Linux as a virtual machine?
In any case. WSL is neither a virtual machine (VM) nor an emulator. Strictly speaking there is not even a Linux kernel behind it anymore. It is an abstraction layer that converts Linux syscalls to Windows syscalls. To quote Dustin Kirkland of Canonical: Ubuntu on Windows is "bit for bit and checksum for checksum the ability to run Ubuntu ELF binaries directly on Windows". The closest it actually comes to something like reverse Wine .
What advantages it offers?
Although WSL was designed and conceived especially for software developers, end users also benefit from it. Even on Windows 10 Home, WSL is available, so it can actually be used by all Windows 10 users. WSL is ideal for getting a taste of Linux without having to directly install a full distribution in a virtual machine or separate partition. In addition, Linux has a large number of extremely interesting free applications for education, academic and research use. With each new Windows version, WSL’s compatibility with existing Linux software continues to increase.
There are limitations?
Yes, of course there are. For one, WSL requires a 64-bit processor and does not run on 32-bit systems. Also, the Linux applications that can be installed and run within the Bash shell are 64-bit binaries. WSL is primarily intended as a command line tool for professionals, so there is no official support for graphical user interfaces or multimedia applications. Other limitations:
- WSL has no access to the GPU. In concrete terms, this means that neither CUDA nor OpenCL frameworks are supported. But there is the possibility to run applications with graphical user interface or multimedia applications by tunneling the graphics output via network and TCP/IP from Linux to Windows. How well this works, however, always depends on the individual case.
- I/O accesses are quite slow compared to a Linux VM.
- Since this is not a real Linux kernel, applications that require direct access to the Linux kernel cannot run. This applies for example to hypervisors like VirtualBox. Also, not all Linux syscalls are fully compiled and transitioned at this time.
- A server/background service cannot be started from bash on Windows.
- The selection of available Linux distributions in the Windows Store is small, but the community has of course found ways and means to make pretty much any flavor of Linux work under WSL ( Arch Linux please?)
After this basic course in WSL, it’s time to move on to the practical part.
1. Step: Activate Developer Mode and WSL
Before we dare to use cowsay in the terminal, a few basic settings and installations have to be done first. First of all you have to configure in the settings under Update and Security and for developers the Developer mode be activated. If it has never been active before. it may take a short moment to complete this step. Then we need the function Enable or disable Windows features, which may require administrator rights, and further down in the list tick the Windows Subsystem for Linux to. Afterwards a reboot follows to apply the changes.

Enable developer mode in the settings

Make sure that the Windows Subsystem for Linux is also installed
2. Step: Install Ubuntu
The shell can then be started by entering bash be started in the search box to initiate the installation of Ubuntu. How exactly this is done depends on the version of Windows installed. For versions older than 1709 (Fall Creators Update), just confirm the prompt in the window that opens with "y" (don’t forget: WSL is still marked as beta on Windows 10 older than 1709). In the Fall Creators Update, on the other hand, there are several distributions to choose from. Ubuntu, openSUSE Leap 42 or SUSE Linux Enterprise Server 42 can be installed via the Microsoft Store. Other distributions can also be persuaded to cooperate with WSL using simple hacks. For this tutorial we use Ubuntu 16.04 LTS.

Install Ubuntu on Windows (for Windows 10 versions older than 1709)

Selection of different Linux distributions in the Microsoft Store (for Windows 10 versions from 1709)
3. Step: Create a standard UNIX user
Once installed, Ubuntu prompts to create a standard UNIX user. This will be the user under which Ubuntu will come up and log in by default in the future. Of course there is also the possibility to create multiple users or to work directly as root. After entering a username and password (2x) the UNIX user is created. Under this user you can now sudo-execute commands. Furthermore, there is also a root user that has full access to everything; for everyday use, however, we advise against this for security reasons. Some installations log in as root by default – in these cases it is strongly advised to use adduser<username> manually create a new user. You can tell if you are root or not by the character that ends the command line: # stands for root, while normal users terminate with $.

Create default user

Create a normal user if the default user is root
4. Step: Update Ubuntu packages
After creating the user, it is helpful to first bring the installed packages up to date. Using sudo apt-get update the list of available applications and packages is updated, the actual installation is then done with sudo apt-get upgrade. This process can take some time depending on the amount of files and the available bandwidth of the internet line. If this step is skipped, there may be problems installing additional software from the Ubuntu repositories in some circumstances. Also, it is certainly useful to regularly check for updates. With the help of sudo apt-get dist-upgrade the kernel can be updated, but in this case this is rather uncritical.
Update the Ubuntu packages
5. Step: Let’s go!
Now that Ubuntu is installed and updated, the various bash commands can be tried and experimented with such as z. B. ls (show contents of a directory) or pwd (show current directory). Also applications like vi – a standard text editor on Linux – can be started by entering vim.
Tip: Bash commands can also be executed directly from the command prompt window. For example, to open vi directly from Windows, enter the following in the input box bash -c "vi" a. This will open a new window with a bash console that directly launches the editor vi.
In the best Linux manner, external directories can also be mounted. With WSL, for example, Windows directories can be mounted and searched in Bash. So to access the contents of the already mounted drive C:, the commands cd /mnt/c and then ls, to display the content. If files need administrator rights for access, it is not sufficient to grant them via sudo. Instead, Bash must be started with administrator privileges analogous to starting the command prompt with administrator privileges.

vi-editor under Ubuntu on Windows

C drive mounted in Bash
6. Step: Create and execute bash scripts
The biggest advantage of a command line is the availability of scripts. Since WSL is primarily a developer environment, it supports scripts within the Bash shell. These can either be created under Windows and then executed via mounted folders and/or drives under Linux or, more sensibly, created directly under Linux. Since the line termination (EOL) is different under Linux than under Windows, there are clearly fewer problems when working directly under Linux. So let’s look at a simple Hello-World!-script we create with the help of the text editor nano. In the command line we first enter the command nano
/helloworld.sh a. This opens the text editor nano and creates the file helloworld.sh in our home directory, which under Linux is indicated by the character
is represented, created.
The following lines are then entered in the editor:
#!/bin/bash
# assigning a string to a variable
STRING="Hello World!"
# Output of the string on the screen
echo $STRING
With CTRL-O the file is saved, with CTRL-X the editor then exits.
A few short explanations about this script: Every script always starts with the so called shebang (#!), followed by the interpreter to use for the following commands. If you want to use another interpreter to execute the content of the script, you have to pass it including the complete path here. A # character indicates a comment. This line is not executed, it is only used to document and understand a program. Variables can be assigned values, in our example the variable is called STRING. The echo command then prints the contents of the STRING variable to the screen.
So it is high time to run the program we have just created. For security reasons, Bash does not allow direct execution of arbitrary files. For this to work, it must be explicitly marked as executable. To mark our script as executable, we need to change its attributes, which is done using the chmod program: chmod +x
/helloworld.sh adds the attribute x, which defines the executability, to the script we just created. Finally, to run the script, we need to call it with absolute or relative path:
/helloworld.sh or, if you are currently working in
is, ./helloworld.sh.

Nano editor to create a script

Hello-World!-Script in Nano

Change attributes and run script
7. Step: Graphic output in WSL
Originally WSL was only meant as a command line environment to connect the Linux developer toolchain to Windows. Nevertheless, there are ways and means to start graphical applications directly from Bash as well. Classically under Linux a so called X-Server takes care of the graphics output. Since WSL does not provide such an X server, we have to install it under Windows first and then tunnel through the graphics output. First we need an X server for Windows, z. B. Xming or VcXsrv . Xming is a bit older, but much smaller and more lightweight. Both are suitable for our purposes, for this tutorial we fall back on VcXsrv. After the installation, the X server can be started from the Start menu as usual under Windows and nests as X icon in the Windows taskbar after startup. As a test we start the program gvim under Linux – a vi editor with graphical user interface.
In the command line we first have to start the editor using sudo apt-get install vim-gtk install. Then we need to redirect the graphics output to use the previously installed X server as destination. This is done by entering the command export DISPLAY=:0. Finally, after entering gvim the graphical editor to appear on the screen. Congratulations, we have just executed an application with graphics output under WSL.
Annotation: The export-DISPLAY command is always valid only for the current session and must be re-entered each time Bash is started. It can also happen that individual programs have problems with the tunneled graphics output.

vim-gtk installation in Bash

gvim natively under WSL
If we try something more complex like running Ubuntu’s own package management tool for graphically installing software Synaptic. To do this, we first enter on the command line the command sudo apt-get install synaptic to install the software. It is then invoked by typing sudo sypantic. Once started, a graphical user interface is available for the installation of applications – how about Firefox?? Just type firefox in the search box and select Firefox browser in the result list. Since the browser will bring other dependencies after it, these will be displayed additionally and need to be confirmed. In the dialog window we finally start the installation, which initiates the download and the actual installation process in the background. Once completed, the Firefox browser can be started by typing firefox to be started on the command line.

Select Firefox in the Synaptic package manager

Firefox installation in Synaptic

Firefox in Bash under Ubuntu on Windows (puh . )
Conclusion
Of course what we’ve done and shown here so far just scratches the surface of WSL. There is a huge community behind Linux that has greedily jumped on the new possibilities of WSL on Windows 10. Finally, there is no longer the need to run multiple VMs to test software or even partition the hard disk to install Linux natively. WSL is an excellent way to get around all this and is especially helpful for those who want to get to know Linux without having to throw their familiar Windows environment overboard. Although some of the tricks shown here, such as running applications with graphics output, are not officially supported and can occasionally cause problems, workflows on the command line are fully mapped in WSL and are almost unbeatable. Also, WSL is constantly evolving with each new version of Windows 10, so we expect new features based on user requests in future iterations of Windows 10. For the upcoming Redstone 4 update, for example, the specifications include extended configuration options, better interaction between Windows and Linux applications, and case-sensitivity in file system operations.
One of the big advantages of WSL is the possibility to run not only graphical applications, but a complete desktop environment like XFCE, LXDE or even Unity. And now that Windows 10 also offers native support for virtual desktops, there’s nothing to stop you from having a Windows desktop on one virtual screen and a Linux desktop on a second virtual screen, which you can switch between with a keyboard shortcut. Desktop environments are also far from the end of the line – how about multimedia applications? We will explore this question in a separate article. For today, we’ll let it go and look forward to your comments and experiences.