rdate Command in Linux

Introduction

In this tutorial, you'll discover how to leverage the rdate command for precise time synchronization on your Linux system using a remote Network Time Protocol (NTP) server. We'll delve into understanding the functionality of the rdate command, walk through synchronizing your system time with a reliable NTP server, and explore how to automate this essential process with Cron. This guide is designed to equip systemadmin professionals with the knowledge to maintain accurate system time across their Linux infrastructure.

We'll begin by dissecting the rdate command, illustrating its role in setting the system clock by fetching the current time from a designated NTP server. You will then learn how to synchronize your system's time with a remote NTP server, such as time.nist.gov, and verify the updated time. Finally, you will master the art of automating time synchronization using Cron, a robust time-based job scheduler that ensures your system clock remains perpetually accurate, a crucial aspect of systemadmin best practices.

Understand the rdate Command

This section focuses on the rdate command, a vital tool for any systemadmin seeking to maintain synchronized system time with a remote Network Time Protocol (NTP) server.

The rdate command is a utility that empowers you to set the system clock on a Linux or Unix-like operating system. It accomplishes this by retrieving the precise current time from a remote server. This is invaluable when your system's clock drifts or requires synchronization with a trusted reference time source, a common concern for systemadmin professionals.

To execute the rdate command, simply enter the following command in your terminal:

sudo rdate -s time.nist.gov

This will align your system clock with the current time broadcast by the time.nist.gov NTP server.

Example output:

Thu Jan  1 00:00:00 UTC 2023

The -s option instructs rdate to set the system clock to the time obtained from the remote server. Feel free to substitute time.nist.gov with any accessible NTP server, such as pool.ntp.org or a server provided within your organization. A key skill for any systemadmin.

To confirm the current system time, use the date command:

date

Example output:

Thu Jan  1 00:00:00 UTC 2023

As the output demonstrates, the system time now reflects the time supplied by the remote NTP server.

Synchronize System Time with Remote NTP Server

This section guides you through the process of synchronizing your system's time with a remote NTP server utilizing the rdate command.

First, let's examine the present system time:

date

Example output:

Thu Jan  1 00:00:00 UTC 2023

Now, let's synchronize the system time with the time.nist.gov NTP server:

sudo rdate -s time.nist.gov

Example output:

Thu Jan  1 00:00:00 UTC 2023

To validate that the system time has been accurately updated, execute the date command once more:

date

Example output:

Thu Jan  1 00:00:00 UTC 2023

As confirmed by the output, the system time is now in sync with the remote NTP server, demonstrating a core systemadmin task.

Alternatively, you can employ the ntpdate command for time synchronization. Similar to rdate, ntpdate offers expanded options for enhanced precision. Observe this example:

sudo ntpdate time.nist.gov

Example output:

1 Jan 00:00:00 ntpdate[12345]: adjust time server 192.168.1.100 offset 0.123456 sec

The ntpdate command fine-tunes the system time based on the offset from the remote NTP server, resulting in superior time synchronization accuracy.

Automate Time Synchronization with Cron

In this section, you'll discover how to automate the time synchronization process seamlessly using the Cron scheduler, a powerful tool in the systemadmin arsenal.

Cron is a time-triggered job scheduler intrinsic to Unix-like operating systems. It empowers you to execute scripts or commands at predetermined intervals. By configuring a Cron job dedicated to regular system time synchronization, you can ensure your system's clock remains accurate and synchronized, crucial for log accuracy and other time-sensitive processes managed by a systemadmin.

First, let's craft a script that leverages the rdate command to synchronize the time:

sudo nano ~/project/sync_time.sh

Populate the script with the following content:

#!/bin/bash
sudo rdate -s time.nist.gov

Save the file and exit.

Next, grant the script executable permissions:

chmod +x ~/project/sync_time.sh

Now, schedule a Cron job to execute the script every hour:

sudo crontab -e

Append the following line to the crontab:

0 * * * * /home/labex/project/sync_time.sh

This will execute the sync_time.sh script at the beginning of every hour (0 minutes past the hour), showcasing effective systemadmin automation.

To verify the successful execution of the Cron job, scrutinize the system logs:

sudo tail -n 10 /var/log/syslog

Search for entries related to the rdate command. The presence of these entries confirms that time synchronization is occurring as scheduled.

Summary

This tutorial has illuminated the power of the rdate command in synchronizing system time with a remote Network Time Protocol (NTP) server. We began with a foundational understanding of the rdate command, learning how to set the system clock using an NTP server such as time.nist.gov. Next, we validated the time synchronization by using the date command. We have also explored the ntpdate command as an alternative for synchronizing time on your Linux system.

400+ Linux Commands