How to install and configure NTP server in RHEL7

Let’s create the VM first:

[root@rhel7 ~]# virt-install \
> --hvm \
> --name ntp \
> --ram 1024 \
> --disk path=/kvm/ntp.img,size=50 \
> --vcpus 1 \
> --os-type linux \
> --os-variant rhel7 \
> --network bridge=virbr0 \
> --graphics none \
> --location 'http://192.168.122.3/rhel7' \
> --extra-args "ks=http://192.168.122.3/ksfiles/rhel7-minimal-ks.cfg \
> console=tty0 console=ttyS0,115200 SERVERNAME=ntp.jefrey.io IPADDR=192.168.122.4"

Starting install...
...
...
[root@ntp ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.122.4  netmask 255.255.255.0  broadcast 192.168.122.255
        inet6 fe80::5054:ff:fe1e:123f  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:1e:12:3f  txqueuelen 1000  (Ethernet)
        RX packets 2636  bytes 176249 (172.1 KiB)
        RX errors 0  dropped 35  overruns 0  frame 0
        TX packets 955  bytes 70327 (68.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@ntp ~]#
[root@ntp ~]# vi /etc/sysconfig/network
[root@ntp ~]# cat !$
cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=ntp.jefrey.io
GATEWAY=192.168.122.1
GATEWAYDEV=eth0
[root@ntp ~]#

Install the package, edit the configuration file, make it start on boot and check if it’s syncing:

[root@ntp ~]# yum -y install ntp
[root@ntp ~]# 
[root@ntp ~]# chkconfig ntpd on
Note: Forwarding request to 'systemctl enable ntpd.service'.
ln -s '/usr/lib/systemd/system/ntpd.service' '/etc/systemd/system/multi-user.target.wants/ntpd.service'
[root@ntp ~]# 
[root@ntp ~]# vi /etc/ntp.conf 
[root@ntp ~]# 
[root@ntp ~]# grep -v '#' /etc/ntp.conf | sed '/^$/d'
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1 
restrict ::1
restrict 192.168.122.0 mask 255.255.255.0 nomodify notrap
server 0.sg.pool.ntp.org
server 1.sg.pool.ntp.org
server 2.sg.pool.ntp.org
server 3.sg.pool.ntp.org
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
logfile /var/log/ntp.log
[root@ntp ~]# 
[root@ntp ~]# service ntpd restart
Redirecting to /bin/systemctl restart  ntpd.service
[root@ntp ~]# ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 54.251.61.122   .INIT.          16 u    -   64    0    0.000    0.000   0.000
 203.174.83.202  .INIT.          16 u    -   64    0    0.000    0.000   0.000
 103.233.241.1   .INIT.          16 u    -   64    0    0.000    0.000   0.000
 128.199.169.185 .INIT.          16 u    -   64    0    0.000    0.000   0.000
[root@ntp ~]# 
[root@ntp ~]# 
[root@ntp ~]# ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 54.251.61.122   137.189.4.10     2 u    8   64    1   15.034   -8.561   0.000
 203.174.83.202  133.100.10.8     2 u    8   64    1   15.865  -14.194   0.000
 103.233.241.1   128.227.205.3    2 u    8   64    1   28.849  -20.912   0.000
*128.199.169.185 187.253.153.32   2 u    8   64    1   14.592  -13.859   0.000
[root@ntp ~]#

My server is syncing!

Add the port to firewalld so clients can sync:

[root@ntp ~]# firewall-cmd --zone=public --add-port=123/udp --permanent
success
[root@ntp ~]# firewall-cmd --reload
success
[root@ntp ~]#

Should you wish to configure an NTP client manually, here’s the main stuff that needs to be completed:

# yum -y install ntp
# cp /etc/ntp.conf /etc/ntp.conf.orig
# > /etc/ntp.conf
# vi /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1 
restrict ::1
server ntp.jefrey.io
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
logfile /var/log/ntp.log

# firewall-cmd --add-service=ntp --permanent
# firewall-cmd --reload
# systemctl start ntpd
# systemctl enable ntpd
# systemctl status ntpd

It should be syncing to the NTP server.