LFCS – Network Time Protocol (NTP)

Jarret B

Active Member
Staff member
Joined
May 22, 2017
Messages
248
Reaction score
228
Credits
5,755
We covered the Network Time Protocol Daemon a little in the article ‘https://www.linux.org/threads/lfcs-–-kerberos-authentication-centos7.39296/’, as well as in ‘https://www.linux.org/threads/lfcs-–-kerberos-authentication-ubuntu.39733/’.

This article will go a little deeper and add in another Network Time Protocol other than NTP.


Different Times

Each system has different times. I’m not referring to each being a different time than others.

Each system, especially Linux, has three different times:
  1. Real-Time Clock (RTC)
  2. Local Time
  3. Universal Time
The Real-Time Clock (RTC) is the time on the system that is controlled by an Integrated Circuit (IC) that keeps time. We see the RTC value on the BIOS screen showing date and time. The true value of the RTC doesn’t include any adjustments for the Time Zone.

Looking at the Local Time, it is the adjusted time of the RTC that takes into consideration the Time Zone.

Coordinated Universal Time (UTC) is the successor to the Greenwich Mean Time (GMT). This is the mean time by which we adjust the time by the Time Zone to get the real local time.

Basically, NTP will adjust the UTC, which will then adjust the RTC on the system board. Once corrected, then the Local time will be changed according to the Time Zone settings. For example, in Figure 1 for CentOS, you can see the Time Zone is ‘America/Indiana/Indianapolis (EDT, -0400)’. The system reduces the RTC time (5:19 A.M.) by four hours (Time Zone adjustment), resulting in the local time being 1:19 A.M.

Figure 1.jpg

FIGURE 1

Figure 2 shows the output for the same command on an Ubuntu system.

Figure 2.jpg

FIGURE 2

To change your date and time manually from a terminal, you can use the command ‘date --set “yyyymmdd hh:ss”’.

Network Time Protocol (NTP)

The system manages the Network Time Protocol (NTP) with the Network Time Protocol Daemon (NTPD).

For an easy way to determine if a system is using some type of time synchronization with a Time Server, is to run the command ‘timedatectl’. One line, on CentOS, in the output lists the status of ‘NTP Enabled’ and ‘NTP Synchronized’. The result is ‘yes’ or ‘no’. A system by default should not have NTP installed or enabled.

For Ubuntu, the line is ‘System Clock Synchronized: ’. By default, it is ‘no’.

On either system, you can check the service with the command ‘sudo systemctl status ntpd’. The result should be that the service is not found or inactive.

To install the Network Time Protocol Daemon, use ‘sudo yum install ntp -y’ for CentOS and ‘sudo apt install ntp -y’ for Ubuntu.

Once installed, you need to start the service, then enable it to start on system boot. A third command can check the status. Use the following commands for CentOS:

sudo systemctl start ntpd
sudo systemctl enable ntpd
sudo systemctl status ntpd


For Ubuntu, the commands are the same, except the service name:

sudo systemctl start ntp
sudo systemctl enable ntp
sudo systemctl status ntp


On either Operating System, you can check the configuration file located at ‘/etc/ntp.conf’. About halfway down the file should be lines similar to:

pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst


For CentOS, instead of ‘pool’ it says ‘server’. The server's name does not contain ‘ubuntu’, but ‘centos’. These are the servers that are used to sync the time from the Internet when enabling NTP.

The NTP service runs on UDP Port 123. You may need to open the firewall port if you have issues with time synchronization.

Once you have the NTP Service running, you can see which server you are using as the NTP Server by using the command ‘nptq -p’. It shows a list of servers that begin with the default pool from the configuration file. The server with the asterisk (*) next to it is the one your system is using.

You should notice a column that is listed as ‘poll’. The value should start out at 64 (seconds). The service checks the servers every 64 seconds for an update to the time. After time passes and the service determines that the time is not being updated, the polling time will increase by 64 seconds until it reaches 1024 (seconds).

Another important column is the Stratum (st) column. The time servers at the highest point are atomic clocks, GPS clock, etc. These are Stratum 0 since the time is originating with them. A system that gets its time from these servers will become a Stratum 1 time system. As each system goes down the line, it adds one to the stratum number. The higher a number, maximum of 15, the more probable that the time is off from the original time server.

To stop the Network Time Protocol Service, use the commands as follows: (using ‘ntp’ for Ubuntu and ‘ntpd’ for CentOS)

sudo systemctl disable <service>
sudo systemctl stop <service>


The NTP service is good for systems that are not rebooted often, such as servers. For another option of a time service, try Chrony.

Chrony

Chrony is a replacement for the Network Time Protocol Daemon (NTPD). The Chrony service works faster and better on systems that are rebooted often, such as desktops.

To install Chrony, use the command for CentOS:

sudo yum install chrony -y

For Ubuntu, use the following:

sudo apt install chrony -y

To start and enable the service on Ubuntu, use the following commands:

sudo systemctl start chrony
sudo systemctl enable chrony


For CentOS, use:

sudo systemctl start chronyd
sudo systemctl enable chronyd


The Chrony configuration file is found at ‘/etc/chrony.conf’ for CentOS and ‘/etc/chrony/chrony.conf’ for Ubuntu.

The files should look very similar to the configuration file for NTP.

To see the basic information about the time service, use the command ‘chronyc tracking’.

If you want to see the actual servers you are pooling from and which system is the one currently being used, use the command ‘chronyc sources’. The asterisk (*) is the current server being used to sync time.

Troubleshooting

The main two points to check on when you have problems is that the service is started. The second is that DNS resolution is occurring.

If you recall from the configuration files, the time servers are listed by name and not IP Address. If Domain Name resolution is not working, then the time synchronization cannot occur.

If DNS Resolution is not happening, get that working. Restart the NTP or Chrony service and then retry the synchronization.

Conclusion

Getting all systems on the same time can be a very beneficial tool on a network. In this manner, all log files are synchronized by time on all systems. Having the times synced can help troubleshoot issues between systems.

If some systems are off by a minute or more, there can be file sharing issues, etc.

On some networks, it may be best to have one or two time servers. The desktop clients can update from these two by changing the server name in the configuration file. If we sync the local time servers from the Internet, and we then sync all desktops from these two systems, then the local network times should be synced perfectly for the administrator.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top