CentOS Linux: Setting timezone and synchronizing time with NTP

June 2nd, 2011

Learn how to set the correct timezone and synchronize time with NTP servers on your CentOS box.

Setting the timezone

Setting the timezone on CentOS or Redhat Enterprise Linux (RHEL) is easy.

Login as root either locally or remotely via SSH.

See what the current timezone is:

date

You should see output like this:

Wed Jun  1 10:33:29 PDT 2011

To change the timezone first look at what timezones are available by running the following command on the command line interface:

ls /usr/share/zoneinfo/

You should see a listing like this:

[root@serve3 ~]# ls /usr/share/zoneinfo
Africa      Australia  Cuba     Etc      GMT0       Iceland      Japan      MST      Poland      right      Universal  Zulu
America     Brazil     EET      Europe   GMT-0      Indian       Kwajalein  MST7MDT  Portugal    ROC        US
Antarctica  Canada     Egypt    Factory  GMT+0      Iran         Libya      Navajo   posix       ROK        UTC
Arctic      CET        Eire     GB       Greenwich  iso3166.tab  MET        NZ       posixrules  Singapore  WET
Asia        Chile      EST      GB-Eire  Hongkong   Israel       Mexico     NZ-CHAT  PRC         Turkey     W-SU
Atlantic    CST6CDT    EST5EDT  GMT      HST        Jamaica      Mideast    Pacific  PST8PDT     UCT        zone.tab

Then simply delete the current timezone:

rm /etc/localtime

And replace it with a symbolic link to the new timezone from /usr/share/zoneinfo. For example if your chosen zone is Pacific time:

ln –s /usr/share/zoneinfo/PST8PDT /etc/localtime

Synchronizing time with NTP server

Network Time Protocol (NTP) is a standard way of synchronizing computer clocks across a network. Using NTP you can keep your server’s clock synchronized with super accurate atomic clocks located around the world. Computer clocks tend to “drift” so regularly synchronizing them with NTP servers helps keep them accurate.

The first step is to make sure you have the ntp program installed. Do a:

which ntpdate

If its not available type:

yum install ntp

Once ntp is installed synchronize your computer clock with:

ntpdate 0.us.pool.ntp.org

You are not limited to the above server. There are numerous NTP servers around the world. You can find a complete list at ntp.org.

Also keep in mind that ntp only affects the system time. The hardware clock on your server will not reflect that. So you want to set it as well so that the correct time is maintained after reboot:

hwclock --systohc

ntpd

To keep your server clock automatically synchronized you can run the ntpd daemon which is installed as part of the ntp package.

Edit the /etc/ntp.conf file to comment out the following lines:

#server 127.127.1.0     # local clock
#fudge  127.127.1.0 stratum 10

The above two lines can sometimes prevent ntpd from properly synchronizing your clock. They are already commented out by default on CentOS/RHEL 6. But on 5.x you have to comment them out manually.

Finally type the following two commands to start the daemon and make it run automatically at boot up:

service ntpd start
chkconfig ntpd on

20 Responses to “CentOS Linux: Setting timezone and synchronizing time with NTP”

  1. Thiago says:

    Thanks a lot… a lot!

    Posted on 15 May 2012 Reply
  2. Brandon says:

    worked like a charm! Thanks!

    Posted on 22 May 2012 Reply
  3. Betty B Thomas says:

    Thanks!!!

    Posted on 31 Aug 2012 Reply
  4. rfas says:

    Perfect!

    Posted on 09 Oct 2012 Reply
  5. Thomas says:

    Thank you very much.

    Posted on 01 Dec 2012 Reply
  6. [...] have previously written about setting the time and timezone in CentOS. Setting the timezone is much easier in [...]

    Posted on 09 Feb 2013
  7. Fandy says:

    Hello, nice tutorial
    How i can set in GMT+7 for Asia/Jakarta?
    Thank you :)

    Posted on 19 Feb 2013 Reply
    • Abdussamad says:

      Just symlink /etc/localtime to the appropriate file in /usr/share/zoneinfo/Asia/

      Posted on 19 Feb 2013 Reply
  8. Erwin says:

    Nice job… pulling my hair like for half day :D ty

    Posted on 20 Feb 2013 Reply
  9. Chirag says:

    Hi Auther,
    I have installed NTPD and service is running as daemon. The service is syncing time properly from NTP server.

    My question is, does NTPD (NTP Client) also syncs timezone from NTP server if it is different at Client over NTP Server ?

    Posted on 20 Mar 2013 Reply
    • Abdussamad says:

      No it doesn’t sync timezone.

      Posted on 20 Mar 2013 Reply
      • Chirag says:

        Thanks for quick response,

        Is there any command way to change timezone in ntpd instead of making symbolic link as you explained in blog.

        Posted on 20 Mar 2013 Reply
        • Abdussamad says:

          Well you could copy the appropriate timezone file from /usr/share/zoneinfo to /etc/localtime instead of sym linking it. I don’t know of any other way.

          Posted on 21 Mar 2013 Reply
  10. user says:

    Thank you :)

    Posted on 25 Mar 2013 Reply
  11. Chris says:

    Wonderful tutorial. Thank you so much.

    Posted on 10 Apr 2013 Reply
  12. amit says:

    Thanks a lot. It works

    Posted on 03 May 2013 Reply
  13. Rohan Mhatre says:

    Linex server selected time zone is Ashia Kolkatta but it gives IST+5.30 timestamp. how to get rid of this additional +5.30 offset to get correct IST time.

    Posted on 12 Aug 2013 Reply
    • Abdussamad says:

      If the timezone is correct try running ntpdate again.

      Posted on 12 Aug 2013 Reply
  14. Rohan Mhatre says:

    it shows time equals current time +5.30. and error no servers can be useed, exiting. ntpq -p shows server synced to gps server.

    Posted on 12 Aug 2013 Reply
    • Abdussamad says:

      What command did you run? Did you run ntpdate pool.ntp.org? Try shutting down the ntp service before running ntpdate.

      Posted on 12 Aug 2013 Reply

Leave a Reply

Your email address will not be published. Required fields are marked *