Set Up Time Synchronization on CentOS 7 | Easy Steps

Posted on

Set Up Time Synchronization on CentOS 7 | Easy Steps

In this tutorial, brought to you by Orcacore, we’ll guide you through the process of how to Set Up Time Synchronization on CentOS 7. Time synchronization is crucial for ensuring the consistency and accuracy of time across different systems. Clocks, especially those relying on inexpensive components, are prone to drifting over time, potentially causing discrepancies that can lead to serious issues in computing environments.

Even if initially synchronized, clocks will diverge significantly over time. In modern computing, such clock drift can present a significant problem, impacting everything from log file analysis to coordinated distributed system operations.

  • Importance of Accurate Time
  • Potential Issues with Clock Drift
  • Introduction to NTP

Network Time Protocol (NTP) was specifically designed to address this issue. It’s a protocol used to synchronize the clocks of computer systems over a network. NTP is one of the oldest Internet protocols still in widespread use today, and it provides a reliable and robust mechanism for maintaining accurate time.

Before we begin, ensure you are logged into your CentOS 7 server as a non-root user with sudo privileges. If you need assistance with this, refer to our guide on the Initial Server Setup with CentOS 7.

Now, let’s move on to the steps required to Set Up Time Synchronization on CentOS 7.

1. Check Current Time Zone And Clock on CentOS 7

The first step in the process is to determine the current time zone and clock settings on your CentOS 7 server.

The simplest way to check the current time on your server is by using the date command. Any user can execute this command:

sudo date
**Output**
Sat Oct  1 04:35:48 EDT 2022

Note: By default, your server will typically be configured to use the UTC (Coordinated Universal Time) time zone. UTC is the time at zero degrees longitude. Using UTC is often recommended to reduce confusion, especially when managing infrastructure spanning multiple time zones.

If you need to change the time zone on your CentOS 7 server to match your specific location or requirements, you can use the timedatectl command.

First, list the available time zones using the following command:

sudo timedatectl list-timezones

This command will output a long list of available time zones.

Once you’ve identified the correct time zone, you can set it using the following command, replacing America/New_York with your desired time zone:

sudo timedatectl set-timezone America/New_York

Now that you know how to check and adjust the time zone and clock on CentOS 7, let’s proceed with installing and configuring NTP to ensure accurate time synchronization.

2. Install NTP and Check the ntpd Status on CentOS 7

To Set Up Time Synchronization on CentOS 7, we need to install the NTP package.

Install NTP using the following yum command:

sudo yum install ntp -y

After the installation is complete, start and restart the ntpd service:

sudo systemctl restart ntpd

To verify that the ntpd service is active and running, use the following command:

sudo systemctl status ntpd

The output should indicate that the service is active and running, similar to the following:

Check NTP Service Status

For more detailed information about the status of ntpd and its synchronization sources, you can use the ntpq -p command:

ntpq -p

The output will display a list of NTP servers that your server is synchronizing with, along with their status and offset information. Your output may vary.

If everything is configured correctly, your server’s date and time should now be synchronized with the NTP servers. To verify this, you can use the timedatectl command again:

timedatectl

The output should show "NTP synchronized: yes", indicating that the time on your CentOS 7 server has been successfully synchronized. "NTP enabled: yes" means that timesyncd is enabled and running.

Conclusion

In conclusion, Set Up Time Synchronization on CentOS 7 is a vital aspect of server management. By following these steps, you can ensure that your CentOS 7 server maintains accurate time, which is crucial for various applications and services. You have learned how to check the current time, change the time zone, install NTP, and verify its status. This knowledge will help you keep your system’s time synchronized for optimal performance.

We hope you found this guide helpful. Please follow us on Facebook, Instagram, and YouTube.

You might also find these guides useful:

Alternative Methods for Time Synchronization on CentOS 7

While NTP via ntpd is a reliable method, here are two alternative approaches to achieving time synchronization on CentOS 7:

1. Using Chrony

Chrony is another popular NTP client that can be used instead of the traditional ntpd daemon. Chrony is designed to synchronize the system clock more quickly and accurately, especially in environments where the server is not always connected to the internet or experiences intermittent network connectivity. It is also designed to work well on virtual machines.

Explanation:

Chrony achieves faster synchronization by making use of advanced algorithms that analyze the rate at which the system clock drifts and compensate for it. This makes Chrony more robust in unstable network conditions.

Installation and Configuration:

First, stop and disable ntpd:

sudo systemctl stop ntpd
sudo systemctl disable ntpd

Then, install Chrony:

sudo yum install chrony -y

Start and enable the Chrony service:

sudo systemctl start chronyd
sudo systemctl enable chronyd

Verify that the service is running:

sudo systemctl status chronyd

To check the synchronization status, use the chronyc sources command:

chronyc sources

This command will display a list of NTP servers Chrony is using to synchronize the time. The chronyc tracking command gives more detailed information on the current time offset, frequency, and other synchronization parameters.

chronyc tracking

Chrony configuration is typically done in the /etc/chrony.conf file. Here, you can specify the NTP servers to use, allow clients to synchronize from your server, and configure other settings. By default, Chrony often uses the same public NTP servers as ntpd, so in many cases, the default configuration will suffice.

2. Using systemd-timesyncd

systemd-timesyncd is a simpler NTP client that is part of the systemd system and service manager. It is designed to be lightweight and easy to configure, making it suitable for systems where full-fledged NTP functionality is not required. It is already installed on most modern systems using systemd.

Explanation:

systemd-timesyncd is less feature-rich than ntpd or Chrony, but it is perfectly adequate for basic time synchronization needs. It periodically queries NTP servers and adjusts the system clock accordingly.

Configuration:

systemd-timesyncd is typically enabled by default on systems running systemd. To check its status, use the following command:

sudo systemctl status systemd-timesyncd

If it’s not running, start and enable it:

sudo systemctl start systemd-timesyncd
sudo systemctl enable systemd-timesyncd

To check the current time synchronization status, use the timedatectl status command:

timedatectl status

Look for the lines indicating "NTP synchronized" and "NTP service".

systemd-timesyncd uses the /etc/systemd/timesyncd.conf file for configuration. You can specify NTP servers in this file if you want to use specific servers instead of the default ones. For example:

[Time]
NTP=ntp.example.com ntp2.example.com
FallbackNTP=pool.ntp.org

After making changes to the configuration file, restart the service:

sudo systemctl restart systemd-timesyncd

While systemd-timesyncd is simpler, it might not be suitable for environments that require high precision or have complex network configurations.

These two alternative methods provide viable options for time synchronization on CentOS 7, depending on the specific requirements and complexity of your environment.