Install Windows Subsystem for Linux on Windows 10

From JMK Wiki
Jump to navigation Jump to search

Installing a GNU userland

Enabling Windows Subsystem for Linux

enable WSL

To be able to run a GNU userland on windows, the Windows Subsystem for Linux (WSL) needs to be enabled. This can be achieved by opening Turn Windows features on and off from the Start menu and enable the Windows Subsystem for Linux feature.

This requires windows to be restarted in order to be able to continue.

Installing a GNU userland

To install a GNU userland open the Microsoft Store from the Start menu and type Linux into the search box. Click on show all and browse the available Linux distributions. Click on the desired (preferably free) distribution and click Get.

Note: this guide will use Debian GNU/Linux as its selected distribution. Other distributions may have different install procedures or package prerequisites in the following chapters.

Hint: it is not necessary to log into the store in order to download and install the selected distribution.

Setting up the GNU userland

Initial setup of the GNU userland

Open the Debian GNU/Linux app from the start menu and follow the instructions:

  • wait for the initial installation to finish
  • provide a username
  • enter and confirm a password for the user

Setting up a root password (optional)

To be able to switch to the root user instead of using sudo a root password needs to be set. For this switch to the root user using sudo and set type passwd:

user@localhost ~ $ sudo su -

Note: this is a user propmt - these commands should be executed as an unprivileged user.

root@localhost ~ # passwd
root@localhost ~ # exit

Note: this is a superuser propmt - these commands should be executed as the root user - this means that either sudo needs to be used in front of the command or the user must switch to the root user using su.

After the root password has been set up one can switch to the root user by simply typing su and providing the root password.

user@localhost ~ $ su -
root@localhost ~ # id

Unfortunately the sudoers package cannot be removed because it is necessary to start WSL services on Windows boot.

Update the system

Update the list of available packages:

root@localhost ~ # apt update

List all upgradable packages:

root@localhost ~ # apt list --upgradable

Upgrade all upgradable packages:

root@localhost ~ # apt upgrade

Update the time and date settings

root@localhost ~ # dpkg-reconfigure tzdata

Install some useful tools (optional)

root@localhost ~ # apt install screen mc dstat openssh-server bash-completion dos2unix
  • screen: terminal multiplexer with VT100/ANSI terminal emulation
  • mc: Midnight Commander - a powerful file manager
  • dstat: versatile resource statistics tool
  • openssh-server: secure shell (SSH) server, for secure access from remote machines
  • bash-completion: programmable completion for the bash shell
  • dos2unix: convert text file line endings between CRLF and LF

Configure services (optional)

System logger

Start rsyslogd:

root@localhost ~ # /etc/init.d/rsyslog start

OpenSSH server

Harden OpenSSH server configuration:

root@localhost ~ # nano -w /etc/ssh/sshd_config

Add the following line at the end of the file (with your username instead of <USERNAME>):

AllowUsers <USERNAME>

Start the OpenSSH server:

root@localhost ~ # /etc/init.d/ssh start

Job scheduler

Allow cron log:

root@localhost ~ # nano -w /etc/rsyslog.conf

Find the following line and uncomment it (remove the leading # character):

#cron.*                         /var/log/cron.log

Save the file and exit the editor (<F2> => <y> => <Enter>).

Restart the system logger:

root@localhost ~ # /etc/init.d/rsyslog restart

Start the job scheduler

root@localhost ~ # /etc/init.d/cron start

Enable services at windows boot (optional)

In order to be able to start WSL services at Windows boot time, the desired service's init script need to be added to the sudoers configuration, so it can be started from Windows without the need to provide a password.

Note: background processes require windows build 1803 or later.

  • screen-cleanup: cleans up stale sessions and initializes the session directory for the sessions
  • rsyslog: system logger
  • ssh: OpenSSH server
  • cron: time-based job scheduler
root@localhost ~ # echo -e "%sudo\tALL=NOPASSWD: /etc/init.d/screen-cleanup" > /etc/sudoers.d/startup
root@localhost ~ # echo -e "%sudo\tALL=NOPASSWD: /etc/init.d/rsyslog" >> /etc/sudoers.d/startup
root@localhost ~ # echo -e "%sudo\tALL=NOPASSWD: /etc/init.d/ssh" >> /etc/sudoers.d/startup
root@localhost ~ # echo -e "%sudo\tALL=NOPASSWD: /etc/init.d/cron" >> /etc/sudoers.d/startup
root@localhost ~ # chmod 440 /etc/sudoers.d/startup

Save the following as wls.reg and run it to add the registry keys to the current user's startup tasks:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]
"wsl - cron"="debian -c \"sudo /etc/init.d/cron start\""
"wsl - screen-cleanup"="debian -c \"sudo /etc/init.d/screen-cleanup start\""
"wsl - rsyslog"="debian -c \"sudo /etc/init.d/rsyslog start\""
"wsl - ssh"="debian -c \"sudo /etc/init.d/ssh start\""

Install an X server (optional)

WSL can run graphical applications, but in order to be able to display them an X server needs to be installed on the windows system.

Available X servers are:

Download, install and start one of the above servers. Upon starting Xming or VcxSrv set the display number to 0.

Set the display in WSL:

user@localhost ~ $ echo "export DISPLAY=:0" >> ~/.bashrc
user@localhost ~ $ source ~/.bashrc

Install a graphical application to test the X server:

root@localhost ~ # apt install glxgears

Start glxgears:

user@localhost ~ $ glxgears

Continue with Installing Java Development Kit on Windows Subsystem for Linux