Start WSL services automatically on Ubuntu with Windows 11 or 10 startup

Do you want to start various services such as Nginx, Apache2, and MySQL automatically on WSL- Windows Subsystem for Linux running with Ubuntu 20.04/18.04 on Windows 10?  Then, here is the way.

Microsoft, to some extent, has solved the problem of developers and users who quickly want to use Linux on Windows 10 without using any Virtual Machine platforms such as VMware Player and Oracle VirtualBox. However, there are still so many limitations with WSL. In WSL 1, as it has not been booted with init, thus so many commands are not possible to use. Moreover, WSL 2 is around here but with lots of bugs.

Therefore, people still prefer to use WSL 1 instead of WSL 2. Well, in this tutorial, I will not talk about which version of Windows Subsystem is better; instead, I will show you the steps in case any one of you wants to start some WSL running services automatically with Windows 10 startup or after boot.

Automatically start WSL services with Windows 11 or 10 boot up

Whether you use Ubuntu 20.04/18.04/16.04 LTS WSL Linux Distros, including OpenSuse, Kali Linux, and CentOS, doesn’t matter. The method given here will apply to all of them; slight changes could be there.

Set Default WSL Distro

Right-click the Windows 10 Start button and select “Windows PowerShell (Admin).” After that, type the below command to find out all the installed WSL Linux distros on your PC or laptop.

wsl -l

Now, see check out the Distro name you are using. For example, here, we are using Ubuntu 20.04. Thus the command will be like this:

wsl –set-default <distro-name>

wsl --set-default Ubuntu-20.04

Once performed, move towards the next step.

Prevent WSL services from asking for user password

As we know, the Linux services run with the permission of root or accounts that are part of sudo groups only unless you have assigned that particular user. Thus, to make sure whenever the WSL services run automatically, it should not ask for a password; otherwise, it would not be able to run.

Therefore, open Ubuntu 20.04 or whatever WSL version you use here and switch to the Sudoers directory.

cd /etc/sudoers.d

Here you will find a file, edit that:

sudo nano 99-snapd.conf

Add the following lines to it.

%sudo ALL=(ALL) NOPASSWD: /usr/sbin/service mysql *
%sudo ALL=(ALL) NOPASSWD: /usr/sbin/service apache2 *

Here we are adding two services, MySQL and Apache; however, you can add whatever service you want to autorun with Windows 10 startup.

Save the file by using Ctrl+X, type the Y, and then hit Enter.

ubuntu2004 XsC4QUQPvv

Create a bat file to start WSL services in the Windows startup folder

Open the Windows startup folder. For that, press Windows Key + R to open the Run box and type shell:startup and press the Enter key.

Inside the folder right and create a text file. Name it WSL startup or whatever you want.

Now, Open it and type:

wsl sudo service mysql start
wsl sudo service apache2 start

Click on File->Save it as a .bat extension file.

Note: Delete the other text file with the same name.

Create WSL auto start services on windows 10

That’s it. From now whenever you start your Windows 10, the services running on WSL Distros will automatically get started.

 

3 thoughts on “Start WSL services automatically on Ubuntu with Windows 11 or 10 startup”

  1. The .bat file will only run AFTER you logon. The services will not start automatically until you logon as the .bat file will only run afterwards.

    It would be better to open GPEDIT.MSC and add the script to the ‘Computer Configuration\Windows Settings\Scripts\Startup’ policy.

    Reply
  2. Any file you want to be appended to the sudoers config should not end with a ~ or contain a . so 99-snapd.conf will not be appended, also it’s better to use visudo to edit the sudoers config, this is what worked for me.
    sudo visudo -f /etc/sudoers.d/wsl_start_up

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.