Keep the time and date of your Linux box always up to date

The attached file is a simple bash script, to correct the date and time of your linux box (binaries paths are relative to a Debian Etch).


  • ntpdate: apt-get install ntpdate
  • ntpd stopped: if you have a ntp daemon running you don’t need this script
  • port 123 (UDP) opened
  • user permission: the user that run the script must have the permission to set up the clock

What the script does

The script try to update the date/time of your machine, contacting a list of sntp public servers, until one of them replies successfully.
If none of them is reachable or the ntpdate command fails (eg: if ntpd is running), the script sends a mail with the log.

Remember to modify (line 39) the charset of the mail command according to your own, the subject if you like and of course the email address.
You can also modify the addresses of the sntp server or the order in the list (lines 8-24).

Leave a Reply

Your email address will not be published. Required fields are marked *