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.
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
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. 220.127.116.11 ==> 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 18.104.22.168 mask 255.255.255.245 nomodify notrap noquery server 22.214.171.124
Now replace 126.96.36.199 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
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
Type below command to check running port on the system.
# netstat -tulpn