Gentoo Wiki ArchivesGentoo Wiki

HOWTO_Synchronize_time:_simple_and_lightweight_solutions


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Contents

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.

Solutions

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.

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.

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.

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.

Troubleshooting

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:

Now, htpdate will sync the time correctly, that is according to your local time zone.

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).

See also

External links

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.

Retrieved from "http://www.gentoo-wiki.info/HOWTO_Synchronize_time:_simple_and_lightweight_solutions"

Last modified: Thu, 04 Sep 2008 14:06:00 +1000 Hits: 12,110

Created by NickStallman.net, Luxury Homes Australia
Real estate agents should list their apartments, townhouses and units in Australia.