How to use timedatectl to set the time zone from the command line and enable network time synchronization (NTP)

This article explains how to change the time zone using timedatectl, and how to automatically synchronize the system clock with a remote server using NTP (Network Time Protocol) on Linux.Date and time Is a command line utility provided as part of systemd, which allows to change various settings of the system clock.

How to change time zone on Linux using timedatectl

Before changing the time zone, use timedatectl to find out the currently set time zone (also show other information about the system time setting):

timedatectl

with timedatectl status.
Example 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 available time zones so that you know the exact name of the time zone that will be used on the system (required for commands that change the time zone):

timedatectl list-timezones

The list of time zones is large. You can use grep to filter it to show only the time zone of a continent or the capital city of a country, for example only the possible European time zones:

timedatectl list-timezones | grep Europe

Now, set the time zone on the Linux system using the following command:

timedatectl set-timezone 

Where Is listed by time zone timedatectl list-timezones. For example, set the Linux system time zone to America/Lost_Angeles versus:

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 can also create this link manually and achieve the same purpose. Using the same example, set the time zone to America/Los_Angeles, /etc/localtime Needs to be a symbolic link /usr/share/zoneinfo/America/Los_Angeles.

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

First use timedatectl to find out if the Network Time Synchronization (NTP) service is active and if the system clock is synchronized:

timedatectl

Use the following command to enable the NTP service on the Linux system:

timedatectl set-ntp true

If you want to disable it, just use false Instead true.
It is worth noting that this command will fail if the NTP service is not installed. timesyncd, ntpd, Chrony or others. However, in many cases timesyncd should be installed by default (for example, installed by default on Ubuntu 16.04 and later).
If you make changes using a service like chrony or ntpd, timedatectl will not show those changes until systemd-timedated Restart:

sudo systemctl restart systemd-timedated

On Ubuntu 18.04 server, I also have to restart systemd-timesyncd (Such as not needed on my Ubuntu 19.04 or Solus OS system), otherwise the system time will not be synchronized. If you also used timesyncd and timedatectl shows System clock synchronized: Such as noEven if NTP service shown as active, Restart systemd-timesyncd:

sudo systemctl restart systemd-timesyncd

I should also add that when using the default systemd timesyncd service, you will see more information than timedatectl provides, such as the NTP time server used, and a log showing the last synchronization performed, including:

systemctl status systemd-timesyncd

Example 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 later (for example, this does not work on Ubuntu 18.04 as it uses systemd 237), you can use the following command to display the systemd-timesyncd status 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 attribute systemd-timesyncd use 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 do this by editing /etc/systemd/timesyncd.conf Configuration file. E.g. Change the NTP server (you can use the server provided by NTP NTP Pool Project), Uncomment the NTP line, and add the server you want to use, separated by spaces. After changing the configuration file, restart systemd-timesyncd:

sudo systemctl restart systemd-timesyncd

Source

Sidebar