wsl2

Start privilege Linux daemon when Windows boots without user login

Windows 10 WSL

With WSL, you can install many Linux distros in Windows, e.g. officially Ubuntu, Ubuntu1804, Debian, … and unofficial like Archlinux.  To start a backgroud linux daemon when Windows start, you need

  1. Recent version of  Windows 10 (e.g. 1803, or maybe 1709 but not tested)
  2. Enable Windows subsystem for Linux
  3. Install a linux distro from Windows store (e.g. Ubuntu1804)

To test, create a simple script in your home folder, e.g. /home/username/testscript.sh


#! /bin/bash
while [ 1 ]
do
 echo `date` >> /mnt/c/tmp/test.txt
 sleep 10
done

Now at Windows schedule task, create a new task, with

command=bash

argument = -c /home/username/testscript.sh

Modify this task to “Run whether user is logged on or not”.

wsl-roottask_wintask1

wsl-roottask_wintask2

Here is the output

wsl-roottask

Reboot your windows machine without user login to see if the task is started or not.

Run Linux system daemon in Windows

Warning: You should understand completely before doing the following because it allows normal user to run as root.

You can start any task, even “cron” can be started without problem.  But if you want to start cron, you need a special trick, setuid.  This is because normal user cannot start privilege daemon like /usr/sbin/cron.

The command to start ‘cron’ in Windows task scheduler is similar,


bash -c /etc/init.d/cron start

But before you do it, you must setuid of the task, /usr/sbin/cron (login as root, chmod u+s /usr/sbin/cron).  You may ask some Linux guys on how to limit which user(s) can run the ‘cron’ daemon.

Alternatively, you can modify the sudo configuration, e.g. /etc/sudoers.d and add the default user there so that the default user don’t need password to sudo the cron daemon.

I create the following cron task


* * * * * /bin/bash -c "echo hello from cron" >> /mnt/c/tmp/test.txt

You already notice the tail output screen above consists of this cron task.

 

You can also start the ssh server.  Points to  notice are:

  1. Port, if you also enable Windows 10 openssh server, you should change the Ubuntu’s sshd port to another one.
  2. The command to start is (assuming you can sudo without password):

bash -c "sudo service ssh start"

 

Update 2021-06-18
I try the above on wsl2 with Windows 10, 1909 (details below). It works without problem

Summary (assume windows 10 Pro, 1909 or above, wsl2 enabled):

  1. Execute wsl –set-default-version 2
  2. Install Ubuntu-20.04 (or any distro you like)
  3. Inside the distro, sudo apt install docker.io (you may use the official way to install docker.  But this is the simplier way to install docker)
  4. sudo nohup dockerd & (because wsl2 Ubuntu doesn’t come with systemd, so need to start the executable directly)
  5. Add the default user to the docker group by editing the group file, sudo vi /etc/group
  6. sudo nohup /usr/bin/dockerd  #may add additional options you like, e.g. -H fd:// –containerd=/run/containerd/containerd.sock
  7. Check docker daemon is running or not by “docker ps” or “docker run hello-world”
  8. add a new file, /etc/sudoers.d/username, with entry, username ALL=(ALL) NOPASSWD: /usr/bin/dockerd
  9. kill -s HUP $dockerd_pid  #kill the dockerd in previous step
  10. At windows, try to execute dockerd: wsl.exe -d Ubuntu-20.04 sudo /usr/bin/dockerd 
  11. If everything is ok, kill the dockerd daemon
  12. Add new schedule task, “Run whether user is logon or not”, “do not store password”, Trigger: At system start; Action: wsl -d Ubuntu-20.04 sudo /usr/bin/dockerd
  13. Run the scheduled task created in last step