-


How To Set Timezone And Enable Network Time Sync (NTP) From The Command Line Using timedatectl

This article explains how to use timedatectl to change the timezone and enable automatic synchronization of the system clock with a remote server using the NTP (Network Time Protocol) on Linux.

timedatectl is a command line utility available as part of systemd that allows changing various settings of your system clock.

How to Change timezone on Linux using timedatectl


Before changing your time zone, start by using timedatectl to find out the currently set time zone (also shows other information about the system time settings):

timedatectl

This is the same as timedatectl status.

Example with output:

$ timedatectl
                      Local time: Fri 2019-07-19 13:23:38 IST
                  Universal time: Fri 2019-07-19 12:23:38 UTC
                        RTC time: Fri 2019-07-19 12:23:38
                       Time zone: Europe/Dublin (IST, +0100)
       System clock synchronized: no
systemd-timesyncd.service active: yes
                 RTC in local TZ: no

Now let's list all the available time zones, so you know the exact name of the time zone you'll use on your system (needed for the command that changes the time zone):

timedatectl list-timezones

The list of time zones is quite large. You can filter it to only show time zones for a continent or the capital city of a country/state, by using grep, e.g. to only show possible Europe time zones:

timedatectl list-timezones | grep Europe

Now set the timezone on your Linux system using the command:

timedatectl set-timezone <timezone>

Where <timezone> is a timezone listed by timedatectl list-timezones. For example, set your Linux system timezone to America/Lost_Angeles with:

timedatectl set-timezone America/Los_Angeles

This command creates a symbolic link for the time zone you choose from /usr/share/zoneinfo/ to /etc/localtime. You may also create this link manually and achieve the same thing. Using the same example, to set the timezone to America/Los_Angeles, /etc/localtime needs to be a symbolic link to /usr/share/zoneinfo/America/Los_Angeles.

How to synchronize the system clock with a remote server (enable NTP) using timedatectl


Start by using timedatectl to find if the network time synchronization (NTP) service is active and your system clock synchronized:

timedatectl

Enable the NTP service on your Linux system with the command:

timedatectl set-ntp true

If you want to disable it, just use false instead of true.

It's worth noting that this command fails if a NTP service is not installed, e.g. timesyncd, ntpd, Chrony or others. timesyncd should be installed by default in many cases though (for example it's installed by default with Ubuntu 16.04 and newer).

If using a service like chrony or ntpd to make changes, these are not shown by timedatectl until systemd-timedated is restarted:

sudo systemctl restart systemd-timedated

On an Ubuntu 18.04 server I also had to restart systemd-timesyncd (but this was no needed on my Ubuntu 19.04 or Solus OS systems for example), or else the system time would not get synchronized. In case you're also using timesyncd, and timedatectl shows System clock synchronized: as no, even though NTP service shows as active, restart systemd-timesyncd:

sudo systemctl restart systemd-timesyncd

I should also add that when using the default systemd timesyncd service, you can see some more information than that provided by timedatectl, like the NTP time server used, and a log showing the last time the synchronization was performed, with:

systemctl status systemd-timesyncd

Example with output:

$ systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendo
   Active: active (running) since Fri 2019-07-19 13:28:49 IST; 11min ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 2232 (systemd-timesyn)
   Status: "Synchronized to time server 91.189.94.4:123 (ntp.ubuntu.com)."
    Tasks: 2 (limit: 3159)
   CGroup: /system.slice/systemd-timesyncd.service
           └─2232 /lib/systemd/systemd-timesyncd

Jul 19 13:28:49 logix22 systemd[1]: Starting Network Time Synchronization...
Jul 19 13:28:49 logix22 systemd[1]: Started Network Time Synchronization.
Jul 19 13:28:48 logix22 systemd-timesyncd[2232]: Synchronized to time server 91.

On systemd 239 and newer (e.g. this won't work on Ubuntu 18.04, because it uses systemd 237) you may show the systemd-timesyncd status using timedatectl show-timesync:

$ timedatectl show-timesync
SystemNTPServers=0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org
FallbackNTPServers=ntp.ubuntu.com
ServerName=0.pool.ntp.org
ServerAddress=194.102.58.173
RootDistanceMaxUSec=5s
PollIntervalMinUSec=32s
PollIntervalMaxUSec=34min 8s
PollIntervalUSec=1min 4s
NTPMessage={ Leap=0, Version=4, Mode=4, Stratum=2, Precision=-23, RootDelay=22.003ms, RootDispersion=21.194ms, Reference=C102015C, OriginateTimestamp=Fri 2019-07-19 13:59:53 IST, ReceiveTimestamp=Fri 2019-07-19 13:59:53 IST, TransmitTimestamp=Fri 2019-07-19 13:59:53 IST, DestinationTimestamp=Fri 2019-07-19 13:59:53 IST, Ignored=no PacketCount=1, Jitter=0 }
Frequency=1647689

And the properties systemd-timesyncd using timedatectl timesync-status:

$ timedatectl timesync-status
       Server: 194.102.58.173 (0.pool.ntp.org)
Poll interval: 1min 4s (min: 32s; max 34min 8s)
         Leap: normal
      Version: 4
      Stratum: 2
    Reference: C102015C
    Precision: 1us (-23)
Root distance: 32.195ms (max: 5s)
       Offset: +3.652ms
        Delay: 2.903ms
       Jitter: 0
 Packet count: 1
    Frequency: +25.142ppm

You can change the settings shown here by editing the /etc/systemd/timesyncd.conf configuration file. E.g. to change the NTP servers (you could use the servers provided by the NTP Pool Project), uncomment the NTP line, and add the servers you want to use separated by a space. After changing the configuration file, restart systemd-timesyncd:

sudo systemctl restart systemd-timesyncd