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
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.
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:
Typically, your server will generate an output with the default UTC time zone.
OutputThu Aug 5 15:55:20 UTC 2021
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
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
timedatectl to make this change:
- sudo timedatectl set-timezone America/New_York
You can verify your changes by running
OutputThu Aug 5 11:56:01 EDT 2021
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.
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
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:
OutputLocal time: Thu 2021-08-05 11:56:40 EDT Universal time: Thu 2021-08-05 15:56:40 UTC RTC time: Thu 2021-08-05 15:56:41 Time zone: America/New_York (EDT, -0400) System clock synchronized: yes 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
- 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
NTP service: will show as
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.
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:
Check that your output reads
NTP service: inactive. This means
timesyncd has stopped. Now you’re ready to install the
ntp package with
apt update to refresh your local package index:
apt install ntp to install this package:
ntpd will begin automatically after your installation completes. You can verify that everything is working correctly by querying
ntpd for status information:
Outputremote 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 +t1.time.bf1.yah 126.96.36.199 2 u 16 64 1 61.766 -20.068 1.964 +puppet.kenyonra 188.8.131.52 3 u 16 64 1 2.622 -18.407 2.407 *ntp3.your.org .GPS. 1 u 15 64 1 50.303 -17.499 2.708 +time.cloudflare 10.4.1.175 3 u 15 64 1 1.488 -18.295 2.670 +mis.wci.com 184.108.40.206 2 u 15 64 1 21.527 -18.377 2.414 +ipv4.ntp1.rbaum 220.127.116.11 2 u 12 64 1 49.741 -17.897 3.417 +time.cloudflare 10.4.1.175 3 u 15 64 1 1.039 -16.692 3.378 +18.104.22.168 22.214.171.124 2 u 14 64 1 70.060 -16.993 3.363 +ny-time.gofile. 126.96.36.199 2 u 21 64 1 75.349 -18.333 2.763 golem.canonical 188.8.131.52 2 u 28 64 1 134.482 -21.655 0.000 ntp3.junkemailf 184.108.40.206 2 u 19 64 1 2.632 -16.330 4.387 clock.xmission. .XMIS. 1 u 18 64 1 24.927 -16.712 3.415 alphyn.canonica 220.127.116.11 2 u 26 64 1 73.612 -19.371 0.000 strongbad.voice 18.104.22.168 2 u 17 64 1 70.766 -18.159 3.481 chilipepper.can 22.214.171.124 2 u 25 64 1 134.982 -19.848 0.000 pugot.canonical 126.96.36.199 2 u 28 64 1 135.694 -21.075 0.000
ntpq is a query tool for
-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.
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.