a pleasant descent into madness

Tag: SUDO

[DEPRECATED] Automatically start docker service in WSL2

DEPRECATED, look at Start WSL 2 Service on Host startup instead.

I keep forgetting to start the docker service when opening up an instance of WSL(2)

Let’s fix that!

So what do we want to do?
We want to run the docker service from startup!
And that has to be done on sudo… but we don’t want to be reminded of that constantly
(in the form of a prompt to enter the sudo password on every WSL2 instance start).

First of all, sadly, we need to stop WSL from asking us for the sudo password.

Stop WSL2 from asking for sudo password

Next up, open up wsl and run

sudo service docker start > /dev/null 2>&1 &

This starts the service in the background while piping the output to null

As you opened a new WSL instance, this should be happening within your userprofile and opening up the .bashrc file.

Add following line to the end of the file:

sudo service docker start

Now WSL instance startup may take some seconds depending on how long it will take to start up docker in WSL.
Have fun!

Stop WSL2 from asking for sudo password

WSL2 normally is being used during development, ideally by a person that actually know what they’re doing, these people – and developers – sometimes have to run scripts on wsl startup or keep having to start services manually running sudo.

This can be time consuming or even hinderance at worst.
To circumvent this issue, you can stop WSL from asking you for the sudo password.

I know that this is not the best possible thing you can do security-wise, but i’ve been looking for a lot of possible ways to work around auto-sudoing and could not come up with a satisfactory solution, especially if you are the type that keeps hibernating the PC instead of rebooting (like me) so WSL never actually has to restart at all.

This imposes a security risk and enables scripts running on user-level to be executed with higher privilegues, please only do this if you actually know what you are doing.
Personally, as WSL is a development environment, i find the list pretty low, but that also depends on what kind of packages you download and what kind of data you handle on your machine, especially because WSL enables access to the windows file system!

Anyway, straight to the point:
To stop WSL from asking you for the SUDO password, open up a WSL (2) Shell and type

sudo visudo

And add the following line to the end of the file

YourUsername ALL=(ALL) NOPASSWD: ALL

Of course you will want to type in your username instead of “YourUsername”.
Then press Ctrl + O and exit the window.
After the next WSL startup you should be all set-up.

© 2024 Yavuz-Support Blog

Theme by Anders NorenUp ↑