ag@linux:~$

history 22 of 36

Use Cron to Sync the System Time with ntpdate

A daily cron job that syncs the clock with ntpdate, with each option explained, the missing server fixed, and today's chrony and timesyncd alternatives.

Written

From my Linux study notes, 2015. Some commands and versions here are old, so please reuse them carefully. Anything marked "added later", and the notes at the end, explain what has changed since.

Before you start

  • Setting the clock needs root, so add this job to root’s crontab (crontab -e as root, or sudo crontab -e).
  • ntpdate must be installed. It was part of the ntp packages on older CentOS and Red Hat systems.
  • Your server must be allowed to reach an NTP server on UDP port 123.

If you are new to cron, see How to Create a Cron Job with crontab for what the five time fields mean.

Sync the time once a day at 02:00

The original cron line:

0 2 * * * /usr/sbin/ntpdate -s -b -p 8 -u

Correction: this line is missing the NTP server to sync with, so ntpdate has nothing to contact and the job fails. The server must go at the end of the command. The original post gave the full command with a server:

Terminal window
/usr/sbin/ntpdate -s -b -p 8 -u 129.132.2.21

So the corrected cron line is:

0 2 * * * /usr/sbin/ntpdate -s -b -p 8 -u 129.132.2.21

129.132.2.21 is the server the original post used. I cannot confirm that it still answers NTP requests, so today it is safer to use the public NTP pool:

0 2 * * * /usr/sbin/ntpdate -s -b -p 8 -u pool.ntp.org

0 2 * * * means minute 0, hour 2, every day of the month, every month, every day of the week: 02:00 every day.

What the options mean

Option Meaning
-s Send output to the system log (syslog) instead of the screen, which suits cron.
-b Step (jump) the clock to the right time instead of slowly adjusting it.
-p 8 Take 8 samples from the server (the maximum) for a more accurate result.
-u Use an unprivileged (high-numbered) source port for outgoing packets, which helps behind a firewall that blocks incoming traffic to privileged ports.

Check it worked

Run the command once by hand as root, without -s, so you can see the result on screen:

Terminal window
/usr/sbin/ntpdate -b -p 8 -u pool.ntp.org

Then check the time with:

Terminal window
date

Notes for today

A once-a-day jump is a basic approach: the clock can drift during the day, and stepping the time can confuse programs that expect it to move smoothly. Modern systems normally run a time service that keeps the clock synced all the time, instead of a cron job with ntpdate. The NTP project itself has long described ntpdate as due to be retired, with its job taken over by the time service.

  • Rocky Linux, AlmaLinux and other Red Hat-family systems (version 8 and later): chrony is the standard time service and the old ntp/ntpdate packages are not in the main repositories. Enable it with:

    Terminal window
    sudo systemctl enable --now chronyd

    Check its status with chronyc tracking and see which servers it uses with chronyc sources. To step the clock immediately (similar to what ntpdate -b did), run sudo chronyc makestep.

  • Ubuntu uses systemd-timesyncd by default. Debian can use systemd-timesyncd or chrony (on Debian and Ubuntu the chrony service is called chrony).

  • Any systemd system: timedatectl shows whether the clock is synchronised and whether network time is turned on. Turn network time on with:

    Terminal window
    sudo timedatectl set-ntp true

Run only one time service at a time; do not combine a cron ntpdate job with chrony or timesyncd.