Go to content

geek_stuff/server & linux

Setup NTP synchronization in WSL2 Ubuntu 20.04

There are lots of attempts to sync time on wsl2, as it somehow clock drifts.

To name few:

However, I didn't like these methods, because:

  • everytime I open terminal, it syncs, which generates unnecessary traffic, too often timesync.
  • second method, I have run them manually.
  • launches wsl2 if wsl2 is not running, which consumes memory.

So, I was thinking sync time using systemd.

root@ikkonote-sam:/etc/systemd# systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
     Active: inactive (dead)
  Condition: start condition failed at Thu 2021-08-26 14:39:24 +07; 2s ago
             └─ ConditionVirtualization=!container was not met
       Docs: man:systemd-timesyncd.service(8)

Aug 26 13:18:01 ikkonote-sam systemd[1]: Condition check resulted in Network Time Synchronization being skipped.
Aug 26 13:30:24 ikkonote-sam systemd[1]: Condition check resulted in Network Time Synchronization being skipped.
Aug 26 13:32:16 ikkonote-sam systemd[1]: Condition check resulted in Network Time Synchronization being skipped.
Aug 26 14:39:24 ikkonote-sam systemd[1]: Condition check resulted in Network Time Synchronization being skipped.
root@ikkonote-sam:/etc/systemd#

When I run systemctl status systemd-timesyncd, it showed inactive with ConditionVirtualization=!container was not met

let's find where this systemd-timesyncd.service is and see what's inside.

root@ikkonote-sam:/etc/systemd# locate systemd-timesyncd.service
/etc/systemd/system/sysinit.target.wants/systemd-timesyncd.service

root@ikkonote-sam:/etc/systemd# cat /etc/systemd/system/sysinit.target.wants/systemd-timesyncd.service

Let's take a look what this file contains.

root@ikkonote-sam:/etc/systemd# cat /etc/systemd/system/sysinit.target.wants/systemd-timesyncd.service
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Network Time Synchronization
Documentation=man:systemd-timesyncd.service(8)
ConditionCapability=CAP_SYS_TIME
ConditionVirtualization=!container
DefaultDependencies=no
After=systemd-sysusers.service
Before=time-set.target sysinit.target shutdown.target
Conflicts=shutdown.target
Wants=time-set.target time-sync.target

...ommited...

So let's comment out the line where ConditionVirtualization=!container using vi.

root@ikkonote-sam:/etc/systemd# cat /etc/systemd/system/sysinit.target.wants/systemd-timesyncd.service
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Network Time Synchronization
Documentation=man:systemd-timesyncd.service(8)
# ConditionCapability=CAP_SYS_TIME
ConditionVirtualization=!container
DefaultDependencies=no
After=systemd-sysusers.service
Before=time-set.target sysinit.target shutdown.target
Conflicts=shutdown.target
Wants=time-set.target time-sync.target

...ommited...

Now, let's try again

root@ikkonote-sam:/etc/systemd# systemctl restart systemd-timesyncd
Warning: The unit file, source configuration file or drop-ins of systemd-timesyncd.service changed on disk. Run 'systemctl daemon-reload' to reload units.

We changed the configuration, systemd is complaining to reload.

root@ikkonote-sam:/etc/systemd# systemctl daemon-reload

restart system, and check status

root@ikkonote-sam:/etc/systemd# systemctl restart systemd-timesyncd
root@ikkonote-sam:/etc/systemd# systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-08-26 14:40:57 +07; 6s ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 9758 (systemd-timesyn)
     Status: "Initial synchronization to time server 162.159.200.1:123 (vn.pool.ntp.org)."
      Tasks: 2 (limit: 15194)
     Memory: 1.1M
     CGroup: /system.slice/systemd-timesyncd.service
             └─9758 /lib/systemd/systemd-timesyncd

Aug 26 14:40:57 ikkonote-sam systemd[1]: Starting Network Time Synchronization...
Aug 26 14:40:57 ikkonote-sam systemd[1]: Started Network Time Synchronization.
Aug 26 14:40:59 ikkonote-sam systemd-timesyncd[9758]: Initial synchronization to time server 162.159.200.1:123 (vn.pool.ntp.org).
root@ikkonote-sam:/etc/systemd#

Voila! it now works.

 

 

Ps. If you'd like to manually configure the ntp server, you can lookup to `/etc/systemd/timesyncd.conf`, but default config works okay for me. If you want to see default values of the configuration just type: `man timesyncd.conf`