Use cron and ntpdate to keep your linux server’s time updated

To sync your linux server’s time with the NTP Pool Time Servers, use the following command.

ntpdate pool.ntp.org

If you’d like to sync with a time server weekly, add a new file named ntpdate-sync to /etc/cron.weekly. Then set the file permissions to 755 and set owner:group to root:root. Finally, exit set the file contents to the following.

#! /bin/sh
/usr/sbin/ntpdate pool.ntp.org

To sync on a custom schedule, add a new file named ntpdate-sync to /etc/cron.d. Then set the file permissions to 644 and set owner:group to root:root. Finally set the file contents as follows.

# /etc/cron.d/ntpdate-sync: crontab fragment for ntpdate
# Run ntpdate at 2:30AM on the first of every month
30 2 1 * * root /usr/sbin/ntpdate 2>&1 | /usr/bin/tee -a /var/log/cron/ntpdate-sync.log

Update – 10/18/09
Changed custom schedule cron example from once every 15 minutes to once per month.

2 Responses to “Use cron and ntpdate to keep your linux server’s time updated”

  1. Shane Bishop says:

    Please don’t tell people to run ntpdate via a cronjob. If it’s just once a week, fine. But especially don’t recommend doing it every 15 minutes. It’s a frequent bone of contention for ntp server operators, because people doing this causes floods of ntp traffic all at once. That in turn causes the time returned by the server to be less accurate than it could be, and therefore less useful to the users in the long run.

    Instead, people should be running ntpd. On Ubuntu, this is as simple as installing the ntp package (the default config is fine for most folks). It picks random intervals to sync time, and can also compensate for regular clock skew, avoiding large jumps in time if you have a particularly bad clock. It is also very lightweight for those who are paranoid about having yet another process running on their computers.

  2. CHEMiST says:

    Good point. My suggestion is to run ntpdate on a weekly basis. The second cron was simply an example but could have been misleading. I’ve updated the post to clear up any confusion.

    Really appreciate the feedback, I’ll give ntpd a dig.

Leave a Reply