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 to do that.
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, still people prefer to use WSL 1 instead of WSL 2. Well, in this tutorial, I am not going to talk about which version of Windows Subsystem is better, instead will show you the steps, in case any one of you want to start some WSL running services automatically with Windows 10 startup or after boot.
Steps to automatically start WSL services with Windows 10 boot up
It doesn’t matter are you using Ubuntu 20.04/18.04/16.04 LTS WSL Linux Distros including OpenSuse, Kali Linux, CentOS the method given here will apply on all of them, yes, slight changes could be there.
Set Default WSL Distro
Right-click on 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 done move towards the next step.
Prevent WSL services from asking 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 some 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 are using, here switch to the Sudoers directory.
Here you will find a file, edit that:
sudo nano 99-snapd.conf
Add the following lines in it.
%sudo ALL=(ALL) NOPASSWD: /usr/sbin/service mysql * %sudo ALL=(ALL) NOPASSWD: /usr/sbin/service apache2 *
Here we are adding two service MySQL and Apache, however you can add whatever service you want to autorun with Windows 10 startup.
Save the file by using: Ctrl+X and type the Y and then hit the Enter key.
Create a bat file to start WSL services in the Windows startup folder
Open the Windows startup folder. For that, simply press Windows Key + R to open the Run box and then 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.