Tuesday, March 31, 2009

How to time sync your Debian server

There are various ways to setup timezones and time/date in Linux and the preferred method varies distribution to distribution, so today I'm going to show you how to setup the timezone in Debian (and consequently many of its derivatives.)

The base package that controls timezone in Debian is tzdata and you can use the following command to setup the package again if you made an incorrect choice when you initially setup the package (or if you just want to change your local time to a different zone.) This command needs to be run as root.

dpkg-reconfigure tzdata


Unlike many other distributions Debian expects you to pick a city that is in your current time zone rather than specifying the GMT offset. For most of the US time zones you can use one of the following if you don't notice a city closer to your location. First you will need to select your continent and then your city. For the US and Canada, America is the continent.

America/Detroit = EST/EDT
America/Chicago = CST/CDT

America/Boise = MST/MDT
America/Los_Angeles = PST/PDT


Once your timezone is setup correctly you can move on to setting the system time and date. Typically in Debian this is done with the ntpdate application which you should have installed already, if not you can install it by issuing the following command as root.

aptitude install ntpdate


The first time the program installs it will attempt to connect to the default servers for debian and set the time correctly. After the initial time it connects you can use the following command as root to re-sync the time.

ntpdate-debian


The default servers it attempts to connect to are in the /etc/default/ntpdate file in a space separated list called NTPSERVERS, if you don't want to use the default servers Debian provides you can replace them with the time servers you wish to use.

For most purposes this will keep your time synchronized accurately with a central source to make logs easier to evaluate and correlate time and date stamps between multiple systems and routers, however after the initial run it will only update the system time when the network changes state (up or down) or the machine reboots.

If you require even more accurate accounting of time you can use ntpd to keep the time synchronized consistently and with a reduced amount of jitter due to latency on the system and network.

If you wish to setup ntpdate to run on a regular basis you can setup a cron for ntpdate, for example, putting this in an executable file called ntpdate in the /etc/cron.weekly directory:

#!/bin/bash
set -e
[ -x /usr/sbin/ntpdate-debian ] || exit 0
/usr/sbin/ntpdate-debian

However, if your drift or time needs are significant enough to warrant this kind of change you should be looking at using ntpd to maintain accurate time keeping after boot time as large jumps in time can have adverse effects on the cron and system.

0 comments:

Post a Comment