HOWTO Synchronize time: simple and lightweight solutions
From Gentoo Linux Wiki
| Installation • Kernel & Hardware • Networks • Portage • Software • System • X Server • Gaming • Non-x86 • Emulators • Misc |
Contents |
[edit] Introduction
When trying to synchronize the clock on my system using an external source such as an NTP server, I have always stumbled on difficult solutions that required a good deal of configuration. I was also discouraged by the weight in kB of the packages, considering the simple task required: sync the system, local time with the correct time provided by some Internet Server. If you are interested in such solutions, check the See also section. Otherwise, keep reading.
Several minimalistic packages in portage (that I've found so far) are htpdate, rdate and getdate.
[edit] Solutions
[edit] htpdate
People connecting to the Internet through a, sometimes transparent, proxy server or behind a firewall, in corporate or education-like environments, often have access to the HTTP and HTTPS protocols only. Since NTP requires access to outgoing and incoming packets from UDP port 123, synchronizing the system time with this protocol is somewhat difficult. If no LAN-accesable NTP server is available, one can synchronize the time using HTTP, and htpdate can handle this job (with an accuracy of +-0.5 seconds).
| Code: Install htpdate |
emerge -tv htpdate |
The following command shows how to query (-q) a random web page in verbose mode (-d) using a proxy address (-P).
| Code: Query the server for the current time |
htpdate -dq -P PROXY_ADDRESS:PROXY_PORT www.linux.org www.opera.com |
To set the system time, simply replace -q option with -s.
| Code: Synchronize the system time |
htpdate -ds -P PROXY_ADDRESS:PROXY_PORT www.linux.org www.opera.com |
That's it. Additionally, instead of using www.linux.org, or www.opera.com, I recommend that you choose web servers that are hosted near you (near in terms of TCP/IP routes, not pseudo-geographical location). For instance, a .com.ar site might be hosted in a North American datacenter. Your town hall web site may always be a good reference.
You might also want to run htpdate as a daemon. As root, edit /etc/conf.d/htpdate by specifying at least one correct Internet web page, setting up the correct proxy address (if needed) and, eventually, adding additional options (check the man page). Then rc-update add htpdate default and /etc/init.d/htpdate start.
[edit] rdate
The rdate program downloads the time via the network. It connects to an RFC 868 time server over a TCP/IP network, prints the returned time and/or sets the system clock. It uses TCP or UDP to retrieve the current time of another machine. The project has been continued/forked in openrdate. The latter is an independent package of the OpenBSD's rdate program and implements RFC 868 (inetd time) and RFC 2030 (SNTP/NTP) protocols. rdate is in portage.
| Code: Install rdate |
emerge -tva rdate |
| Code: Print the retrieved time |
rdate -p ptbtime1.ptb.de |
| Code: Synchronize the system clock with the retrieved time |
rdate -s ptbtime1.ptb.de |
rdate also includes a boot script that can set the time during startup. The The rdate boot script configuration file /etc/conf.d/rdate uses an NTP pool server. rdate doesn't have support for NTP pool servers. Locating a Stratum two time server will need done to have the init script run correctly.
[edit] getdate
The getdate utility seems to be itself a fork of the rdate fork. I have no experience with it (still transparent proxy limitations), but from the man page it should go like the following:
| Code: Install getdate |
emerge -tva getdate |
| Code: Print the retrieved time |
getdate ptbtime1.ptb.de |
| Code: Synchronize the system clock with the retrieved time |
getdate -set ptbtime1.ptb.de |
| Code: Adjust the system clock with the retrieved time |
getdate -adjust ptbtime1.ptb.de |
The -set option is a "brutal" change of the system time, while -adjust will let the kernel gently adjust the system time until it is correct.
[edit] Finalize synchronization
To verify that your system time has changed correctly, run date. Also, consider to synchronize your hardware time with your system time (for example, here, upon reboot, the time went again out of sync). To store the Linux date and time in CMOS, issue the following command: hwclock --systohc. Again, run hwclock --show to check your work.
[edit] Troubleshooting
[edit] Time zones
Occasionally, your timezone settings might be incorrect. Mine, for example, was set to EST, while I'm located in CET. I'll use this example. There are a couple of ways to set the timezone:
- Run tzselect (comes with sys-libs/timezone-data),
- Run cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime or
- Add TIMEZONE="Europe/Berlin" to /etc/conf.d/clock. Then emerge -tv sys-libs/timezone-data.
Now, htpdate will sync the time correctly, that is according to your local time zone.
[edit] Graphical clocks
Graphical clocks, like the one used by Desktop Environments, for example, may have a life of their own. That is, they might listen to their own internal timezone setting. This is why, after syncing, check your work with date. At the same time, check your clock's internal settings to see if it honours the correct timezone. For example, in Xfce, Orage clock requires not only that Orage be set to the correct timezone in Preferences, but also that Orage clock be configured appropriately in Clock options (confusing, I know, but it makes sense).
[edit] See also
- HOWTO NTP - accurately synchronizes the system time
- HOWTO NTP Using OpenNTPD - an easy-to-use, partial implementation of the NTP
- HOWTO Syncing Time via internet - for system clocks that get out of sync rapidly (must accept inaccuracy of up to two minutes, in worst case)
[edit] External links
- HTTP Time Protocol (HTP) - Homepage of htpdate
- openrdate - A platform-independent package of OpenBSD's rdate program.
[edit] Credits
Original article written by --Landroni 18:40, 3 September 2007 (UTC) based on input of the gentoo-user mailing list thread called Proper proxy syntax to synchronize time via rdate. If you can improve this page, don't be shy; do it. If this article was of any help or you have any comments, please leave a message on my talk page.
