Localize Ubuntu Installs To Your Timezone

by Stephen Fluin 2012.03.14

One of the common tasks with a new Ubuntu server setup is to localize the server to a timezone.  Timezones are used by the filesystem for timestamps, they are used by PHP to perform date lookups, and by any databases when doing date comparisons based on NOW().

The way GNU/Linux, and Ubuntu in particular store time is by keeping the system clock synchronized to UTC (Universal Coordinated Time), and then storing an offset to the localized timezone.  To set this up in Ubuntu, just install the tzdata with the following command:

sudo apt-get install tzdata

After running this command and entering your sudo password, you will be prompted for which geographic region you are in, and then prompted for a selection of timezones.  After selecting the correct timezone for your server (for me, I like to keep my servers localized to my local time), you will need to restart any of the applications that use this. For a standard webserver, you will need to run the following:

sudo apache2ctl restart
sudo service mysql restart

Synchronizing and Updating the Server Time

Over time, the clock will get out of sync with the true passage of time (measured by atomic clocks accross the globe, by our GPS system, and by several publicly available network time pools).  Ubuntu makes it very easy to resync your system clock to the network time pool. In stock GNU/Linux, this is achieved by running the ntpdate application and supplying a network time pool. On Ubuntu it's even easier, you can just run sudo ntpdate-debian which will sync your system against the debian-specified time servers.

You can put this command in a crontab to frequently resync your clock. Syncing the clock takes very little network or CPU. My experience shows that depending on the quality of the hardware clock, your system will get a second out of date every few days, or every few weeks.