Linux Administrator

How To Setup NTP (Network Time Protocol) Server In CentOS/RHEL 7

setup-ntp-server
mm
Written by Santosh Prasad

NTP (Network Time Protocol) is used to synchronize computer’s time with another reference time source. It runs over port 123 UDP at transport layer and allows computers to synchronize time over networks for accurate time.

In CentOS and RHEL system you can use NTP or OpenNTPD server package. In both server and client packages comes by default.

Install NTP

Type the below command to install ntp package on RHEL and CentOS system.

# yum install ntp

Configure NTP Client

To configure NTP, edit /etc/ntp.conf file:

# vim /etc/ntp.conf

And put the below line this file.

server ntp.server.com
or
server 192.168.0.5

Where,

ntp.server.com :- This is the hostname or IP address of the NTP server. You can also use public NTP server located at ntp.org.

Cron For NTP

You can also set cron to run ntpd.

# echo '20 * * * * root /usr/sbin/ntpd -q -u ntp:ntp' > /etc/cron.d/ntpd

In above cron I have instructed cron to run ntpd and after setting the clock exit, and then -u option instructed it to run as the ntp user.

Configure NTP Server

You can also configure your own NTP server if you have lots of server and system in a network. This NTP server will use the central NTP server, provided by your ISP or public time server located at ntp.org to obtain accurate time and date. Then this server NTP server will allow other system in network to request the time and date. Some Example:

192.168.1.5           ==> CentOS / Fedora / RHEL NTPD Server.
202.54.1.5            ==> ISP remote NTP server.
192.168.1.0/24        ==> NTP clients including desktop systems.

First, You have to install and enable ntpd on system, typing below command

# yum install ntp
# chkconfig ntpd on

Now edit /etc/ntp.conf file.

# vim /etc/ntp.conf

Put the below line:

restrict default ignore

This entry will deny all access to any machine, client or server, and you need to specifically reauthorized policy setting.

restrict 202.54.1.5 mask 255.255.255.245 nomodify notrap noquery
server 202.54.1.5

Now replace 202.54.1.5 and mask with your actual remote ISP or ntp.org NTP server IP.

Configure NTP Client To Server

After configuring NTP server, you need to allow ntp client to access the server. For example, if you want to allow 192.168.0.1/24 network. Edit the /etc/ntp.conf fine and add make the below entry:

# Hosts on local network are less restricted.
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap

Firewall Setting

Now update your firewall setting.

# vim /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p udp --dport 123 -j ACCEPT

Save and close file.

Restart NTPD Service

Restart ntpd and firewall service.

# service ntpd start
# service iptables restart

Check Port

Type below command to check running port on the system.

# netstat -tulpn 

Thanks:)

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment