Table of Contents
In this article, we will look into how to configure NTP Server on RHEL/CentOS 7/8 Using 7 Easy Steps. If you are working in any of the Cluster environment then you might be aware of the importance of having clock synced between all the nodes in a Cluster. Generally, a cluster requires a setup from where all its nodes can sync its clock. Any drift in clock might push application to non working state causing huge production loss.
You can also set the clock date and time manually using date and timedatectl commands but every Server hardware has a tendency of clock drifting over a period of time. Hence setting up the date and time manually might not always be a viable solution especially in a cluster environment. Instead one can easily think to configure NTP Server
from where all the nodes can synchronize its date and time without much resource utilization. I will explain more about the steps to configure NTP Server in below sections.
How to Install and Configure NTP Server on RHEL/CentOS 7/8
Also Read: 10 Practical and Useful ntpq Command Examples in Linux(RHEL/CentOS 7/8)
Step 1: Prerequisites
a) You require a running RHEL/CentOS 7/8
Server.
b) You should have yum
installed in your Server. You can check Top 22 YUM command examples in RedHat/CentOS 7 to know more about yum
command.
c) You should have root
or sudo
access to run Privileged commands on your Server. Please Check How to Add User to Sudoers to know more about providing sudo
access to the User.
Step 2: Update Your Server
If you have not updated your Server from quite some time then it will always recommended to first update your installed packages with the latest updates before installing any new package. You can use the yum utility and run yum update -y command to download and install all the latest updates from CentOS Repo.
[root@localhost ~]# yum update -y Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.piconets.webwerks.in * epel: download.nus.edu.sg * extras: mirrors.piconets.webwerks.in * updates: mirrors.piconets.webwerks.in Resolving Dependencies --> Running transaction check ---> Package NetworkManager.x86_64 1:1.18.8-1.el7 will be updated ---> Package NetworkManager.x86_64 1:1.18.8-2.el7_9 will be an update ---> Package NetworkManager-glib.x86_64 1:1.18.8-1.el7 will be updated ---> Package NetworkManager-glib.x86_64 1:1.18.8-2.el7_9 will be an update ---> Package NetworkManager-libnm.x86_64 1:1.18.8-1.el7 will be updated ---> Package NetworkManager-libnm.x86_64 1:1.18.8-2.el7_9 will be an update ---> Package NetworkManager-team.x86_64 1:1.18.8-1.el7 will be updated ---> Package NetworkManager-team.x86_64 1:1.18.8-2.el7_9 will be an update ---> Package NetworkManager-tui.x86_64 1:1.18.8-1.el7 will be updated ---> Package NetworkManager-tui.x86_64 1:1.18.8-2.el7_9 will be an update ---> Package NetworkManager-wifi.x86_64 1:1.18.8-1.el7 will be updated ---> Package NetworkManager-wifi.x86_64 1:1.18.8-2.el7_9 will be an update ---> Package ansible.noarch 0:2.9.14-1.el7 will be updated ---> Package ansible.noarch 0:2.9.16-1.el7 will be an update
Step 3: Install NTP Packages
In the next step you can install NTP package by using yum install ntp -y command as shown below. This will download and install ntp package along with its dependencies.
[root@localhost ~]# yum install ntp -y (1/2): epel/x86_64/primary_db | 6.9 MB 00:00:02 (2/2): epel/x86_64/updateinfo | 1.0 MB 00:00:02 Resolving Dependencies --> Running transaction check ---> Package ntp.x86_64 0:4.2.6p5-29.el7.centos.2 will be installed --> Processing Dependency: ntpdate = 4.2.6p5-29.el7.centos.2 for package: ntp-4.2.6p5-29.el7.centos.2.x86_64 configure ntp server --> Running transaction check ---> Package ntpdate.x86_64 0:4.2.6p5-29.el7.centos.2 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================================================== Package Arch Version Repository Size ======================================================================================================================================================================== Installing: ntp x86_64 4.2.6p5-29.el7.centos.2 base 549 k Installing for dependencies: ntpdate x86_64 4.2.6p5-29.el7.centos.2 base 87 k Transaction Summary ======================================================================================================================================================================== Install 1 Package (+1 Dependent package) Total download size: 635 k Installed size: 1.5 M Downloading packages: (1/2): ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm | 87 kB 00:00:00 (2/2): ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm | 549 kB 00:00:00 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Total 1.3 MB/s | 635 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : ntpdate-4.2.6p5-29.el7.centos.2.x86_64 1/2 Installing : ntp-4.2.6p5-29.el7.centos.2.x86_64 2/2 Verifying : ntpdate-4.2.6p5-29.el7.centos.2.x86_64 1/2 Verifying : ntp-4.2.6p5-29.el7.centos.2.x86_64 2/2 Installed: ntp.x86_64 0:4.2.6p5-29.el7.centos.2 Dependency Installed: ntpdate.x86_64 0:4.2.6p5-29.el7.centos.2 Complete!
Step 4: Configure NTP Servers
NTP configuration is usually found under /etc
path. To add NTP Server information in /etc/ntp.conf file we need to open the file with our favorite vi editor and provide the NTP Server IP or hostname with server keyword as shown below. You can also add the Server information at the end of the file and then save and close the file by pressing Esc
and then :wq!
[root@localhost ~]# vi /etc/ntp.conf # For more information about this file, see the man pages # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5). driftfile /var/lib/ntp/drift # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. restrict default nomodify notrap nopeer noquery # Permit all access over the loopback interface. This could # be tightened as well, but to do so would effect some of # the administrative functions. restrict 127.0.0.1 restrict ::1 # Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst
In the steps to configure NTP Server you might also want to restrict the NTP Server access for some of the clients. This can be accomplished by using below configuration in /etc/ntp.conf
file. Here you need to replace the network information with the client network and their subnet mask information. This will restrict the given Client access.
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
Once we have the configuration file saved and if all the other entries are correct then you can go ahead and start/restart the ntpd service to updates the changes using service ntpd restart or systemctl restart ntpd command depends upon you are using SysV or SystemD.
[root@localhost ~]# systemctl restart ntpd
If you want the ntpd service to survive the system restart then you need to enable the service as well by using chkconfig or systemctl command depends on SysV or SystemD you are using.
[root@localhost ~]# systemctl enable ntpd Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
Step 5: Check NTP Synchronization Status
In the next step you can wait for the Server to sync its time with NTP Server and then check the synchronization status by using ntpq -np command as shown below.
[root@localhost ~]# ntpq -np remote refid st t when poll reach delay offset jitter ============================================================================== +162.159.200.1 10.35.14.16 3 u 3 64 7 41.941 0.085 0.706 *5.189.141.35 17.253.54.123 2 u 3 64 7 138.188 -1.540 0.852 157.119.108.165 162.159.200.123 4 u 4 64 3 16.673 -3.839 0.597
-n : Output all host addresses in dotted-quad numeric format rather than converting to the canonical host names.
-p : Print a list of the peers known to the server as well as a summary of their state. More on ntpq Man Page.
Step 6: Verify date and time
If everything went well then Your date and time should synchronized from the NTP Server. You can verify your timezone, date and time by using timedatectl command as shown below. Alternatively, you can also use date command to verify date and time.
[root@localhost ~]# timedatectl Local time: Sat 2021-01-09 10:35:27 EST Universal time: Sat 2021-01-09 15:35:27 UTC RTC time: Sat 2021-01-09 15:35:09 Time zone: America/New_York (EST, -0500) NTP enabled: yes NTP synchronized: yes RTC in local TZ: no DST active: no Last DST change: DST ended at Sun 2020-11-01 01:59:59 EDT Sun 2020-11-01 01:00:00 EST Next DST change: DST begins (the clock jumps one hour forward) at Sun 2021-03-14 01:59:59 EST Sun 2021-03-14 03:00:00 EDT
If you want to query from NTP Server and check the current offset and delay then you can do it by using ntpdate command. For example here we are querying local server clock offset and delay from CentOS NTP Server pool.ntp.org
by using ntpdate -q pool.ntp.org
command as shown below.
[root@localhost ~]# ntpdate -q pool.ntp.org server 162.159.200.123, stratum 3, offset 1172.829764, delay 0.06750 server 45.86.70.11, stratum 2, offset 1172.761198, delay 0.31595 server 95.216.192.15, stratum 2, offset 1172.831740, delay 0.17059 server 162.159.200.1, stratum 3, offset 1172.829492, delay 0.06866 9 Jan 10:53:42 ntpdate[6796]: step time server 95.216.192.15 offset 1172.831740 sec
Step 7: Remove NTP Package
Like installation you can also remove the NTP package easily from your Server by using yum remove ntp -y
command as shown below.
[root@localhost ~]# yum remove ntp -y Loaded plugins: fastestmirror Resolving Dependencies --> Running transaction check ---> Package ntp.x86_64 0:4.2.6p5-29.el7.centos.2 will be erased --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================================================== Package Arch Version Repository Size ======================================================================================================================================================================== Removing: ntp x86_64 4.2.6p5-29.el7.centos.2 @base 1.4 M Transaction Summary ======================================================================================================================================================================== Remove 1 Package Installed size: 1.4 M Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Erasing : ntp-4.2.6p5-29.el7.centos.2.x86_64 1/1 Verifying : ntp-4.2.6p5-29.el7.centos.2.x86_64 1/1 Removed: ntp.x86_64 0:4.2.6p5-29.el7.centos.2 Complete!