[HowTo] Automatically sync timezone, date, time with minimal overhead

There are numerous approaches to setting time and date in linux, often easily accessible in Desktop Environment settings.

But we may have reasons to make sure our time is set correctly even during travel and other scenarios. Some ‘automatic sync’ offerings may require continual background services or otherwise not act the way we want.

In this example we will accomplish

  • Automatic timezone setting applied via network location.
  • Automatic date and time sync with network.
  • Timezone configured on connection change, not with VPNs etc.
  • Date and time configured after boot and network, not continual background service.

Requirements

  • networkmanager
  • ntp

Timezone script for NetworkManager:

/etc/NetworkManager/dispatcher.d/09-timezone

#!/bin/sh
case "$2" in
    connectivity-change)
        timedatectl set-timezone "$(curl --fail https://ipapi.co/timezone)"
    ;;
esac

Enable the one-shot time sync service from ntpd:

systemctl enable ntpdate

(executes ntpd -q -n -g -u ntp:ntp after network and nameservers are reached and exits)

Note: Time sync can occur up to a few minutes after login, depending on your connection.

Extra considerations;

Further configuration may be necessary, especially in the case of multiboot. Covered in this guide:

See more information, including other software or alternative time-zone servers, at the Archwiki:
https://wiki.archlinux.org/title/System_time

2 Likes