Friday, February 16, 2018

NTP : Network Time Protocol: Install NTP server on Linux machines

Network Time Protocol: Install NTP server on Linux machines


NTP or Network Time Protocol is a very important service that is used to keep the system time accurate. NTP service is used to synchronize time on all local servers with respect to a designated server called, NTP server. That NTP server itself syncs time from a public NTP server.

NTP synchronized systems clocks are synchronized upto millisecond precision. Most big organizations have NTP servers for managing time for their IT infra. In this tutorial, we will learn to install & configure NTP server on CentOS & Ubuntu machines.


Installation

We need to install a package named ‘ntp’ on the machine which will be configured as NTP servers. To install ntp on the CentOS/RHEL servers, open terminal & run the following command,
$ sudo yum install ntp
For Ubuntu systems, execute the following command from the terminal,
$ sudo apt-get install ntp
Now start the ntp service & enable it for boot time with the following commands,
$ sudo systemctl start ntpd
$ sudo systemctl enable ntpd

Configuration

After we have installed the ntp server, we will now configure it & first thing we need is the address for public ntp servers closest to us or at a desired location. To get the list of all the ntp server, goto the following url,
http://www.pool.ntp.org/zone/@
& select the ntp server of your choosing. We will now make the server entries in ntp configuration file i.e. ‘/etc/ntp.conf’. For this tutorial, we will be using the ntp servers from North Ameraica/United states,
$ sudo vim /etc/ntp.conf
server 0.us.pool.ntp.org
server 1.us.pool.ntp.org
server 2.us.pool.ntp.org
server 3.us.pool.ntp.org
Also enable logging to troubleshoot any issues with ntp, to do this make the entry for following line in the same file,
logfile /var/log/ntp.log
Save the file & exit. Restart the ntp service to implement the changes made,
$ sudo systemctl restart ntpd
Now to make sure that our ntp server is synchronized with the public ntp server, run the following command from the terminal,
$ ntpq –p
This command will show complete information for time sync between local ntp server & public nt server.
Note:- If using firewall on the system, make sure that UDP port 123 is open for ntp to work. Open the ntp port with the following command,
$ firewall-cmd –add-service=ntp –permanent
$ firewall-cmd –reload

Configuring a local client

Once our local ntp server is ready, we will configure it on Linux client machine. But before we do that, we need to make sure that our local ntp server is available for synchronizing time on local network. Open the ntp server & make an entry for following line on ntp.conf,
$ sudo vim /etc/ntp.conf
# Hosts on local network are less restricted.
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
Here, 192.168.1.0 is the local network. Now restart the ntp service after saving the file & login to client machine on which ntp will be configured.
Similarly as we did on ntp server, we also need to install ntp service on client machine as well. After the service has been installed, open the file ‘ntp.conf’ & add the ntp server IP address to the bottom of the file (its 192.168.1.100 in our case),
$ sudo vim /etc/ntp.conf
server 192.168.1.100
Now restart the ntp service after saving the file. That’s it, our ntp client server is configured & will now sync the time from our local ntp server instead of internet or other sources.
Note:- We can also run the following command to update the time manually on client machine from our local ntp server,
$ ntpdate 192.168.1.100
That’s it guys, with this we end our tutorial on how to install & configure ntp server.

No comments: