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.
[gaarai@server ~]$ sudo mv /etc/localtime /etc/localtime.bak
Next, create the link:
[gaarai@server ~]$ sudo 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.
Did I help you?
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.
Although using link force would also overcome this (as the target will be overwritten):
ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime
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 Frank!
Your additional steps, before and after the great info by the OP were what I needed to finish the job.
Wayne
I’m not sure if it is on all systems, but on CentOS 6.3 you have to use two dashes in that hwclock command:
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
hwclock
commands):date
date --utc
hwclock
hwclock --utc
Along 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.
Hi,
Used to instructions to correct my timezone setting from the default GMT to my timezone.
worked perfectly, now all my charts and alerts make sense
much less hassle than the advice posted on nagios!
Works like a charm! Thanks a lot!
Thank you very much sir, works great.
Great post. Worked perfectly me. Thanks and lot !!!
I followed the instructions to a tee and when I subsequently rebooted it
automatically rebooted 3 more times, just as it got to something
(too quick to read but it involved the word “clock”) before it finally settled down
and I got my login prompt.
WTF just happened? Thanks.
Scratchy
Cent OS 5.8
No GUI, for Asterisk/app_rpt ONLY.
Sorry, I’ve never experienced anything like that. Without a more precise message, I could only guess what the cause could have been.
Scratchy,
Are you still run the app_rpt? I am trying to set up my repeater and cannot find a lot of information that I need.
Thanks,
Mark W4GVX
Hi,
A command:
tzselect
works fine for me on Centos 6.2:
The tzselect program asks the user for information about the current location, and outputs the resulting timezone description to standard out-put. The output is suitable as a value for the TZ environment variable.
Thanks!
awesome. thanks heaps.
Sweet and simple, just how I like my linux commands. Thanks much much much!
Clear and concise, thanks for it
Thanks. Its sweet and simple.
Simply works without any hiccups.
Thanks you for this guide, this is very helpful
[…] Reference : http://chrisjean.com/2009/03/03/change-timezone-in-centos/ […]
This made it a lot easier, thanks. I’m hoping this change in server time will reflect on Awstats so the reports are in sync with my local time.
Boom, thanks!
Great write up, with cloud servers and VPS’s everywhere these days people sometimes forget.
This advice is incorrect!
yum update will “splat” your changes and return you to UTC.
See https://www.centos.org/forums/viewtopic.php?t=3589
Is /etc/localtime a symlink to a file in /usr/share/zoneinfo? It shouldn’t be, rm the symlink and copy the correct file to /etc/localtime. What happens if the file is a symlink is that an updated package copies what it thinks is the correct file and splats over the top of the one you’ve pointed to, overwriting it with the wrong file. You’ll need to `yum reinstall tzdata` after correcting the symlink to get the correct file in place and then probably recopy it to /etc/localtime. Also review the /etc/sysconfig/clock file and make sure that it now contains the correct name of the zone file you really want it to use.
I agree with Ralph. Whether you copy or link is just a matter of style, but the fact is that this is no longer a suitable method for setting the timezone on an RPM based system. The trouble is that many people remember it from the old days, and are getting caught out.
We are currently patching a whole load of systems, and that includes an update to glibc. When you use yum to re-install glibc-common it triggers a tzdata refresh, which will over-write /etc/localtime, whether it is a file or a symbolic link.
[tjinkers@ebl-oel6vm1 ~]$ rpm -q –triggeredby tzdata
glibc-common-2.12-1.166.el6_7.7.x86_64
[tjinkers@ebl-oel6vm1 ~]$
The correct method in an RPM based system is to update /etc/sysconfig/clock and then run /usr/sbin/tzdata-update.
Thanks Chris, it works
[…] http://chrisjean.com/2009/03/03/change-timezone-in-centos/ […]
Worked perfect! Saved me alot of time. Thank you !
Hi,
I tried changing the timezone but it gives me wrong time.
The time is 6 hours ahead where as the current IST time is 15:25
[root@Voice-Server ~]# date
Wed Aug 20 19:52:39 IST 2014
Based upon the output, the timezone is correct but the clock needs to be updated. You can use ntp to automatically update the system clock from the network (this can be scheduled so that the clock is automatically synced on a schedule). You can also manually set the clock using the
hwclock
command as shown here.Thanks for the great write-up! I got an error creating the link because I didn’t remove the old localtime file (rm -f localtime). The error was: ln: creating symbolic link `/etc/localtime’: File exists
So delete the old file before creating the link
Vielen Danke!
Es war perfekt.
Just wanted to let you know this article is still helping people out. Thanks.
Tnks
It’s very helpfull
Awesome! Thanks a lot.
Thank you.
Very straightforward explanation and procedure.
Noticed that the hardware clock time was different from the system time. Used the following to sync both times:
#hwclock –systohc
Hi Chris,
Thanks for your help.
With the above guidance, i resolved a issue for my customer.
Durai
Chris you are amazing!!!
Thx
Thanks! that’s a quick way to update timezone