How to Obtain Accurate Server Time in CentOS?


Accurate server time is crucial for various aspects of system administration, including synchronization between distributed systems, logging, security measures, and troubleshooting. In CentOS, a popular Linux distribution, there are several methods to obtain accurate server time.

In this article, we will explore different approaches to ensure precise timekeeping in CentOS. We'll begin by checking the system time and verifying the time zone settings. Then, we'll dive into two common methods: using the Network Time Protocol (NTP) and the Chrony time daemon. Additionally, we'll discuss the hardware clock and its significance in maintaining accurate time.

By the end of this article, you'll have a solid understanding of how to obtain accurate server time in CentOS, enabling you to keep your systems synchronized and ensure reliable time-based operations.

Checking System Time

To ensure accurate server time in CentOS, it's important to first check the current system time and verify the time zone configuration.

Using the Date Command

The date command in CentOS allows you to display the current system time. Open the terminal and execute the following command −

date

This will display the current date and time in the default format. Verify that the displayed time corresponds to your local time.

Verifying the System Time Zone

The system time zone determines the reference point for time calculation. It's essential to ensure that the system time zone is correctly configured.

To check the system time zone, use the following command 

timedatectl show --property=Timezone

The output will display the configured time zone. Verify that it matches your intended time zone.

If the system time or time zone is incorrect, it can lead to discrepancies in various system operations. Now that we have checked the system time, let's explore the methods to obtain accurate server time using NTP.

NTP (Network Time Protocol)

NTP (Network Time Protocol) is a widely used protocol for synchronizing time across computer systems. CentOS offers built-in support for NTP, allowing you to obtain accurate server time by synchronizing with reliable time servers.

Installing NTP

To install NTP on CentOS, open the terminal and execute the following command −

sudo yum install ntp

The package manager will download and install the NTP package along with its dependencies.

Configuring NTP servers

NTP servers are responsible for providing accurate time information. By default, CentOS is configured to use the NTP Pool Project servers. However, you can modify the configuration to use specific NTP servers of your choice.

To configure NTP servers, open the /etc/ntp.conf file in a text editor −

sudo nano /etc/ntp.conf

Locate the "server" directives and update them with the IP addresses or hostnames of your preferred NTP servers. Save the file and exit the text editor.

Synchronizing Time with NTP

To synchronize the system time with the configured NTP servers, use the following command 

sudo systemctl start ntpd

This command starts the NTP service and initiates the time synchronization process. The system will contact the configured NTP servers and adjust the system time accordingly.

Verifying NTP Synchronization

To check if the system time is synchronized with the NTP servers, execute the following command −

ntpq -p

This command displays a list of peers (NTP servers) and their synchronization status. Look for the "reach" column, which indicates the reachability of the servers. A reach value of 377 indicates successful synchronization.

With this, we have successfully configured NTP and synchronized the system time with reliable time servers. In the next section, we will explore an alternative time synchronization method using the Chrony time daemon.

Chrony Time Daemon

Chrony is another time synchronization daemon available in CentOS. It provides similar functionality to NTP but with some additional features and optimizations. Let's explore how to install and configure Chrony to obtain accurate server time.

Installing Chrony

To install Chrony on CentOS, open the terminal and execute the following command 

sudo yum install chrony

The package manager will download and install the Chrony package along with its dependencies.

Configuring Chrony Servers

Chrony uses the /etc/chrony.conf configuration file to define the time servers. Open the file in a text editor 

sudo nano /etc/chrony.conf

Within the file, you'll find a section with "server" directives. Update these directives with the IP addresses or hostnames of your preferred Chrony servers. Save the file and exit the text editor.

Synchronizing Time with Chrony

To synchronize the system time using Chrony, start the Chrony service by executing the following command 

sudo systemctl start chronyd

Chrony will initiate the time synchronization process by contacting the configured Chrony servers and adjusting the system time accordingly.

Verifying Chrony Synchronization

To check the synchronization status with Chrony, use the following command 

chronyc tracking

This command displays information about the system's time sources and the current synchronization status. Look for the "Reference ID" and "Last offset" fields to ensure synchronization.

Chrony provides additional commands and options for more advanced time synchronization management. You can explore these features in the Chrony documentation.

In the next section, we'll discuss the hardware clock and its significance.

Hardware Clock and Timezone

The hardware clock, also known as the Real-Time Clock (RTC), is a clock built into the computer's hardware that keeps track of time even when the system is powered off. CentOS uses the hardware clock to initialize the system time during startup.

Hardware Clock and UTC

By default, CentOS assumes that the hardware clock is set to Coordinated Universal Time (UTC). It then applies the system's configured time zone offset to derive the local time.

To check the hardware clock time in UTC, execute the following command −

sudo hwclock --show --utc

Configuring Hardware Clock

If your hardware clock is not set to UTC, you can adjust it accordingly. First, set the hardware clock time to UTC using the following command 

sudo hwclock --systohc --utc

This command updates the hardware clock based on the system time.

Timezone and Hardware Clock

The system's timezone affects how the hardware clock is interpreted during startup. By default, CentOS assumes the hardware clock is set to UTC and applies the configured timezone offset.

If you want the hardware clock to be interpreted as local time, use the following command 

sudo timedatectl set-local-rtc 1

To revert to interpreting the hardware clock as UTC, use the command 

sudo timedatectl set-local-rtc 0

NTP and Hardware Clock

When using NTP or Chrony for time synchronization, the system time is adjusted without modifying the hardware clock. The time synchronization process ensures accurate time within the operating system, while the hardware clock remains unchanged.

Understanding the interaction between the hardware clock and time synchronization is important for maintaining accurate time in CentOS.

Conclusion

Accurate server time is crucial for various applications and services running on CentOS. In this guide, we explored different methods to obtain accurate server time.

We started by discussing the Network Time Protocol (NTP) and its usage for time synchronization. We then explored the Chrony time daemon as an alternative option.

Additionally, we covered the configuration of the hardware clock and its interaction with the system's timezone. Understanding these aspects is essential for maintaining accurate time on your CentOS server.

Updated on: 09-Aug-2023

144 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements