How can I set the time in Centos so that resetting the OS doesn't revert to its default value?

30,504

Solution 1

You can use NTP (Network Time Protocol) if this machine is Internet connected. This will synchronize the machine's clock to an Internet time server.

yum install ntp, then edit the /etc/ntp.conf file so that you have at least one line that looks like:

server 0.pool.ntp.org

Then chkconfig ntpd on so that it will start up automatically on boot.

Once this is done, start the NTP service with service ntpd start or /etc/init.d/ntpd start. Or, for newer versions of CentOS that use systemd, run systemctl start ntpd.

Here's some additional documentation from Slicehost.

Solution 2

The program date sets the system clock, i.e. the clock maintained internally by the operating system. There is a separate clock inside the computer which keeps the time when the computer is off (or rebooting). If you've set the system clock manually, call hwclock to set the hardware clock as well.

hwclock --utc --systohc

(Replace --utc by --localtime if your hardware clock is set to local time. This should only be done if you dual boot with Windows.)

Some distributions have a script that calls hwclock with the right options (--utc or --localtime, and if relevant other hardware-specific options). For example, on Debian, run /etc/init.d/hwclock.sh stop. I don't know about CentOS.

All of this is for a one-time thing. If your computer has a permanent or frequent Internet connection, once you've set the time approximately right manually, make it synchronize its time over the network, using NTP. See cjc's answer.

Share:
30,504
steve mais
Author by

steve mais

For the love of God, do not downvote new comers! help them kindly so they can feel a apart of this community!

Updated on September 18, 2022

Comments

  • steve mais
    steve mais over 1 year

    I have a VPS running on Cent Os 5.8 (x86). There are situations in which we are forced to reset the entire VPS (it hangs and the only way to get it back on is to reset it). When we do so, the time is reset too.

    We use the following command to set the time (the timezone is configured through kloxo admin panel and is not reset like the time does):

    #date +%T%p -s "10:10:10" (as for hour 10 and minutes 10 and seconds 10 )
    
    • Tim
      Tim over 11 years
      You have no choice but to tolerate the lockups? I would be hammering on the VPS providers door for answers.
    • Tim
      Tim over 11 years
      Are you hard coding 10:10:10 every time or is that an example? Are you trying to pull real time values?
    • cjc
      cjc over 11 years
      Can't you use NTP?
    • steve mais
      steve mais over 11 years
      @Tim:Well he is not answering and keeps saying the vps is completely unmanned and its all on us. And at the moment yes, because i couldn't have found the cause(s) so far.(i will open another question for solving this question later) that 10:10:10 is just an example of my input.My local timezone is Iran/Tehran, the timezone is set , for setting the time, i sync it with my pcs current time.Im trying to set the time not getting it( if i got your meaning by 'pulling the time' right)
    • steve mais
      steve mais over 11 years
      @cjc:What is NTP? i have no idea what that is.I am just a simple linux user(newbie)
  • steve mais
    steve mais over 11 years
    It still says 7:13 while it should say 11:13PM,are there any other options which i should mess around with?
  • cjc
    cjc over 11 years
    @Hossein That's a timezone issue. Look at redhat.com/advice/tips/timezone.html
  • steve mais
    steve mais over 11 years
    I just noticed everything is fine:),I guess that was a browser cache issue and not the servers itself;) it is set to 12:22PM as it should have :)