(For people who like to hibernate/sleep rather than reboot.)
So after keeping you machine running for some days, weeks, or in unlikely cases maybe months without a reboot, you might notice that wsl has trouble retrieving packages from the web and maybe even other strange problems.
This might be related to “clock skew”, an issue where the clock in WSL (2) desynchronizes with the host clock.
There’s an easy fix which sadly requires you to run a sudo-ed script in wsl on startup.
So, let’s start by disabling the sudo password
And now we should open a new WSL 2 instance, this should locate us in our userprofile directory (user home).
Run following command
sudo nano .bashrc
Then add following line to the end of the file
sudo ntpdate ntp.ubuntu.com &>/dev/null
now press CTRL + O and exit the script
After the next WSL instance start, you should not have a clockskew problem anymore.
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.