Here’s what you need to know when you want to change the timezone on a CentOS or Red Hat Enterprise Linux server.
1. Change your timezone
# mv /etc/localtime /etc/localtime.backup # ln -s /usr/share/zoneinfo/Europe/Brussels /etc/localtime
You need to substitute the lines in bold with your actual current timezone. That will change the timezone in the current session and should prevail upon reboots. To be absolutely certain, edit the file /etc/sysconfig/clock as well. That’s the file that will be read on boot the determine the timezone.
# cat /etc/sysconfig/clock ZONE="Europe/Brussels" UTC=true ARC=false
Same story: replace the zone-info with your actual zone.
Most of your running applications will read the timezone information upon startup, so if you change this you will have to restart your applications if you want them to see the new timezone.
2. Change your current time on the server
If your clock is off on the server, first check if the timezone (explained above) is correct. If it is, you should doublecheck your NTP (Network Time Protocol) daemon settings.
First, check if the ntpd is installed.
# rpm -qa | grep ntp ntp-4.2.2p1-9.el5.centos.2.1
If that doesn’t list anything, install ntpd first.
# yum install ntp
And check if it’s started automatically upon boot.
# chkconfig --list ntpd ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
If that’s off on runlevel 3, edit it so it will start when you reboot the system.
# chkconfig ntpd on # chkconfig --list ntpd ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
To start with, update the time manually by calling the ntp daemon, then start the service so it updates automatically.
# ntpdate be.pool.ntp.org 8 Nov 16:58:12 ntpdate[3635]: step time server 193.110.251.51 offset 1.045684 sec # /etc/init.d/ntpd start Starting ntpd: [ OK ]
If you receive the following error when running ntpdate:
# ntpdate be.pool.ntp.org 8 Nov 16:58:05 ntpdate[3547]: the NTP socket is in use, exiting
Then stop the ntpd first, then try the above steps again (/etc/init.d/ntpd stop).
Once you’re certain the clock is correct, you can sync the settings back to your hardware clock.
# hwclock --systohc
That should be it to set your clock correctly, assuming your firewall allows NTP connections to remote servers. If you’re running on OpenVZ or Virtuozzo, ask your administrator to change the clock for you as that is controlled by your host.
3. Optional: copy the timezone files from another server
It’s possible that your original timezone files, the ones in /usr/share/zoneinfo/* get corrupted. If you’ve tried all the above, and still can’t get the timezones correctly on your system, copy the zonefiles from a working system.
By using the symlink method as described above, you could corrupt your original files when running the system-config-date binary to configure your timezone.