How to change time in Linux from a network source

Short answer:
sudo /etc/init.d/ntpd stop
sudo ntpd -q -x -g
sudo /etc/init.d/ntpd start

One can always use the `date --set=` command to change system time, but that entails human error. I needed a command that would set my system time from a network source, which is more reliable than my wall clock.

ntpd does this automatically for you. But it does in very small increments so that the programs running on the system do not see a sudden huge change in time and go crazy; which makes sense most of the times.

But if you are running virtual machines, which you suspend and wake up often, then you need a way to change the system time of the virtual machine immediately after you unsuspend it. ntpd does not work well in this case, since it will take forever to bring the system up to current time.

A little digging brought up ntpdate, but since that is being depracated in favour of ntpd, I used `ntpd -q -x -g` to bring the virtual machine hosted system to current time.

If you are running a half-decent virtual machine software, like VirtualBox, it comes with 'Guest Additions', which, when installed on the guest OS will perform such chores for you after every wake up.

No comments:

Post a Comment