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.
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.
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.
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.
That’s it. From now whenever you start your Windows 10, the services running on WSL Distros will automatically get started.