Ntpd

From Wsms

Jump to: navigation, search

Note: This page should be titled ntpd (all lowercase). It is Ntpd due to technical limitations of Mediawiki.

Contents

ntpd

ntpd is the network time protocol deamon. It sets your systems clock form a master server and can be configured to provide the time to other systems.

common usage

On Redhat/Fedora/CentOS:

[root@arthur etc]# yum -y install ntp
[root@arthur etc]# chkconfig --list ntpd
ntpd            0:off   1:off   2:off   3:on    4:off   5:on    6:off
[root@arthur etc]# service ntpd restart

to make your system a time server

Edit /etc/ntp.conf so that the line for your LAN looks something liki this:

restrict 192.168.2.0 mask 255.255.255.0 nomodify notrap

Be sure to check the output in /var/log/messages to make sure everything is working. Also, be sure that your firewall allows udp connections from the LAN to port 123.

client setup

To make your client use your server...

modification to /etc/init.d/ntpd

I found that ntpd works poorly on virtual machines running under VMWare. A partial fix for the problem is to add an ntpdate command to the startup section of /etc/init.d/ntpd. Here is the modified version:

#!/bin/bash
#
# ntpd		This shell script takes care of starting and stopping
#		ntpd (NTPv4 daemon).
#
# chkconfig: - 58 74
# description: ntpd is the NTPv4 daemon. \
# The Network Time Protocol (NTP) is used to synchronize the time of \
# a computer client or server to another server or reference time source, \
# such as a radio or satellite receiver or modem.

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

if [ -f /etc/sysconfig/ntpd ];then
        . /etc/sysconfig/ntpd
fi

ntpconf=/etc/ntp.conf
ntpstep=/etc/ntp/step-tickers


RETVAL=0
prog="ntpd"

sync_hwclock() {
	ARC=0
	SRM=0
	UTC=0

	if [ -f /etc/sysconfig/clock ]; then
	   . /etc/sysconfig/clock

	   # convert old style clock config to new values
	   if [ "${CLOCKMODE}" = "GMT" ]; then
	      UTC=true
	   elif [ "${CLOCKMODE}" = "ARC" ]; then
	      ARC=true
	   fi
	fi

	CLOCKFLAGS="$CLOCKFLAGS --systohc"

	case "$UTC" in
	    yes|true)	CLOCKFLAGS="$CLOCKFLAGS --utc";;
	    no|false)	CLOCKFLAGS="$CLOCKFLAGS --localtime";;
	esac
	case "$ARC" in
	    yes|true)	CLOCKFLAGS="$CLOCKFLAGS --arc";;
	esac
	case "$SRM" in
	    yes|true)	CLOCKFLAGS="$CLOCKFLAGS --srm";;
	esac

	action $"Syncing hardware clock to system time" /sbin/hwclock $CLOCKFLAGS
}

readconf() {
	dostep=''
	dropstr=''
	OPTIND=1
	while getopts ":aAbc:dD:f:gi:k:l:LmnN:p:P:qr:s:t:u:v:V:x" args $OPTIONS;
	do 
	  case "$args" in
	    x) dostep=yes;;
	    c) ntpconf="$OPTARG";;
	    u) dropstr="-U $(echo $OPTARG | sed 's/:.*//')";;
	  esac
	done

	[ -x /usr/sbin/ntpd -a -f $ntpconf ] || exit 0

	tickers=''
	if [ -s "$ntpstep" ]; then
	    tickers=$(sed 's/#.*//' $ntpstep)
	    echo "$tickers" | grep -qi '[a-z0-9]' && dostep=yes || tickers=''
	fi
	if [ -n "$dostep" -a -z "$tickers" ]; then
	    # -x option is used, but step-tickers doesn't exist or contain
	    # anything useful, use servers from ntp.conf instead
	    tickers=$(awk '$1=="peer"||$1=="server"{print $2}' $ntpconf | \
	        fgrep -v 127.127.1.0)
	fi
}

start() {
        ntpdate pool.ntp.org # GGG 20070315
	readconf;

	if [ -n "$dostep" ]; then
	    echo -n $"$prog: Synchronizing with time server: "
	    /usr/sbin/ntpdate $dropstr -s -b $tickers 2>/dev/null >/dev/null
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && success || failure
	    echo
	    if [ $RETVAL -eq 0 ]; then
	        [ "$SYNC_HWCLOCK" = "yes" ] && sync_hwclock
	    else
	        OPTIONS="$OPTIONS -g"
	    fi
	else
	    # -g can replace the grep for time servers
	    # as it permits ntpd to violate its 1000s limit once.
	    OPTIONS="$OPTIONS -g"
	fi
        # Start daemons.
        echo -n $"Starting $prog: "
        daemon ntpd $OPTIONS
	RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ntpd
	return $RETVAL
}

stop() {
        echo -n $"Shutting down $prog: "
	killproc ntpd
	RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ntpd
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
        ;;
  stop)
	stop
        ;;
  status)
	status ntpd
	RETVAL=$?
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f /var/lock/subsys/ntpd ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        exit 1
esac

exit $RETVAL

Use it like this:

[root@localhost ~]# service ntpd restart
Shutting down ntpd:                                        [  OK  ]
15 Mar 16:25:40 ntpdate[2986]: step time server 38.112.70.6 offset 135.372352 sec
Starting ntpd:                                             [  OK  ]

See also:
Linux commands
ntp
http://www.linuxjournal.com/article/8412
20061215#synchronize_vmware.27s_clock_with_ntpd_-_Why_doesn.27t_this_work.21.3F.21

Personal tools