How to properly start WSL2 services on Windows startup?


menuhin

New member
Local time
12:24 PM
Posts
2
OS
Windows 11, WSL2 Debian
Scenario: I want to start some services that is provided by the installed distribution on WSL2 (Debian), e.g. sshd etc.

Solution 1:
  1. Set up sshd on distro of choice. Ensure it works (firewall set up properly, etc.) when launched manually
  2. Ensure that the distro which will run sshd is the default distro (use wslconfig)
  3. Create a task using Task Scheduler (taskschd.msc) that has the action "Start a program", with the program set to wsl with optional arguments -u root service ssh start
  4. Set the task to run on computer startup. Specify a user account to run the task as, and specify that the task run whether or not the user is logged in

Solution 2:
  1. On the WSL put to the end of the /etc/sudoers file the following line:
    %sudo ALL=NOPASSWD: /etc/init.d/ssh start
  2. Put to the shell:Startup folder bat file with the content:
    powershell.exe "& 'C:\Windows\System32\bash.exe' -c 'sudo /etc/init.d/ssh start'"

Question 1: In either case, WSL is not called to start up (solution 1 calls wsl.exe to run a command and solution 2 calls bash.exe to run a file within the WSL installation) - why these both works without directly calling WSL to start first?

Question 2: how about the normal services under /etc/init.d/ - will these services automatically run as long as WSL is started up, even not manually? And if no, why?
 

My Computer

System One

  • OS
    Windows 11, WSL2 Debian
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkPad X220 Tablet
    CPU
    Intel Core i7-2640M CPU @ 2.80GHz 2.80 GHz
    Memory
    8 GB
    Monitor(s) Displays
    (external) Monitor Lenovo LT2452P, 24" IPS LED, 1920 x 1200
    Screen Resolution
    1366 × 768

menuhin

New member
Thread Starter
Local time
12:24 PM
Posts
2
OS
Windows 11, WSL2 Debian
Similar to stack-overflow, I try replying to my questions after I found out the answers.

Question 1: In either case, WSL is not called to start up (solution 1 calls wsl.exe to run a command and solution 2 calls bash.exe to run a file within the WSL installation) - why these both works without directly calling WSL to start first?

It is not necessary to have a running wsl.exe for the services of a distro installed on WSL to start and run. These WSL services, will be run from within the processes of wslhost.exe
Solution 1: Pros - scheduled and conditioned, and the start of process is hidden, no sudo needed*; Cons - it's hidden so it's not immediately obvious if something doesn't work
Solution 2: Pros - simpler**, and clear if it works or doesn't; Cons - the bat process is not hidden

* it's possible to start a process as users other than root, e.g. you default user
** processed called by bashexe generally will run at a system or root level, but now bash.exe is deprecated, so one can use wsl.exe instead, such as (in a bat file if one tries to avoid vbs like in Solution 1):
Code:
powershell.exe "& C:\Windows\System32\wsl.exe -u root service ssh start"


Question 2: how about the normal services under /etc/init.d/ - will these services automatically run as long as WSL is started up, even not manually? And if no, why?

This doesn't work for me, i.e. starting wsl.exe and those auto startup processes (e.g. under /etc/init.d) do not start up like in a real linux installation. And what I've learnt is that calling wsl.exe doesn't (at least for now) start up a linux system like how one start up linux on a real machine or a virtual machine. So those processes won't auto-run.
 

My Computer

System One

  • OS
    Windows 11, WSL2 Debian
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkPad X220 Tablet
    CPU
    Intel Core i7-2640M CPU @ 2.80GHz 2.80 GHz
    Memory
    8 GB
    Monitor(s) Displays
    (external) Monitor Lenovo LT2452P, 24" IPS LED, 1920 x 1200
    Screen Resolution
    1366 × 768
Top Bottom