13

Using the time C function (number of seconds since the Epoch) shows that the time on my current CentOS 6 server is about 7 hours behind compared to another server with the correct time. How can I correct the system clock? I don't think it's drift because I just setup this server a few weeks ago, but it could be. I setup ntpd but it's not helping, maybe because the time difference is too much.

4
  • 3
    Check the output from the command TZ=UTC date. Nov 30, 2013 at 7:09
  • 1
    you are right, ntpd will not correct such a time difference. For ntpd to actually work, your system must be within a couple of minutes of the time server.
    – fpmurphy
    Nov 30, 2013 at 16:05
  • Sat Nov 30 09:09:21 UTC 2013 is the output I just got
    – user3180
    Nov 30, 2013 at 16:11
  • Yes, that's seven hours behind. Nov 30, 2013 at 20:11

2 Answers 2

23

The simple answer is "set the date manually", which you need to do, but to prevent this occurring again, there is more that you should do.

  1. Ensure that the system timezone configuration is in a sane state.

    Unless there is a very strong reason not to do so (such as software compatibility issues), server clocks should always run on UTC time.

    If you decide not to use UTC, choose a timezone by running tzselect. A timezone will be printed on screen which you will use below. An example would be Europe/Moscow. Otherwise use UTC as the timezone below.

    Here is that TZ value again, this time on standard output so that you
    can use the /usr/bin/tzselect command in shell scripts:
    Europe/Moscow
    

    Set the system clock to your desired timezone by the following steps:

    1. Replace the contents of /etc/sysconfig/clock with the following:

      ZONE="<timezone>"
      UTC=true
      

      For example:

      ZONE="Europe/Moscow"
      UTC=true
      

      Note that UTC=true should be set here, even if you don't use UTC as your timezone. This refers to the server's hardware clock, which should always be UTC regardless of your chosen system timezone.

    2. Replace the /etc/localtime file with a link to the selected timezone:

      # ln -snf /usr/share/zoneinfo/<timezone> /etc/localtime
      

      For example:

      # ln -snf /usr/share/zoneinfo/Europe/Moscow /etc/localtime
      # ln -snf /usr/share/zoneinfo/UTC /etc/localtime
      
  2. Set the clock manually to the current time.

    1. Sync the system clock to the current time:

      # ntpd -g -q
      
    2. Check that the time appears correct:

      # date
      
    3. Sync the server's hardware clock to the system clock:

      # hwclock -wu
      
  3. Restart the computer. Restarting is necessary because all system services must be restarted to pick up the corrected time and timezone, and the server's hardware clock needs to be tested (e.g. for a faulty battery).

    After restart, check to see that the system shows the correct time and that ntpd is running properly.

2
  • Thank you very much Michael. How do I ensure it is correct during setup? I remember selecting the correct time zone, yet the time ends up still being wrong.
    – user3180
    Dec 3, 2013 at 2:54
  • In the graphical installer, there's a checkbox stating "System clock uses UTC". Always leave this checked, regardless of which timezone you select. Dec 3, 2013 at 2:58
0

point is, make sure timezone is the same of the boxes you are comparing

EDIT

if TZ is different

/etc/localtime

then remember to take UTC offset into account when comparing system time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.