Home > Linux > Periodically synchronize time in Linux

Periodically synchronize time in Linux

We will use the ntp and ntpdate packages to achieve this.
These packages should already be installed on most linux systems. If not you can install them by using the below

# on RPM based Red Hat, Fedora, Cent OS
 sudo yum install ntpdate
# on APT based Debian, Ubuntu
 sudo apt-get install ntpdate

Create a log file in the tmp directory to see the time sync data

touch /tmp/ntpdate.log

Create a bash script to sync time called ntpdate and put the below into it

# sync server time
/usr/sbin/ntpdate pool.ntp.org >> /tmp/ntpdate.log

You can place this script anywhere you like and then set up a cron
I will be putting it into the daily cron directory so that it runs once every day
So my ntpdate script is now in /etc/cron.daily/ntpdate and it will run every day

Make this script executable

chmod +x /etc/cron.daily/ntpdate

Test it by running the script once and look for some output in /tmp/ntpdate.log


In your log file you should see something like

26 Aug 12:19:06 ntpdate[2191]: adjust time server offset 0.272120 sec

Monitor this log for some days to make sure the cron is running fine.
If you need to sync more frequently, you can edit the cron tab and run the script every hour

If this does not work, you can also update the script to ( change paths as required )

# sync server time
echo "------------------------------------------------------------------------"
/sbin/service ntpd stop
/usr/sbin/ntpd -gq
/sbin/service ntpd start


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: