Running SSH daemon as a background task on WSL

This article is about installing Ubuntu 18.04 in the Windows Subsystem for Linux (WSL) on Windows 10 and running the SSH daemon as a background task that will persist between terminal sessions.

Normally when a WSL terminal is closed all processes inside it stop. This is different than for example Cygwin’s sshd which runs as a Windows service independent of any particular terminal session.

Using the tmux command in WSL we can create a background task which we can detach the terminal from and reattach to later. This can be used to create a background task for sshd running inside an Ubuntu container, making it more like a persistent service.

Below are complete instructions for setting up sshd in Ubuntu 18.04 “Bionic Beaver” under WSL in Windows 10 and running an sshd background task.

 

Set up Windows Subsystem for Linux and install Ubuntu 18.04

1. Control Panel -> Programs and Features -> Turn Windows Features On or Off -> select Windows Subsystem for Linux

2. Start Menu -> Microsoft Store -> search for “Ubuntu 18.04” and install it

 

Install and configure ConEmu

1. Download and install ConEmu Preview release. I recommend the installer package. During setup select to install the 64-bit version.

2. Look at ConEmu Settings -> Startup. Under Predefined Tasks there should be a default task named Bash::bash. By default it should launch the Ubuntu 18.04 container. However if you have installed more than one container you need to supply the GID of the container as part of the command in ConEmu:

The GID is located in the Windows registry under HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss. See the image below:

The above image shows the Ubuntu 18.04 container’s GID which is the name of the registry node of the container. In this case it is 5fc6c426-c695-408b-9611-358962bcd21f

In ConEmu Settings -> Startup -> Tasks edit the entry for Bash::bash and change the command to the following, using the correct GID for your Ubuntu 18.04 container:

set “PATH=%ConEmuBaseDirShort%\wsl;%PATH%” & %ConEmuBaseDirShort%\conemu-cyg-64.exe –wsl –distro-guid={5fc6c426-c695-408b-9611-358962bcd21f} -cur_console:pm:/mnt

 

Run the Ubuntu 18.04 container and configure SSHD

1. Now try to launch the Ubuntu 18.04 container by selecting the Bash::bash task from ConEmu.  Click on the little arrow next to the green plus icon on the tab bar to see the drop down list of available console tasks and select Bash::bash.

You can verify that you’re inside the Ubuntu 18.04 container by running lsb_release -a

2. In your container become root:
sudo -i

3. Uninstall and reinstall openssh-server. This needs to be done because the default installation doesn’t generate the host keys that go in /etc/ssh
apt remove openssh-server
apt install ssh openssh-server

4. Edit /etc/ssh/sshd_config and uncomment the line:
PasswordAuthentication yes
Restart sshd:
service ssh restart

Note that if you are running another sshd such as that from Cygwin, the above command will fail because port 22 will already be in use.

 

Test SSHD and set up pre-shared key auth

You can now try to ssh in to your machine from another machine.

You can set up pre-shared key authentication. On the remote machine generate a private/public key pair:
ssh-keygen -t ed25519
Accept the default values for all prompts and leave the password blank (don’t worry this is not a logon password).
Copy the public key to your Ubuntu 18.04 user’s list of authorized keys. Still on the remote machine run:
ssh-copy-id -i .ssh/id_ed25519.pub user@host
use the correct values for user and host above.
At this point you should be able to do ssh user@host on the remote machine to login in to the Ubuntu 18.04 container without a password.

 

Set up background SSHD task using tmux

Normally when you close the console window for a WSL container all processes will also stop. To be able to have sshd continue running in the background you use the tmux command. If you want to start running daemons in the background in WSL you should think like a systems administrator. Daemons are normally started through systemd. There isn’t a system startup init system in WSL that is integrated with Windows (yet), but Ubuntu inside WSL does use systemd and you can look at the unit file for sshd to see how systemd invokes the daemon. The unit file is symlinked inside /etc/systemd/system/
You can see there that the unit file sources any config options in /etc/default/ssh In our simple case we don’t have any options defined there and we can just run /usr/sbin/sshd using the -D switch to prevent the daemon from detaching.

Shut down the sshd service since we’re going to run it in a tmux session:
sudo service ssh stop
Open a new Ubuntu 18.04 terminal and create a new tmux session for sshd:
tmux new -s sshd
Now in that same terminal invoke sshd with the -D option:
sudo /usr/sbin/sshd -D
Now you can close the terminal window and you will notice that sshd is still running!

Note: If you get the following error:
Missing privilege separation directory: /run/sshd
You can just manually create the /run/sshd directory

In another terminal you can list the background tasks running in tmux:
tmux ls
You can reattach to the sshd task:
tmux attach -t sshd

To stop the daemon just type Ctrl-c

 

Observations/Conclusions

WSL is really cool but I find that using tmux to background sshd does not work well. When the terminal window in which the tmux session is running is closed I frequently experienced that the session would end.  It appears that a second terminal session needs to be open before closing the first, but even trying this I still experienced crashes of the background tmux session.

At this point tmux is kind of like a toy but is not close to being reliable for running a daemon like ssh. Hopefully Microsoft will continue to improve WSL and create an init framework that integrates fully with Windows.

I also experienced a glitch while upgrading packages in Ubuntu in WSL.  I was able to work around the issue but it shows that WSL is not really stable yet and still may have some glitches.

If WSL eventually reaches the point where it can replace Cygwin, able to stably run daemons like MariaDB, Apache, and SSHD it will be amazing.

 

References/Credit:
blogs.msdn.microsoft.com: Background Task Support in WSL

conemu.github.io: Bash on Windows