|
|
||
A Slackware Desktop Enhancement GuideConfiguring the NTP Daemon© Copyright Darrell Anderson. Installing the NTP (Network Time Protocol) daemon is a common request for many Slackware users. Although the NTP software is available in the default Slackware installation, no rc.d scripts or other support is provided. Slackware users must resort to manually configuring this commonly used tool. The basic installation procedure requires:
Almost every online discussion regarding NTP revolves around the presumption that a user is continually connected to the web via broadband and not dial-up. For dialup users that last step of launching and running the ntp daemon presents some challenges. Although the man pages provide instruction for configuring the ntp.conf file, an easier solution is to copy the file from another user and modify as necessary. A sample file is included with this mini How-To. If a person wants only to sync a single stand-alone computer clock, is not servicing other computers on a LAN, and does not experience dramatic drifting with the clock, then all that is really needed is an occasional one-shot time sync using the ntpdate command. For such people, running the ntp daemon is fun but probably overkill. Using ntpdate is easier for such people. However, using the ntp daemon makes sense if the computer clock drifts noticeably, the box is servicing other computers on a LAN to sync the time, or perhaps as an intellectual challenge with configuring the computer. Additionally, using ntpdate to perform a one-time sync before running the ntp daemon ensures that the ntp daemon will not fail because of too large of a clock drift. The ntpdate may be used with any of the listed lower-tiered public ntp servers. The ntpdate command provides a one-shot time sync as opposed to the continuous synchronizing performed by the ntp daemon. The ntp web site has a list of such public servers. ntpdate also is useful for people with a non-continuous internet connection, such as dialup, where running the ntp daemon is challenging. In addition to these public time servers, consider using the ntp system pool servers, whether using ntpdate in a one-shot role or using the ntp daemon to maintain the computer clock. If using ntpdate and a pool server, simply use the pool server URL rather than a public server URL. If using only ntpdate to occasionally sync the clock , remember to actually update the hardware clock: /sbin/hwclock –w. For ntp daemon users, using the pool servers is configured in the /etc/ntp.conf file: server 0.pool.ntp.orgserver 1.pool.ntp.org server 2.pool.ntp.org However, this configuration will tap into pool servers anywhere in the world, which might mean ever-so-modest “inaccurate” time syncs (inaccurate within the realm of a second or so). According to the ntp pool web page, users therefore should use a geographical prefix to obtain better time results. For example: server 0.us.pool.ntp.orgserver 1.us.pool.ntp.org server 2.us.pool.ntp.org or possibly server 0.mx.pool.ntp.orgserver 1.mx.pool.ntp.org server 2.mx.pool.ntp.org If there are an insufficient number of country time servers, which tends to slightly reduce accuracy when usage is high, then the prefix should refer to the larger regional areas. Refer to the ntp web site for the appropriate country or region prefix. For actually running the ntp daemon, no direct support exists in any of the Slackware rc.d scripts to start, restart, or stop the NTP daemon. A common solution is to create a traditional Slackware rc.d script to perform those tasks. A sample script is included with this mini How-To. After creating that script, the last challenge is deciding when to execute the script. One logical location would be the rc.inet2 script, but the traditional place to add user-installed modifications is rc.local. To remain consistent with other Slackware rc.d scripts, launching the rc.ntpd script would look like this: # Start the ntpd network time protocol daemon:if [ -x /etc/rc.d/rc.ntpd ]; then /etc/rc.d/rc.ntpd start fi The rc.local script runs during boot-up. Dialup users will recognize a challenge with trying to start the NTP daemon during boot-up—they are not yet online. Dialup users should not try starting the rc.ntpd script during boot-up. How then to run the NTP daemon? The obvious solution is to run the NTP daemon manually after connecting to the web. Another reasonable approach for dialup users (broadband users too), is to write their rc.ntpd script to test their online status. This is easily performed with a ping command. A challenge arises with manually running the rc.ntpd script after connecting to the net through dialup. Running the NTP script requires root privileges. Instead of running the rc.ntpd script from rc.local, which will fail for dialup users, one option is to add the previous launching instructions in the appropriate pppd dialup script. Or, for kppp users, possibly add the script to the configuration (Configure -> Accounts -> Edit -> Execute -> Upon Connect). People using the /etc/ppp/ip-up script could modify that script to launch the ntpd. Another approach is to add the rc.ntpd script to the sudo list or merely su to root and run the script manually. Another option is to run the rc.ntpd script from cron, which a plausible approach for LAN servers, but the dialup user still needs to verify the computer is online. Broadband users do not share these challenges and need only enable the rc.ntpd script (chmod +x rc.ntpd) and add the appropriate start command in rc.local. Users might consider shutting down the NTP daemon in a graceful manner when powering down or rebooting their boxes. Add some commands to your rc.shutdown script that looks like this: # Shut down the ntpd network time protocol daemon:if [ -x /etc/rc.d/rc.ntpd ]; then NTPD_PID=`/sbin/pidof ntpd` if [ "$NTPD_PID" != "" ]; then /etc/rc.d/rc.ntpd stop fi fi Some caveats. The stock Slackware /etc/rc.d/rc.inet1 initializes the dhcpc daemon such that dhcpcd will overwrite /etc/ntp.conf. Avoid that problem by modifying the /etc/rc.d/rc.inet1.conf for the affected network card by adding DHCP_KEEPNTP[x]="yes". Similarly, some people might not want the dhcpcd to modify their /etc/resolv.conf file. Again edit the rc.inet1.conf file by adding the option DHCP_KEEPRESOLV[x]="yes" for the affected network card. Editing the rc.inet1.conf file helps avoid directly modifying the rc.d scripts. People using static IP addresses need not worry about these caveats. Another concern is to ensure a box is configured properly before running the ntp software. In Slackware, with root privileges, first run the timeconfig script. For KDE users, be sure to thereafter configure the KDE clock utility (requires root privileges). Regarding the computer hardware clock, users who dual boot with Windows should use local times rather than universal times (UTC). For some loose ends, you might want to rotate the ntpd log. This log does not grow excessively, but rotating is always a reasonable policy and approach. In /etc/logrotate.d add the following executable script: # /etc/logrotate.d/ntp# logrotate configuration file for ntpd # slackware default logrotate.conf causes new log files to be created and ntpd keeps the # file open, so we can't just switch logs underneath it. Stopping and re-starting ntpd # is an option, but there's many minutes of re-sync involved so we use the copytruncate # command. See man logrotate for details. /var/log/ntp/ntp.log { nocreate copytruncate } If you support a LAN then you’ll want to sync those boxes with the box using the ntp daemon. A simple executable script in /etc/cron.hourly will suffice: #!/bin/sh# /etc/cron.hourly/clock-sync # a script to sync the clock to the network ntpd server #initialize variables and locations NTPDATE=/usr/sbin/ntpdate # insert the appropriate local box name; box should be listed in /etc/hosts to avoid DNS issues, otherwise use an actual IP address NTPSERVER=boxname $NTPDATE -s $NTPSERVER &>/dev/null If you want to be more fancy and robust, consider the following cron.hourly script: #!/bin/sh# /etc/cron.hourly/clock-sync # a script to sync the clock to the network ntpd server source /etc/functions-internet #initialize variables and locations NTPDATE=/usr/sbin/ntpdate # insert the appropriate local box name; box should be listed in /etc/hosts to avoid DNS issues, otherwise use an actual IP address NTPSERVER=boxname # is this box running the ntp daemon? if yes then don't bother with syncing if [ "`ps ax | grep 'ntpd'`" = "" ] ; then # not running the ntpd, run an internet ping test. # if the LAN gateway server is not online then ntpd probably is not active Ping_Test &>/dev/null if [ "$PINGTEST" = "FAILED" ] ; then # not connected Exit_Script clock-sync fi # if we got a good internet ping test then the local ntpd server is online too: same box $NTPDATE -s $NTPSERVER &>/dev/null fi Lastly, for people using older boxes, do not be surprised if the CMOS battery needs replacing. This simple step could avoid significant hardware clock drift problems. Hopefully the NTP daemon is now working for you. If you are interested in using some of the various ntp command line tools, visit the ntp web site or browse the Linux System Administrators Guide. Finis. |
||