0

I have a Ubuntu 18.04 WSL2 machine running in Windows 10 and from time to time I get this weird problem where the time is out of sync in the WSL2 machine. My timezone is Eastern Time (-5) and as you can see below my WSL is 5 hours in the future. Feels like for some reason my WSL machine interprets my local time as UTC and do a calculation based on that information.

WSL2

$ date 
Fri Jan 22 03:13:28 EST 2021
$ cat /etc/timezone
America/Toronto
$ date +"%Z %z"
EST -0500

PowerShell

> date
Thursday, January 21, 2021 10:13:31 PM
> Get-TimeZone


Id                         : Eastern Standard Time
DisplayName                : (UTC-05:00) Est (É.-U. et Canada)
StandardName               : Est
DaylightName               : Est (heure d’été)
BaseUtcOffset              : -05:00:00
SupportsDaylightSavingTime : True

I tried doing a sudo dpkg-reconfigure tzdata but it did not help.

EDIT: adding a screenshot, time won't match the time I mentioned before but the screenshot shows the correct local time and timezone.

enter image description here

0

2 Answers 2

2

I have a Ubuntu 18.04 WSL2 machine running in Windows 10 and from time to time I get this weird problem where the time is out of sync in the WSL2 machine. My timezone is Eastern Time (-5) and as you can see below my WSL is 5 hours in the future. Feels like for some reason my WSL machine interprets my local time as UTC and do a calculation based on that information.

This is a known issue with WSL2: system date is not same with windows (WSL 2) #4245

You have to run the following command within the WSL2 instance:

sudo hwclock -s

You could instead run the following PowerShell command:

wsl -u root sh -C "hwclock -S"

You also have to perform the following PowerShell command:

wsl --shutdown

You will have to restart WSL instance after you run that command.

The solution also requires Chrony to be used with the following option commented out within /etc/chrony/chrony.conf

# Stop bad estimates upsetting machine clock.
# maxupdateskew 100.0

If you are not already using it:

sudo apt-get update && sudo apt-get install -y chrony && sudo chronyd

An alternative solution is adding the following to .bashrc:

# WSL2 clock skew hack
__customprompt() {
  NOW=$(date +%s)
  if (( NOW > WSL_NEXT_CLOCKSYNC )); then
    echo "$(date -Iseconds) - hwclock [$WSL_NEXT_CLOCKSYNC : $NOW]" >> ~/.wsltimesync
    nohup wsl.exe -u root -c 'hwclock -s' &>/dev/null &
    export WSL_NEXT_CLOCKSYNC=$(date --date='+5 minutes' +%s)
  fi
}

Source:

Another alternative solution is to handle it automatically within Windows based on the detection of a specific event that is logged.

This can be handled by the following PowerShell script:

function Log($message) {
    $output = (Get-Date).ToUniversalTime().ToString("u") + "`t$message"
    Add-Content -Path "~/.wsl-clock.log" -Value $output
}

Log "********************************"
Log "*** Update WSL clock starting..."

$runningDistroCount = wsl --list --running --quiet |
        Where-Object {$_ -ne ""} |
        Measure-Object |
        Select-Object -ExpandProperty Count

if ($runningDistroCount -eq 0){
    Log "No Distros - quitting"
    exit 0
}

$originalDate=wsl sh -c "date -Iseconds"

Log "Performing reset..."
$result = wsl -u root sh -c "hwclock -s" 2>&1
$success = $?
if (-not $success){
    Log "reset failed:"
    Log $result
    exit 2
} else {
    $newDate=wsl bash -c "date -Iseconds"
    Log "clock reset"
    Log "OriginalDate:$originalDate"
    Log "NewDate:     $newDate"
    exit 0
}

Source: Fixing Clock Skew with WSL 2

You can also use wsl-clock

Additional Resources:

0
0

I just issued the following command:

sudo hwclock -s

and it worked for me. This command fixed the issue as described here.

The environment that I was using was:

  • Operating System : Windows 10
  • WSL running Ubuntu 22.04.2 LTS

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .