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 -eas root, orsudo crontab -e). ntpdatemust be installed. It was part of thentppackages 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 -uCorrection: 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:
/usr/sbin/ntpdate -s -b -p 8 -u 129.132.2.21So the corrected cron line is:
0 2 * * * /usr/sbin/ntpdate -s -b -p 8 -u 129.132.2.21129.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.org0 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:
/usr/sbin/ntpdate -b -p 8 -u pool.ntp.orgThen check the time with:
dateNotes 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):
chronyis the standard time service and the oldntp/ntpdatepackages are not in the main repositories. Enable it with:Terminal window sudo systemctl enable --now chronydCheck its status with
chronyc trackingand see which servers it uses withchronyc sources. To step the clock immediately (similar to whatntpdate -bdid), runsudo chronyc makestep. -
Ubuntu uses
systemd-timesyncdby default. Debian can usesystemd-timesyncdorchrony(on Debian and Ubuntu the chrony service is calledchrony). -
Any systemd system:
timedatectlshows 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.