How to Install NTP Server and Client on Ubuntu 18.04 LTS
NTP (Network Time Protocol) is a networking protocol designed to synchronize the clocks of computers over a network. NTP allows you to synchronize the clocks of all the systems within the network. NTP server synchronizes its time by contacting a number of servers around the world.
In this tutorial, we will learn to install NTP server and Setup time synchronization on Ubuntu 18.04 server.
Requirements
- A server running Ubuntu 18.04 server.
- A non-root user with sudo privileges.
- A static IP address 192.168.43.229 configured on your server.
Install and Configure NTP Server
By default, NTP package is available in the Ubuntu 18.04 LTS default repository. You can install it by just running the following command:
sudo apt-get install ntp -y
Next, you will need to configure NTP to synchronize its time from public NTP server. You can do this by editing ntp.conf file:
sudo nano /etc/ntp.conf
Replace the lines:
pool 0.ubuntu.pool.ntp.org iburst pool 1.ubuntu.pool.ntp.org iburst pool 2.ubuntu.pool.ntp.org iburst pool 3.ubuntu.pool.ntp.org iburst
With ones that match your location. In my case, I will use the following lines:
pool 0.asia.pool.ntp.org iburst pool 1.asia.pool.ntp.org iburst pool 2.asia.pool.ntp.org iburst pool 3.asia.pool.ntp.org iburst
A list of available NTP Pool time servers can be found here: http://support.ntp.org/bin/view/Servers/NTPPoolServers
Save and close the file. Then, restart the NTP service to apply the changes:
sudo systemctl restart ntp
You can check the status of NTP with the following command:
sudo systemctl status ntp
Output:
? ntp.service - Network Time Service Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2018-10-05 15:10:14 UTC; 42s ago Docs: man:ntpd(8) Main PID: 5587 (ntpd) Tasks: 2 (limit: 1114) CGroup: /system.slice/ntp.service ??5587 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 113:116 Oct 05 15:10:15 ubuntu1804 ntpd[5587]: Soliciting pool server 123.108.200.124 Oct 05 15:10:16 ubuntu1804 ntpd[5587]: Soliciting pool server 64:ff9b::7b6c:c87c Oct 05 15:10:16 ubuntu1804 ntpd[5587]: Soliciting pool server 64:ff9b::7b6c:c87c Oct 05 15:10:18 ubuntu1804 ntpd[5587]: Soliciting pool server 2406:da1a:200:7201:d9ea:9ac5:32e3:339c Oct 05 15:10:18 ubuntu1804 ntpd[5587]: Soliciting pool server 64:ff9b::7b6c:c87c Oct 05 15:10:19 ubuntu1804 ntpd[5587]: Soliciting pool server 91.189.91.157 Oct 05 15:10:20 ubuntu1804 ntpd[5587]: Soliciting pool server 91.189.94.4 Oct 05 15:10:21 ubuntu1804 ntpd[5587]: Soliciting pool server 91.189.89.198 Oct 05 15:10:22 ubuntu1804 ntpd[5587]: Soliciting pool server 91.189.89.199 Oct 05 15:10:23 ubuntu1804 ntpd[5587]: Soliciting pool server 2001:67c:1560:8003::c7
Install and Configure NTP Client
Now, we will install and configure NTP client to sync time with our NTP server.
Before starting, you will need to setup /etc/hosts file. So, your NTP server can be resolved via hostname.
To do so, open the /etc/hosts file:
sudo nano /etc/hosts
Add the following lines:
192.168.43.229 ntp-server-host
Replace the IP address with the IP of your NTP server (the one we installed in the precious chapter). Save and close the file.
Next, install NTP and ntpdate with the following command:
sudo apt-get install ntpdate ntp -y
Now, manually check the time synchronization with your NTP server with the following command:
sudo ntpdate ntp-server-host
If everything is fine, you should see the following output:
5 Oct 20:48:49 ntpdate[6067]: adjust time server ntp-server-host offset 0.049526 sec
Next, you will need to disable the default Ubuntu systemd’s timesyncd service. You can do this with the following command:
sudo timedatectl set-ntp off
Now, edit /etc/ntp.conf file and set your own configured NTP server as preferred NTP server for the time synchronization.
sudo nano /etc/ntp.conf
Add the following lines:
server ntp-server-host prefer iburst
Save and close the file. Then, restart NTP service to apply the changes:
sudo systemctl restart ntp
Finally, use the ntpq command to list the NTP time synchronization queue:
ntpq -p
You should see that ntp-server-host is selected as the current time synchronization source:
remote refid st t when poll reach delay offset jitter ============================================================================== fwdns2.vbctv.in 202.73.57.107 4 u 3 64 1 127.437 -142.63 0.000 alphyn.canonica 131.188.3.220 2 u 2 64 1 276.931 -106.99 0.000 ntp-server-host 106.10.186.201 3 u 1 64 1 0.588 161.386 0.000