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?
hello and thank you. How to restore this backup? localtime.bak
Simply flip the
mv
command arguments in the article. Before doing this, you will likely need to remove the existing/etc/localtime
file or link. The following example shows removing the current/etc/localtime
file and moving the backup file back.thank you very much 😉
Perfect. Thanks!
Worked perfectly!
Thanks a lot.
Thanks for the tutorials
Thanks! I had issues remembering how to do this. Had to set it up on a backup server.
Thank you, incredibly clear.
Thanks for the writeup! Thanks to ntpd, my time was already correct, but linking to the correct time zone made everything perfect here on CentOS 7 🙂
[…] Sumber: https://chrisjean.com/change-timezone-in-centos/ […]
thanks for the write up! works great, and thanks for the explanation as well.
Don’t forget about /etc/sysconfig/clock, otherwise you may find the OS changes it back on you at some point in the future.
Great, thanks for the help. Works fine.
Just as a heads up, on CentOS 6 if you set the timezone by using a soft link, when glibc gets updated it will reset the timezone to UTC.
Thank you so much, made my day!
In CentOS 7, it’s easier to use “timedatectl” to set the timezone:
timedatectl set-timezone America/Los_Angeles
Make sure you update tzdata
yum update tzdata
Thanks good info.
This still helpful nowadays. Thanks a lot for sharing and posting 🙂
Hi Valdo
If you are running on an RPM based system such as RedHat, Centos or Oracle Linux, you will find that this method is storing up problems for you in the future. The next time you update your system and get a new version of glibc you will find that your timezone has reverted back to what it was.
The correct method on a modern RPM based install is to edit /etc/sysconfig/clock and then run /usr/sbin/tzdata-update.
I’ve just learnt this one the hard way!
Hope this helps,
Tim
[…] Reference: https://chrisjean.com/change-timezone-in-centos/ […]
[…] caranya di artikel om Chris ini. Ternyata caranya lebih simple dan cukup lucu sih. Hayo, gimana? Gini nih […]