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
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.