a pleasant descent into madness

Tag: ROOT

WSL2 fix clockskew

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

[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 ↑