SecureCube.com

7Jan/130

Setup Linux NTP daemon

Looking for a reliable NTP daemon to keep your system clock up-to-date. Look no further... OpenNTPD. The following will outline setup of OpenNTPD on Linux.

Download the latest source from OpenNTPD.

Extract and change to the install directory.

tar xzf openntpd-3.9p1.tar.gz
cd openntpd-3.9p1

Create a user and group which the ntp daemon will run and chroot'ed to directory.

groupadd _ntp
useradd -g _ntp -s /sbin/nologin -d /var/empty/ntp -c 'OpenNTP daemon' _ntp
mkdir -p /var/empty/ntp
chown 0 /var/empty/ntp
chgrp 0 /var/empty/ntp
chmod 0755 /var/empty/ntp

Perform standard install from source steps. Read the INSTALL doc for more information.

./configure
make && make install

Next we'll create a init script to start/stop the daemon.

vi /etc/init.d/ntpd
#!/bin/bash

NTPD_CONF=/usr/local/etc/ntpd.conf

if [ ! -f $NTPD_CONF ]
 then
 echo "Could not find $NTPD_CONF"
 exit 1
fi

# -s : Set the time immediately at startup if the
# local clock is off by more than 180 seconds.
PARAMS="-s"

PID=`pidof -o %PPID /usr/local/sbin/ntpd`
case "$1" in
 start)
 echo "Starting OpenNTPD"
 [ -z "$PID" ] && /usr/local/sbin/ntpd $PARAMS
 if [ $? -gt 0 ]; then
 echo "Failed"
 else
 PID=`pidof -o %PPID /usr/local/sbin/ntpd`
 echo $PID >/var/run/openntpd.pid
 echo "Done"
 fi
 ;;
 stop)
 echo "Stopping OpenNTPD"
 [ ! -z "$PID" ] && kill $PID &>/dev/null
 if [ $? -gt 0 ]; then
 echo "Failed"
 else
 echo "Done"
 fi
 ;;
 restart)
 $0 stop
 $0 start
 ;;
 *)
 echo "usage: $0 {start|stop|restart}"
esac
exit 0

Change the init script to allow for execution.

chmod a+x /etc/init.d/ntpd

Next setup the ntpd.conf file to point to servers you want to sync with. You can read the man pages first for syntax.

man ntpd.conf
vi /usr/local/etc/ntpd.conf

You can use this site to find a pool of servers close to you.

Finally we'll start the daemon.

/etc/init.d/ntpd start

You can view the progress of the sync using the following command.

cat /var/log/syslog | grep ntpd

You can see the progression of the clock...

Jan 7 12:02:53 pc-name ntpd[1355]: adjusting local clock by -1.462512s
Jan 7 12:05:36 pc-name ntpd[1355]: adjusting local clock by -1.429211s
Jan 7 12:08:27 pc-name ntpd[2064]: adjusting local clock by -1.362814s
Jan 7 12:12:14 pc-name ntpd[2064]: adjusting local clock by -1.302321s
Jan 7 12:16:37 pc-name ntpd[2064]: adjusting local clock by -1.199679s
Jan 7 12:20:53 pc-name ntpd[2064]: adjusting local clock by -1.119930s
Jan 7 12:24:01 pc-name ntpd[2064]: adjusting local clock by -1.076154s
Jan 7 12:25:53 pc-name ntpd[2064]: adjusting local clock by -1.002400s
Jan 7 12:28:47 pc-name ntpd[2064]: adjusting local clock by -0.946294s
Jan 7 12:31:13 pc-name ntpd[2064]: adjusting local clock by -0.926053s
Jan 7 12:33:22 pc-name ntpd[2064]: adjusting local clock by -0.867900s
Jan 7 12:35:44 pc-name ntpd[2064]: adjusting local clock by -0.819938s
Jan 7 12:37:18 pc-name ntpd[2064]: adjusting local clock by -0.787604s
Jan 7 12:38:56 pc-name ntpd[2064]: adjusting local clock by -0.717054s
Jan 7 12:42:42 pc-name ntpd[2064]: adjusting local clock by -0.653446s
Jan 7 12:45:32 pc-name ntpd[2064]: adjusting local clock by -0.599668s
Jan 7 12:47:44 pc-name ntpd[2064]: adjusting local clock by -0.512153s
Jan 7 12:51:51 pc-name ntpd[2064]: adjusting local clock by -0.472384s
Jan 7 12:54:44 pc-name ntpd[2064]: adjusting local clock by -0.397733s
Jan 7 12:57:25 pc-name ntpd[2064]: adjusting local clock by -0.335255s
Jan 7 13:00:39 pc-name ntpd[2064]: adjusting local clock by -0.275158s
Jan 7 13:03:15 pc-name ntpd[2064]: adjusting local clock by -0.222749s
Jan 7 13:06:49 pc-name ntpd[2064]: adjusting local clock by -0.197332s
Jan 7 13:09:53 pc-name ntpd[2064]: adjusting local clock by -0.175554s

Depending on how far your clock is off you man want to perform a manual update.

rdate -nv tick.usno.navy.mil

Check how far off you clock currently is.

rdate -nv tick.usno.navy.mil

Following are some troubleshooting steps...

Check for ntp connections to ntp sources

netstat -upn

Check your hardware clock isn't jumping around

hwclock --show; hwclock --show; hwclock --show; hwclock --show; hwclock --show

Other things to note is you may not be able to set your clock if your on a VPS server. You'll have to talk to your VPS provider to fix. Also don't use ntpdate in a cron job as this will just force your clock to jump all around where as an ntp daemon will progressivly adjust the clock into accurate time.

Filed under: Linux Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.