Score:1

Timezone getting set wrong in WSL?

cn flag

Running Ubuntu in WSL2 my clock is getting screwed up somehow. And when I try to set it again using:

sudo dpkg-reconfigure tzdata

It still gets set about an hour behind the real time.

The screenshot shows the real current time in Windows, and also Windows via an RDP connection to the machine running WSL2/Ubuntu.

The purple terminal window is the Ubuntu having trouble.

Not super-great with Linux; any idea what's causing the time to be set wrong?

enter image description here

Nmath avatar
ng flag
I know that Windows sets the hardware clock to local time instead of UTC because it's a barbaric neanderthal. In dual boot configurations you have to either change how Windows or Ubuntu derives local time, but I was not aware that this is necessary in WSL. Did you or some application explicitly set the time or adjust time settings in Ubuntu? Because it's possible that messing with it caused the same discrepancy in a dual boot config. You may need to make Ubuntu derive time from local time, or you can configure Windows to derive local time from UTC. https://askubuntu.com/q/169376
Score:0
vn flag

Is there any chance the computer has been in Sleep or Hibernate, or is the WSL machine running in a VM? That was (at one point) known to cause WSL's "hardware clock" to become out-of-sync with the real/Windows time.

Reference: This Stack Overflow answer and the related Github issue.

The core issue (at least the Sleep/Hibernate issue) is fixed by the 5.10.16.3 WSL2 kernel release, which you can update to (if you aren't on it already) with wsl --update (in recent Windows releases) or via Windows Update (assuming you have allowed "other Microsoft products" to update as part of it).

However, there may still be clock drift issues, as noted in that Github issue.

If so, try a sudo hwclock -s, and if that doesn't fix it, a wsl --shutdown and restart.

Axeltherabbit avatar
gq flag
x86_64 Linux 5.15.90.1-microsoft-standard-WSL2 and is still broken
by flag
Using `wsl --update` is a great hotfix for WSL issues. Fixes time-drift and update errors in signed repos. Highly recommend! Note, if you leave a WSL process—like a VS Code instance—open when the computer sleeps/hibernates that can cause some issues.
NotTheDr01ds avatar
vn flag
@SentientFlesh `wsl --update` checks for a new release of WSL. If it finds it, it shuts down (e.g. `wsl --shutdown`, as mentioned in my answer) and installs it. However, if there's no new WSL update, then `wsl --update` is a no-op as far as I can tell. I just tested this by setting the date incorrectly in Ubuntu, then running a `wsl --update`, which did not correct the date/time. Can you provide more info on what behavior you are seeing with `wsl --update`? Thanks!
by flag
@NotTheDr01ds With incorrect time, ran `wsl --update` in PS and it fixed the time. Ran the command with correct time and got: ``` Checking for updates. The most recent version of Windows Subsystem for Linux is already installed. ``` Changed WSL time using `sudo date -s "2027-11-11 11:11:11"` and confirmed with `date`. Ran `wsl --update` in PS again, received the same message as above, but now my WSL time is correct from when before I changed it manually.
by flag
@NotTheDr01ds Ah, I just found out why `wsl --update` seemed to be working. Since I have one remote server I connect to over a VPN that requires the server time to match I have a check in my `.bashrc` that checks to make sure I'm not connected to said server, and if not, run `sudo hwclock -s` to correct the system time if needed. So that little line in my `.bashrc` was making it look like `wsl --update` was working when really it was just WSL performing a check on whether or not it's okay to update the time.
J. Scott Elblein avatar
cn flag
Sounds like you've hit the nail on the head. I came across this later, which also mentions hibernation being an issue (https://tomssl.com/fixing-clock-drift-in-wsl2-using-windows-terminal). Recently the power went out in a storm, and send that WSL computer into hibernation. I think that probably did it. Currently I used the workaround on his page, but will look into using one from the above as well. thanks!
J. Scott Elblein avatar
cn flag
As a side note I just checked the WSL kernel version and it's `Kernel version: 5.10.60.1`. Looks like they didn't get it sorted out yet, lol.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.