I'll show you a way around this!
(Don't forget to read the notes on security below!)
- Start a new WSL session as you usually would.
sudo EDITOR=nano visudoand hit enter
- Use your arrow keys to scroll to the end of the file
- Append the following
username ALL=(ALL) NOPASSWD:ALLto the end of the file. Of course, replacing “username” with your user name.
- Hit CTRL + X and if asked to save, hit Y to confirm. When asked for the file name, leave the default and just hit enter.
- If you didn’t do any typos, that’s it! No more annoying password prompt inside your WSL session.
Update 2021-03-27: One-liner
I just wrote a quick one-liner for you that works in Ubuntu with bash, and possibly more. Just make sure you do *not* precede the “echo” with sudo or else ‘whoami’ will output root instead of your username:
echo "`whoami` ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/`whoami` && sudo chmod 0440 /etc/sudoers.d/`whoami`
This should work on all systems that use the /etc/sudoers.d/ directory to include custom configs. If yours does not, use the manual setup above.
A note on security
This is the same way it’s done on a real Linux installation, and I would of course never recommend doing this on a real Linux installation for your user account.
But because in this case, this is contained within the WSL shell, I’d say it’s “okay” as long as you don’t run untrusted scripts or added dodgy repos to your WSL container. Because WSL can access all your files on your Windows side!
You should know, that this means that any script or application you run inside WSL as your user, which requests sudo, will be granted sudo privilege without password prompt. Follow this guide only if you know what you are doing.