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.
Next, create the link:
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.
Dear Chris,
Thanks for the write-up, it works!
Regards, Viet
You’re welcome Viet. I’m glad that it worked for you.
thanks for your help it works perfectly.
Oscar from Chile.
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!
Thank you. Two of the simplest Unix explanations I’ve ever read.
Many thanks. This was very clear and exactly what I needed.
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.
You’re quite right. Thanks for the correction.
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
Thanks, Frank.
Your solution work great for me.
Thanks.. It`s work the first time
This doesn’t work for me, why?
[root@lion etc]# ls -l /etc/localtime
lrwxrwxrwx 1 root root 33 Sep 19 08:03 /etc/localtime -> /usr/share/zoneinfo/Europe/London
[root@lion etc]# date
Sun Sep 19 08:07:42 EDT 2010
[root@lion etc]# /etc/init.d/ntpd restart
Shutting down ntpd: [ OK ]
Starting ntpd: [ OK ]
[root@lion etc]# date
Sun Sep 19 08:07:52 EDT 2010
Time in London at the time was 13:07
It sounds like your hardware clock may be set incorrectly. Please run the following commands and give me their output (note: you have to be root to run the
hwclockcommands):datedate --utchwclockhwclock --utcAlong with the output from those commands, please provide what the current UTC (Greenwich Mean Time) time should be.
Does this need additional steps in a multi-user system?
I have a remote server I use for hosting and I changed to UTC a while ago, no problem. I also have a desktop box sitting next to me that has CentOS installed, and that’s misbehaving. As root:
# ls -al /etc/localtime
lrwxrwxrwx 1 root root 23 Sep 28 19:26 /etc/localtime -> /usr/share/zoneinfo/GMT
# date
Tue Sep 28 20:12:51 EDT 2010
# date –utc
Wed Sep 29 00:13:05 UTC 2010
# /sbin/hwclock
Wed 29 Sep 2010 12:14:48 AM EDT -0.327488 seconds
# /sbin/hwclock –utc
Wed 29 Sep 2010 12:14:57 AM EDT -0.327488 seconds
Simply put, why would date be telling me EDT when localtime is clearly pointing to UTC?
Oh I should add that the GUI is also ineffective at this change, and swapping between UTC and GMT didn’t matter. And restarting any services or rebooting the whole machine hasn’t helped either.
OK I assumed there’s no way that should be happening so something must be wrong with the underlying file. I reinstalled tzdata and that covered it.
Beauty! Thanks. Props also for pointing out the risks associated with not using a symbolic link back to the zonefile in case of DST updates – it was in the news a little while back all up and down the East Coast of Australia folks got caught out by DST changes. Lots of web services and other software systems were an hour out of whack.
Easy to follow – updated my Linux CENTOS 5.5 pefectly! Thank You =)
yep worked great first time – thanks!!!
Thanks! Exactly what I was looking for.
Be sure to reboot your SQL server.
Otherwise it will continue to operate in the old time zone.
This can cause problems that are hard to debug.
you might just reboot the whole server
I had to manually change the time zone because our server was set up wrong. This was the fix I needed. Thanks!
[...] link: http://chrisjean.com/2009/03/03/change-timezone-in-centos/ LD_AddCustomAttr("AdOpt", "1"); LD_AddCustomAttr("Origin", "other"); [...]
worked like a charm… I was wondering why the emails sent from the website I am developing http://www.gmatx.com were showing 6 hours back . But now it works… thanks.
i tired , but was results not as expected
[root@hanishvm ~]# date
Wed Jan 4 22:07:58 TLT 2012
You have new mail in /var/spool/mail/root
[root@hanishvm ~]# mv /etc/localtime /etc/localtime.bak
[root@hanishvm ~]# ln -s /usr/shar/zoneinfo/Asia/Calcutta /etc/localtime
[root@hanishvm ~]# date
Wed Jan 4 13:11:21 UTC 2012
[root@hanishvm ~]#
It looks like you have a typo:
should be
When I tried your commands with the typo in place, I got the same results. When I fixed the path, everything worked properly.