How To Set The System Date And Time

Normally the date and time on a Linux system are kept synchronised with time servers on the Internet by the ntp (Network Time Protocol) daemon, and it is generally better to let the ntp daemon do its job. You can, however, either force the system time to synchronise, or manually set it.

To force a synchronisation, we first need to stop the ntp daemon. Depending upon the Linux distribution you are using, one of the following two commands should work (the other will give a harmless error):

/etc/init.d/ntp stop
# or
/etc/init.d/ntpd stop

To try to synchronise with an Internet time server:

ntpdate 0.debian.pool.ntp.org

The server name to synchronise with can be any public NTP server; there is a full list available at http://support.ntp.org/bin/view/Servers/StratumTwoTimeServers

If that fails, the time and date may be set manually with:

date -s "20110525 03:09"

That’s in the format “yyyymmdd hh:mm”, so the above example would set the date to 25 May, 2011, and the time to 03:09. The date or time may be omitted, and will default to the current system date and midnight.

Finally, we should start the ntp daemon again:

/etc/init.d/ntp start
# or
/etc/init.d/ntpd start

You can check the current system date and time with the date command:

$ date
Wed May 25 14:53:47 BST 2011

Back to Linux Tips and Advice

© 2012 Tiger Computing Ltd, Wyastone Business Park, Wyastone Leys, Monmouth, NP25 3SR | Legal | Sitemap