One of disadvantages designing so cheap computer is that you have to give up some features which are too expensive. One of these features is RTC - real-time clock. RTC chip has its own battery to store actual time even when device isn`t plugged in. This chip costs about 10$. This is quite ennough to persuade RPi designers to do not implement this feature.
Problem with storing actual time is partially solved by fake-hwclock package which writes actual time to text file every hour and at system reboot. At start, if no better source of time is present, time is set to one stored in that text file. Since RPi is disposing a RJ-45 port, we can use it as very precise time source within NTP. First we have to install package called ntp.
apt-get install ntp
Thats all, RPi is now synchronizing its time to NTP servers. By default it uses NTP servers which are generally too far from you. It has bad influence to time accuracy. So first go to page pool.ntp.org and find a location as near as possible. For me it is Czech Republic. To set NTP servers open ntp configuration file.
And find lines starting with "server". Replace that lines for lines with servers from pool.ntp.org. In my case configuration looks like:
#/etc/ntp.conf, configuration for ntpd
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
# You do need to talk to an NTP server or two (or three).
# pool.ntp.org maps to more than 300 low-stratum NTP servers.
# Your server will pick a different set every time it starts up.
# *** Please consider joining the pool! ***
# *** ***
server 0.cz.pool.ntp.org iburst
server 1.cz.pool.ntp.org iburst
server 2.cz.pool.ntp.org iburst
server 3.cz.pool.ntp.org iburst
# By default, exchange time with everybody, but don't allow configuration.
# See /usr/share/doc/ntp-doc/html/accopt.html for details.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
# Local users may interrogate the ntp server more closely.
# Clients from this (example!) subnet have unlimited access,
# but only if cryptographically authenticated
#restrict 192.168.123.0 mask 255.255.255.0 notrust
# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
# If you want to listen to time broadcasts on your local subnet,
# de-comment the next lines. Please do this only if you trust everybody
# on the network!
Now you can restart ntp service.
Here is command to check if time is synchronizing properly. To list NTP servers with which RPi is synchronizing:
You will get some output like this.
remote refid st t when poll reach delay offset jitter
*18.104.22.168 22.214.171.124 2 u 26 64 1 58.571 0.797 68.634
+126.96.36.199 188.8.131.52 2 u 25 64 1 42.282 -1.507 0.477
+184.108.40.206 220.127.116.11 2 u 24 64 1 41.390 -1.544 68.243
-18.104.22.168 22.214.171.124 3 u 23 64 1 43.510 1.010 1.094
First letter have the most significant value of the table. * means actual time source, + means something like backup time source, - is ignored, but can become backup (+) at any time. First column shows IP adresses of NTP servers, second shows source of time for your NTP servers, third shows so called stratum. Stratum is number from 0 to 15 and shows number of hops from reference time (GPS clock, atomic clock...). That devices has stratum 0, but it is impossible to connect to them directly. For example, you have computer with sme kind of atomic clock conected (veeeery expensive - about 100 000$). Atomic clock box is stratum 0, your computer is stratum 1... Maybe you can also find stratum 16 which means untrusted time source. Column with "u" means that NTP server`s address is unicast. "When" indicates number of seconds passed since last response. "Poll" means interval between time requests, in seconds. "Reach" indicates success/failure to reach source. "Delay" indicates the roundtrip time, in milliseconds, to receive a reply. "Offset" is the difference between ntp server and your system clock in milliseconds. "Jitter" shows the difference, in milliseconds, between two samples.
Now you can check your time... I like to do everything in right way - so this command will print actual time in ISO 8601 (Representation of dates and times standard) format :)
Now when RPi has synchronized its time, you can set it to pass this time information to devices in your local network. You only have to open configuration file of ntp daemon.
And add a string that describes the hosts which requests will be answered.
restrict 192.168.1.0 mask 255.255.255.0
And also add this 2 lines. They will enable sending of broadcasts and multicasts containing time information for devices which accept them (Cisco, Juniper...)). Change first address in bold to broadcast address of your LAN. Do not change multicast address 126.96.36.199 since this addreess is assigned to NTP service by IANA and some network devices join this multicast group automatically.
Now close the configuration file (CTRL+X...) and save changes (...press "y" and Enter). Last step is to restart ntp daemon with:
From now you can configure machines in local network to synchronize its time to RPi. For example in Windows XP double click the clock in tray > Internet Time > check Automatically synchronize with an Internet time server and insert RPi`s IP address.
First versions of Windows to Windows 8 still don`t have support for network time protocol. They use Simple network time protocol instead which have a limited accuracy. Accuracy is matter of up to 4 seconds which is much more than NTP`s few milliseconds :) SNTP in Windows synchronize the time once a week, NTP daemon in Debian synchronizes its time approximately every 512 seconds by default...