Chris Jean's Blog

Linux, WordPress, programming, anime, and other stuff.
twitter
rss
Mar
03

Change Timezone in CentOS

Categories : Linux, Tips 'n Tricks
Change Timezone in CentOS

You just got your new CentOS dedicated server, and you notice that times in your logs aren’t quite right. You check the time from the command line (run “date”), and find that the timezone is set to US Eastern or some other timezone. How do you get this changed?

Unfortunately, this is not an easy thing to figure out. Fortunately though, it’s not hard to do with the right directions.

Please note that you have to have root access to be able to make the changes detailed below.

There are a series of time zone files located at /usr/share/zoneinfo. Select the appropriate named timezone for your location. For my location, Oklahoma, USA, I actually have two that I can select from: America/Chicago or US/Central. Make note of the appropriate folder and file for your timezone.

The active timezone used on your system is in the /etc/localtime file. The default will vary depending on your server host but often seems to be EST or EDT (depending on the time of year you are checking). We simply need to replace this file with the file we selected in the previous step.

Now, I say replace, but I actually recommend creating a link to the pertinent file rather than actually making a copy. Let me explain the reasoning for this quickly.

A crucial part of the timezone shift calculations is the daylight savings calculations. Many people don’t realize this, but the daylight savings days were changed in 2007. When this change happened, all servers needed to be updated with new zoneinfo files.

If your server has an automated process to update these files when daylight savings calculations change, your /usr/share/zoneinfo files will be updated but the /etc/localtime file will not be. So, if you simply made a copy of the file, you’ll have to know when these updates come out and manually copy the file over. If you create a link, everything will take care of itself.

Enough of the banter. On with the show.

First, make a backup of the existing localtime file. It’s always good practice to make backups of original config files.

mv /etc/localtime /etc/localtime.bak

Next, create the link:

ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime

Make sure to replace “America/Chicago” with the directory (if your zone has one) and filename of the timezone you wish to use.

Now you just need to test your change. Run “date” from the command line, and ensure that the appropriate time, date, and timezone are reported.

Tags: , , , , ,

Related Posts

Receive Updates

New posts on chrisjean.com delivered directly to your email.

Email:

9 Comments

  • Viet Le Quang says:

    Dear Chris,

    Thanks for the write-up, it works!

    Regards, Viet

  • oscar says:

    thanks for your help it works perfectly.

    Oscar from Chile.

  • Jonathan says:

    Thanks very much. With the help of your entries about timezones and the tzdata file I’ve been able to save myself a lot of time!

  • Paul says:

    Thank you. Two of the simplest Unix explanations I’ve ever read.

  • Many thanks. This was very clear and exactly what I needed.

  • howard says:

    Thanks, very usefull, burt a minor point for the less experienced people trying to follow the above instructions use

    mv /etc/localtime /etc/localtime.bak
    NOT
    cp /etc/localtime /etc/localtime.bak

    otherwise localtime file will still be in place and you will not be able to do the ln command.

  • Frank says:

    Thanks for the pointers. Just as a summary, here is the whole transaction to set up my computer as an ntp controlled node using UTC
    I did try this and it works great.

    [192.168.1.3:BMCST_3 /Scripts]# cat set-time.sh
    #!/bin/bash

    # Version 0.1 – FJR – A script to Set up the date and time

    service ntpd stop
    ntpdate us.pool.ntp.org
    hwclock –systohc

    mv /etc/localtime /etc/localtime.bak
    ln -s /usr/share/zoneinfo/Etc/UTC /etc/localtime

    service ntpd start

Leave a Reply