Your Linux server has two clocks. The first one is the hardware clock on your computers motherboard. This is the one that has the battery that has to be replaced every several years or so. When the power is off, this clock keeps on ticking. Older hardware clocks kept their time by filling and dumping capacitors.. Sort of a modern electronic version of a water clock.. Most newer clocks count the vibrations of a crystal to derive the time. But they can and do drift..
See the Clock MINI-HOWTO for more information on the hardware clock.
Login as root and try the command:
# clock -r
or just the plain
# clock
or try
# hwclock --show
or just the plain
# hwclock
This will give you the current time in your hardware..
Once your Linux System boots up, the System clock takes over after first getting its initial time and date from the Hardware RTC clock. After your Linux System has been up and running for a while, you may find that the System clock and the Hardware RTC clock have very different readings.
Again logged in as root, try the following commands:
# date
for the System time and date
# clock
for the Hardware time and date
Mine have been days or even years apart.. I would neglect the Hardware clock and periodically reset the System clock with the date command. On one 386sx system, the battery had died so the hardware clock was years off the real time.
The US Government has the most accurate clocks in the world. The US Government also has the most powerful military in the world. The two facts are not separate. Accurate time is essential in calculating where something is; be it a person, ship, missile or building. Accurate time to many decimal places is required to locate a position to the nearest inch, or guide a missile from a submerged submarine to a bunker.
Accurate time is useless unless it can be distributed accurately. In the past one would carry a time piece around setting each clock to that time piece. Each clock was dependent on the accuracy of the setting clock. Telegraphs allowed time to be passed along the line. All clocks along the telegraph wire could be kept fairly close in time together. You still had to carry a clock set from the telegraph to a more distant clock at home or work.
Radio and telephones allowed the time to be distributed to more people. Still you had to find the station or phone number, make the connection and then listen to make the change. See NIST Time and Frequency Services for more information.
With today's Internet, you can get the time for your PC or Linux server directly from the time servers. .
The NIST Site, Set Your Computer Clock Via the Internet has Windows and Mac software for setting your computers clock. It also mentions getting the time via Telnet to port 13 at one of the Internet Time Servers.
Try the following command either as root or as a regular shell user in Linux:
$ telnet 131.107.1.10 13
That will give you the Universal Time (UTC) from time-nw.nist.gov at Microsoft, Redmond, Washington. It's interesting that Microsoft has a dot.gov site.
Log in as root and we will combine several commands and use telnet to get the current time and then set the System time. It does not set the System date. Just the time.
# date -u -s ` telnet 131.107.1.10 13 |grep UTC |cut -b16-23 `
(The single quote is the one below the ~ on most keyboards) The -u option sets the time converting from UTC to your local time. Your local time definition is likely to be in the file /etc/localtime or rather to the file it is linked to. Do an ls -l /etc/localtime to find where yours is linked to.
Telnet works and does get you the date and time, but there is a much easier way to get and set the System time and date.
I stumbled across the netdate command while looking up another command in O'Reilly's Linux In A Nutshell. Netdate had not made sense to me before doing the telnet to the time servers. But now it was clear! Using netdate and several of the time servers I could keep my Linux System time current to the second. Easy.
Login as root and use the following command to set your Linux System clock to the best, most current time:
# netdate 129.6.15.28 132.163.4.102 131.107.1.10 207.200.81.113
Netdate sets the System time according to one of the listed hosts. Use of IPs is recommended on one of the time.gov site pages as the host name may change. Netdate tries to ascertain the most accurate host listed, usually the first. It then sets the System time to this host.
You can use just one or more hosts with netdate. See Internet Time Servers for other hosts.
Netdate is easy!! Just need an Internet connection.
To set the Hardware Real Time Clock (RTC) from the System Clock, use either of the following commands.
# clock -w
or
# hwclock --systohFor more information on each command:
# man netdate
# man clock
# man hwclock
The manual entry for netdate suggests creating a shell script for updating your time using netdate. First do a ls /sbin/timehosts to see if it's already there..
Otherwise, here is a simple script to do the trick:
# cat /sbin/timehosts
#!/bin/sh
#now to set the system date to the most reliable time server
netdate 129.6.15.28 132.163.4.102 131.107.1.10 207.200.81.113# see http://www.bldrdoc.gov/timefreq/service/time-servers.html
#now to set the CMOS/hardware clock
clock -w
#or if clock not available try
#hwclock --systohc
Don't forget to chmod 755 /sbin/timehosts to allow it to be written to by root and executed by all.
Or for a bit more information:
Don't forget to chmod 755 /sbin/setdate to allow it to be written to by root and executed by all.# cat /sbin/setdate
#!/bin/sh
#lets see what the current system date is
echo The current System date and time is
date
echo the CMOS/Hardware clock date/time is
clock -r
#or if clock is not available
#hwclock --show#now to set the system date to the most reliable time server
netdate 129.6.15.28 132.163.4.102 131.107.1.10 207.200.81.113
#has the system time changed much?
echo The Sytem and CMOS/Hardware clock date/time will be set to:
date
#now to set the CMOS/hardware clock
clock -w
#or if clock not available try
#hwclock --systohc#for finding other IPs to use for netdate see the below
# see http://www.bldrdoc.gov/timefreq/service/time-servers.html
Running the above shell scripts as needed is OK. But you can add it to your crontab file and let cron run it. This is great for those motherboard's that tend to gain or loose seconds every day.
You will need to login as root as netdate and clock/hwclock require root to set the time and date.
Will allow you to edit the current crontab file. Just add the following line.# crontab -e
14 1 * * * /sbin/setdate > /dev/null
This will cause the /sbin/setdate script to run at 1:14AM with all output going to the null output.. You can have it run more often or less often. For more information on crontab:
# man crontab
That's it for keeping your Linux System time and hardware Real Time Clock upto the second.
About the only excuses not to keep your Linux server on time is either no Internet connection or it's not your Linux System and you don't have root access. If that's the case, that's too bad.