Set NTP Service in Linux Instance

Last Updated At: 2025-10-21 09:10:00

Overview

The Network Time Protocol daemon (NTPD) is a daemon of the Linux operating system. It is used to correct the time between the local system and the clock source server and fully implements the NTP protocol. The difference between NTPD and NTPDate is that NTPD gradually corrects the time in a step-by-step manner without any time jump, while NTPDate is a breakpoint update. This document takes the CentOS7.5 operating system Cloud Virtual Machine as an example to introduce how to install and use NTPD.

Notes

  • Some operating systems use chrony as the default NTP service. Make sure that NTPD is running and set to start at boot.
  • Use systemctl is-active ntpd.service command to check whether NTPD is running.
  • Use systemctl is-enabled ntpd.service command to check whether NTPD starts up at boot.
  • The communication port of the NTP service is UDP 123. Before you set up the NTP service, make sure that you have opened UDP port 123.
    If the port is not open, see Add Security Group Rules to allow access.

Directions

Installing NTPD

Execute the following command to check whether NTPD is installed.

rpm -qa | grep ntp
  • If the result resembles the following is returned, NTPD has been installed.
    Determine whether ntpd is installed
    Determine whether ntpd is installed

  • If NTPD is not installed, use yum install ntp to install NTPD.

    yum -y install ntp
    

By default, NTPD runs in client mode.

Configure NTP

  1. Execute the following command to open the NTP service configuration file.

    vi /etc/ntp.conf
    
  2. Press i to switch to the edit mode, find the server-related configuration, modify the server to the target NTP clock source server you need to set, and delete the NTP clock source server that is not needed temporarily. As shown below:
    Server settings
    Server settings

  3. Press Esc, enter :wq to save the file and return.

Start NTPD

Execute the following command to restart the NTPD service.

systemctl restart ntpd.service

Check NTPD Status

According to actual needs, execute the following different commands to check the NTPD status.

  • Execute the following command to check whether the NTP service port UDP port 123 is being monitored normally.

    netstat -nupl
    

If the result resembles the following is returned, the monitoring is normal.
netstat -nupl
netstat -nupl

  • Execute the following command to check whether the NTPD status is normal.

    service ntpd status
    

If the result resembles the following is returned, the NTPD status is normal.
ntpd status
ntpd status

  • Execute the following command to check whether NTP is started normally and whether it is configured to the correct NTP clock source server.

    ntpstat
    

Output the IP address of the current NTP clock source server. This IP address should be the IP address of the NTP clock source server configured above. As shown below:

You can also execute nslookup domain name command to obtain the IP address corresponding to the domain name.

  • Execute the following command to obtain more detailed NTP service information.

    ntpq -p
    

The result resembles the following is returned:

  • remote: The name of the NTP server that responded to this request.
  • refid: The upper-level NTP server used by the NTP server.
  • st: The level of the remote server. Servers are ranked from 1 to 16 from high to low. In order to reduce load and network congestion, it is generally recommended to avoid connecting directly to servers ranked 1.
  • when: The number of seconds since the last successful request.
  • poll: The interval at which the local machine and remote server are synchronized (in seconds). When NTP is initially run, the poll value will be relatively small and the frequency of sync with the server will increase. It is recommended to adjust to the correct time range as soon as possible. After adjustment, the poll value will gradually increase and the sync frequency will decrease accordingly.
  • reach: The octal value, used to test whether the server can be connected. The reach value will increase with each successful connection.
  • delay: The round trip time from the local machine to send sync requests to the NTP server.
  • offset: The time offset between the host and the synchronized time source through NTP clock sync, in milliseconds (ms). The closer the offset value is to 0, the closer the time between the host and the NTP server is.
  • jitter: The value used for statistics. Calculates the distribution of offsets in a specific number of consecutive connections. That is, the smaller the absolute value of the jitter value is, the more accurate the host time is.

Set NTPD to Start at Boot

  1. Execute the following command to set NTPD to start at boot.

    systemctl enable ntpd.service
    
  2. Execute the following command to check whether chrony is set to start at boot.

    systemctl is-enabled chronyd.service
    

If chrony is set to start at boot, execute the following command to remove chrony from the startup.
The chrony conflicts with NTPD, which may cause NTPD to fail to start.

systemctl disable chronyd.service