This article describes how to use timedatectl to change the time zone and how to use NTP (Network Time Protocol) on Linux to automatically synchronize the system clock with a remote server.
timedatectl is part of systemd and can be used as a command line utility, which allows to change various settings of the system clock.
How to use timedatectl to change time zone on Linux
Before changing the time zone, please use timedatectl to find the currently set time zone (other information about the system time setting is also displayed):
timedatectl
This is the same as timedatectl status.
Sample output:
# timedatectl
Local time: Sat 2020-09-12 19:14:59 CST
Universal time: Sat 2020-09-12 11:14:59 UTC
RTC time: Sat 2020-09-12 11:14:59
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
Now, let us 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 the command to change the time zone):
timedatectl list-timezones
The list of time zones is huge. You can use grep to filter it to only display the time zone of a certain continent or the capital of a certain country/state, for example, only display possible European time zones:
timedatectl list-timezones | grep Shanghai
Asia/Shanghai
Now , use the following command to set the time zone on the Linux system:
timedatectl set-timezone <timezone>
Where is the time zone listed in <timezone> timedatectl list-timezones? For example, set the Linux system time zone to Asia/Shanghai:
timedatectl set-timezone Asia/Shanghai
This command creates a symbolic link /etc/localtime from /usr/share/zoneinfo/ to the selected time zone. You can also create this link manually and achieve the same purpose. In the same example, to set the time zone to Asia/Shanghai, /etc/localtime needs to be the symbolic link /usr/share/zoneinfo/Asia/Shanghai.
How to use timedatectl to synchronize the system clock with a remote server (enable NTP)
First use timedatectl to find whether the network time synchronization (NTP) service is active and whether 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 of true.
It is worth noting that if the NTP service is not installed, such as timesyncd, ntpd, Chrony or other services, this command will fail. However, in many cases, timesyncd should be installed by default (for example, it is installed by default in Ubuntu 16.04 and later).
If you use a service such as chrony or ntpd to make changes, systemd-timedated will not display these contents before restarting:
systemctl restart systemd-timedated
On the Ubuntu 18.04 server, I must also restart systemd-timesyncd (for example, this operation is not needed on my Ubuntu 19.04 or Solus OS system), otherwise the system time will not be synchronized. If you also use timesyncd and timedatectl shows System clock synchronized: is no, even if the NTP service shows active, you can restart systemd-timesyncd:
systemctl restart systemd-timesyncd
I should also add that when using the default systemd timesyncd service, you can see more information than timedatectl provides, such as the NTP time server used, and the log showing the last synchronization performed, including:
systemctl status systemd- timesyncd
Sample output:
systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
Loaded: loaded (/usr/lib/systemd/system/systemd-timesyncd.service; disable>
Active: active (running) since Sat 2020-09-12 19:21:43 CST; 4s ago
Docs: man:systemd-timesyncd.service(8)
Main PID: 3518 (systemd-timesyn)
Status: "Initial synchronization to time server 5.79.108.34:123 (0.opensus>
Tasks: 2 (limit: 4915)
Memory: 1.5M
CGroup: /system.slice/systemd-timesyncd.service
└─3518 /usr/lib/systemd/systemd-timesyncd
Sep 12 19:21:42 localhost systemd[1]: Starting Network Time Synchronization...
Sep 12 19:21:43 localhost systemd[1]: Started Network Time Synchronization.
Sep 12 19:21:43 localhost systemd-timesyncd[3518]: Initial synchronization to t
On systemd 239 and later (for example, it does not work on Ubuntu 18.04 because it uses systemd 237), you can use the following command to display the systemd-timesyncd status timedatectl show-timesync:
timedatectl show-timesync
FallbackNTPServers=0.opensuse.pool.ntp.org 1.opensuse.pool.ntp.org 2.opensuse.pool.ntp.org 3.opensuse.pool.ntp.org
ServerName=0.opensuse.pool.ntp.org
ServerAddress=5.79.108.34
RootDistanceMaxUSec=5s
PollIntervalMinUSec=32s
PollIntervalMaxUSec=34min 8s
PollIntervalUSec=1min 4s
NTPMessage={ Leap=0, Version=4, Mode=4, Stratum=2, Precision=-24, RootDelay=17.211ms, RootDispersion=80.734ms, Reference=8285010A, OriginateTimestamp=Sat 2020-09-12 19:22:15 CST, ReceiveTimestamp=Sat 2020-09-12 19:22:15 CST, TransmitTimestamp=Sat 2020-09-12 19:22:15 CST, DestinationTimestamp=Sat 2020-09-12 19:22:16 CST, Ignored=no PacketCount=2, Jitter=3.787ms }
Frequency=-6368726
和属性systemd-timesyncd使用timedatectl timesync-status:
timedatectl timesync-status
Server: 5.79.108.34 (0.opensuse.pool.ntp.org)
Poll interval: 1min 4s (min: 32s; max 34min 8s)
Leap: normal
Version: 4
Stratum: 2
Reference: 8285010A
Precision: 1us (-24)
Root distance: 89.339ms (max: 5s)
Offset: -10.021ms
Delay: 358.105ms
Jitter: 3.787ms
Packet count: 2
Frequency: -97.179ppm
You can change the settings shown here by editing the /etc/systemd/timesyncd.conf configuration file. For example, change the NTP server (you can use the server provided by the NTP Pool Project ), uncomment the NTP line, and add the servers to be used, separated by spaces. After changing the configuration file, restart systemd-timesyncd:
systemctl restart systemd-timesyncd