Posted on April 18, 2023 by nexonhost
How To Set Up Time Synchronization On Ubuntu 20.04
Accurate timekeeping is integral to modern software deployments. Without it, you may encounter data corruption, errors, and other issues that are difficult to debug. Time synchronization can help ensure your logs are being recorded in the correct order, and that database updates are appropriately applied.
Fortunately, Ubuntu 20.04 has time synchronization built-in and activated by default using systemd’s timesyncd service. In this article, you will practice some general time-related commands, verify that timesyncd is active, and install an alternate network time service.
Prerequisites
Before starting this tutorial, you will need an Ubuntu 20.04 server with a non-root, sudo-enabled user and a firewall, as described in this Ubuntu 20.04 server setup tutorial.
Navigating Basic Time Commands
To view the time on your server, you will use the command date. Any user can run this command to print out the date and time:
date
Copy
Typically, your server will generate an output with the default UTC time zone.
Tue 18 Apr 2023 05:20:42 PM CEST
UTC is Coordinated Universal Time, the time at zero degrees longitude. While this may not reflect your current time zone, using Universal Time prevents confusion when your infrastructure spans multiple time zones.
If you want to change your time zone, however, you can use the timedatectl command.
First, run this command to generate a list of available time zones:
timedatectl list-timezones
A list of time zones will print to your screen. You can press SPACE to page down, and b to page up. Once you find the correct time zone, make note of it then type q to exit the list.
Next, you can set the time zone with timedatectl set-timezone by replacing the highlighted portion with the time zone you found in the list. You’ll need to use sudo with timedatectl to make this change:
sudo timedatectl set-timezone Europe/Berlin
You can verify your changes by running date again:
date
Tue 18 Apr 2023 09:20:42 PM CEST
The time zone abbreviation will reflect the newly chosen value.
Now that you’ve practiced checking the clock and setting time zones, you can confirm that your time is being synchronized properly in the next section.
Controlling timesyncd with timedatectl
Previously, most network time synchronization was handled by the Network Time Protocol daemon or ntpd. This service connects to a pool of other NTP servers that provide it with constant and accurate time updates.
But now with Ubuntu’s default install, you can use timesyncd instead of ntpd. timesyncd works similarly by connecting to the same time servers, but is llightweight and more closely integrated with systemd on Ubuntu.
You can query the status of timesyncd by running timedatectl with no arguments. You don’t need to use sudo in this case:
timedatectl
Output Local time: Tue 2023-04-18 17:24:15 CEST Universal time: Tue 2023-04-18 15:24:15 UTC RTC time: Tue 2023-04-18 15:24:17 Time zone: Europe/Berlin (CEST, +0200) System clock synchronized: no NTP service: active RTC in local TZ: no
This command prints out the local time, universal time (which may be the same as local time, if you didn’t switch from the UTC time zone), and some network time status information. System clock synchronized: yes reflects that the time is successfully synced, and NTP service: active means that timesyncd is up and running.
If your output shows that NTP service isn’t active, turn it on with timedatectl:
sudo timedatectl set-ntp on
After this, run timedatectl again to confirm the network time status. It may take a minute for the sync to happen, but eventually System clock synchronized: will read yes and NTP service: will show as active.
Switching to ntpd
timesyncd will work in most circumstances. There are instances, however, when an application may be sensitive to any disturbance with time. In this case, ntpd is an alternative network time service you can use. ntpd uses sophisticated techniques to constantly and gradually keep the system time on track.
Before installing ntpd, you need to turn off timesyncd in order to prevent the two services from conflicting with one another. You can do this by disabling network time synchronization with the following command:
sudo timedatectl set-ntp no
Verify that time synchronization is disabled:
timedatectl
Check that your output reads NTP service: inactive. This means timesyncd has stopped. Now you’re ready to install the ntp package with apt.
First, run apt update to refresh your local package index:
sudo apt update
Then, run apt install ntp to install this package:
sudo apt install ntp
ntpd will begin automatically after your installation completes. You can verify that everything is working correctly by querying ntpd for status information:
ntpq -p
remote refid st t when poll reach delay offset jitter ============================================================================== 0.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 1.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 2.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 3.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000 *86.127.71.168 ( 7.83.176.183 2 u 7 64 1 13.758 0.081 0.752
ntpq is a query tool for ntpd. The -p flag requests information about the NTP servers (or peers) ntpd is connected to. Your output will be slightly different but will list the default Ubuntu pool servers plus a few others. Remember, it can take a few minutes for ntpd to establish connections.
Conclusion
In this article, you’ve successfully viewed the system time, changed time zones, worked with Ubuntu’s default timesyncd service, and installed ntpd. If you have advanced timekeeping needs, you can reference the official NTP documentation, and also take a look at the NTP Pool Project, a global group of volunteers providing much of the world’s NTP infrastructure.