One of the most important components of digital software deployment is accurate timekeeping. Whether you want to record accurate logs or update your database correctly, time synchronization is critical.
This tutorial will help you configure time synchronization on your Ubuntu 18.04 VPS or Dedicated server. You will also get to understand some of the most commonly used time-centric commands and master the skills required to implement an optional network time utility.
Ready? Let’s go!
Understanding The Basic Ubuntu 18.04 Commands
The most commonly used time command on Ubuntu 18.04 is the date command. This command is used to check the time and date on an Ubuntu server:
$ sudo date
This will deliver an output showing the time and date on your Ubuntu 18.04 system
SunOct 28 16:03:38 UTC 2018
As you can see; from the above command, your Ubuntu server is set to the default UTC timezone. It always nice to consistently set the time zone to the Coordinated Universal Time and avoid spanning infrastructure in multiple time zones.
However, if you have projects with complex time requirements, you can easily adjust your time zone using the timedatectl command.
To change your timezone, first, run the command below to view a list of the time zones:
$ sudo timedatectl list-timezones
Scroll up and down this list to pinpoint your preferred time zone and note it down. Press q to quit this list, and issue the command to implement this timezone:
$ sudo timedatectl set-timezoneAmerica/New_York
This will change your time zone to America/New_York. To check if this time zone was properly implemented, execute the command below:
$ sudo date
This will give you the output below:
SunOct 28 12:18:59 EDT 2018
That is it! You now know how to view or change your timezone on Ubuntu 18.04 system.
Implementing The Timesyncd
For long, the ntpd (Network Time Protocol Deamon) has been used to handle time synchronization in Ubuntu systems. The ntpd links with a suite of NTP servers to deliver precise time updates constantly.
In spite of its power and usability, the ntpd has been replaced by the timesyncd service in the latest Ubuntu systems. The timesyncd service is almost identical to the ntpd in terms of the way it works and the time server it connects to. However, timesncd has multiple advantages over the ntpd in that its lightweight, integrates seamlessly with the systemd and Ubuntu systems.
To check the timesyncd’s status on your Ubuntu 18.04 system, run the command below:
$ sudo timedatectl
This will give you the following output:
Local time: Sun 2018-10-28 12:54:53 EDT Universal time: Sun 2018-10-28 12:54:53 UTC RTC time: Sun 2018-10-28 12:54:53 Time zone: America/New_York (EDT, -0400) System clock synchronized: yes systemd-timesyncd.service active: yes RTC in local TZ: no
The value YES for the System clock synchronized proves that time is correctly synced and the YES for the systemd-timesyncd.service active indicateds that timesyncd has been enabled and is running properly.
In case, the timesyncd in inactive, run the command below to tun it on;
$ sudo timedatectl set-ntpon
Once you enable timesyncd, execute the timedatectl to check the status of the network time.
Changing To Ntpd
The timesyncd is ideal for many purposes, but not all. Some applications are time-sensitive and may be better handled by the ntpd. This is because ntpd utilizes sophisticated techniques and has the ability to handle even the smallest time perturbation. If you are working on the time-sensitive applications, follow the steps below to switch to ntpd utility.
First, run the command below to deactivate the timesyncd:
$ sudo timedatectl set-ntpno
Next, issue the command below to check timesyncd’s status:
$ sudo timedatectl
Check to see the value of systemd-timesyncd.service active is no on the output. Now you can execute the command below to install ntp:
$ sudo apt update $ sudo apt install ntp
The ntpd will be installed and started automatically. Run the command below to check the status details of ntpd:
$ sudo ntpq -p
This will give you the output similar to the one below:
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 +ec2-52-0-56-137 22.214.171.124 2 u 16 64 1 7.872 -2.137 1.485 +126.96.36.199 188.8.131.52 2 u 12 64 1 65.204 3.740 2.686 +block.steinhoff 184.108.40.206 2 u 11 64 1 33.364 1.710 3.586 +eterna.binary.n 220.127.116.11 3 u 11 64 1 35.330 2.821 2.839 +2604:a880:800:1 18.104.22.168 2 u 14 64 1 0.394 0.386 2.462 +ec2-52-6-160-3. 22.214.171.124 2 u 11 64 1 8.150 2.050 3.053 +mx.danb.email 127.67.113.92 2 u 13 64 1 63.868 1.539 2.240 *hydrogen.consta 126.96.36.199 2 u 12 64 1 2.989 1.755 2.563 .....
Congratulations! You have learned how to change time zones, check system time, and deploy the default timesyncd. For more information on ntpd, visit NTP’s documentation page.