Find the answer to your Linux question:
Results 1 to 6 of 6
I was wondering how would I setup the ntp.conf so that when ntpd starts it syncs with the time servers immediately. Any help would be appreciated. For an example, I ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    3

    NTP sync issues

    I was wondering how would I setup the ntp.conf so that when ntpd starts it syncs with the time servers immediately. Any help would be appreciated.

    For an example, I set the date/time by using:

    # date -u 010101012009
    Thu Jan 1 01:01:00 UTC 2009

    When I restart ntpd it should resync automatically with the time servers that I have put in the server line in ntp.conf.

    Am I doing something wrong?

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by myster1ous View Post
    I was wondering how would I setup the ntp.conf so that when ntpd starts it syncs with the time servers immediately. Any help would be appreciated.

    For an example, I set the date/time by using:

    # date -u 010101012009
    Thu Jan 1 01:01:00 UTC 2009

    When I restart ntpd it should resync automatically with the time servers that I have put in the server line in ntp.conf.

    Am I doing something wrong?
    Don't know, but here is a copy of my ntp.conf that works just fine on my CentOS system.
    # Permit time synchronization with our time source, but do not
    # permit the source to query or modify the service on this system.
    restrict default kod nomodify notrap nopeer noquery

    # Permit all access over the loopback interface. This could
    # be tightened as well, but to do so would effect some of
    # the administrative functions.
    restrict 127.0.0.1
    restrict -6 ::1

    # Hosts on local network are less restricted.
    #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

    # Use public servers from the pool.ntp.org project.
    # Please consider joining the pool (pool.ntp.org: Join the NTP Pool!).
    server 0.rhel.pool.ntp.org
    server 1.rhel.pool.ntp.org
    server 2.rhel.pool.ntp.org

    #broadcast 192.168.1.255 key 42 # broadcast server
    #broadcastclient # broadcast client
    #broadcast 224.0.1.1 key 42 # multicast server
    #multicastclient 224.0.1.1 # multicast client
    #manycastserver 239.255.254.254 # manycast server
    #manycastclient 239.255.254.254 key 42 # manycast client

    # Undisciplined Local Clock. This is a fake driver intended for backup
    # and when no outside source of synchronized time is available.
    fudge 127.127.1.0 stratum 10

    # Drift file. Put this in a directory which the daemon can write to.
    # No symbolic links allowed, either, since the daemon updates the file
    # by creating a temporary in the same directory and then rename()'ing
    # it to the file.
    driftfile /var/lib/ntp/drift

    # Key file containing the keys and key identifiers used when operating
    # with symmetric key cryptography.
    keys /etc/ntp/keys

    # Specify the key identifiers which are trusted.
    #trustedkey 4 8 42

    # Specify the key identifier to use with the ntpdc utility.
    #requestkey 8

    # Specify the key identifier to use with the ntpq utility.
    #controlkey 8
    restrict 0.rhel.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    restrict 1.rhel.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    restrict 2.rhel.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    server pool.ntp.org
    restrict pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    3
    Thank you, I'll try if it works.

  4. #4
    Just Joined!
    Join Date
    May 2009
    Posts
    3
    Ok I used some of your settings and applied to mine. It works, but is there a way to make it sync instantly when the ntp daemon is restarted?

    Here is my ntp.conf

    Code:
    # Permit time synchronization with our time source, but do not
    # permit the source to query or modify the service on this system.
    
    restrict default nomodify notrap noquery
    
    # Permit all access over the loopback interface.  This could
    # be tightened as well, but to do so would effect some of
    # the administrative functions.
    restrict 127.0.0.1 
    
    # -- CLIENT NETWORK -------
    # Permit systems on this network to synchronize with this
    # time service.  Do not permit those systems to modify the
    # configuration of this service.  Also, do not use those
    # systems as peers for synchronization.
    # restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
    
    
    # --- OUR TIMESERVERS ----- 
    server 0.rhel.pool.ntp.org
    server 1.rhel.pool.ntp.org
    server 2.rhel.pool.ntp.org
    
    # --- NTP MULTICASTCLIENT ---
    #multicastclient			# listen on default 224.0.1.1
    # restrict 224.0.1.1 mask 255.255.255.255 nomodify notrap
    # restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
    
    
    
    # --- GENERAL CONFIGURATION ---
    #
    # Undisciplined Local Clock. This is a fake driver intended for backup
    # and when no outside source of synchronized time is available. The
    # default stratum is usually 3, but in this case we elect to use stratum
    # 0. Since the server line does not have the prefer keyword, this driver
    # is never used for synchronization, unless no other other
    # synchronization source is available. In case the local host is
    # controlled by some external source, such as an external oscillator or
    # another protocol, the prefer keyword would cause the local host to
    # disregard all other synchronization sources, unless the kernel
    # modifications are in use and declare an unsynchronized condition.
    #
    server	127.127.1.0	# local clock
    fudge	127.127.1.0 stratum 10	
    
    #
    # Drift file.  Put this in a directory which the daemon can write to.
    # No symbolic links allowed, either, since the daemon updates the file
    # by creating a temporary in the same directory and then rename()'ing
    # it to the file.
    #
    driftfile /var/lib/ntp/drift
    broadcastdelay	0.008
    
    #
    # Keys file.  If you want to diddle your server at run time, make a
    # keys file (mode 600 for sure) and define the key number to be
    # used for making requests.
    #
    # PLEASE DO NOT USE THE DEFAULT VALUES HERE. Pick your own, or remote
    # systems might be able to reset your clock at will. Note also that
    # ntpd is started with a -A flag, disabling authentication, that
    # will have to be removed as well.
    #
    keys		/etc/ntp/keys
    
    # Specify the key identifier to use with the ntpq utility.
    #controlkey 8
    restrict 0.rhel.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    restrict 1.rhel.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    restrict 2.rhel.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    server pool.ntp.org
    restrict pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    Thanks again

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    I do know that my system syncs with the time server when it boots - I see it in the boot log. I'll see if I can post the ntp startup script in rcN.d - it starts up when moving into runlevel 1. Ok, here it is,K74ntpd. Put it in each rcN.d subdirectory of /etc/rc.d:
    Code:
    #!/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
    
    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 ":46aAbc:dD:f:gi:k:l:LnN: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() {
            # Check that networking is up.
            [ "$NETWORKING" = "no" ] && exit 1
    
            readconf;
    
            if [ -n "$dostep" ]; then
                echo -n $"$prog: Synchronizing with time server: "
                /usr/sbin/ntpdate $dropstr -s -b $NTPDATE_OPTIONS $tickers &>/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}"
            RETVAL=3
    esac
    
    exit $RETVAL
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Note that in the ntp script that there is code to sync the clock when starting, but that depends upon the /etc/sysconfig/ntpd configuration file. There is an option there, SYNC_HWCLOCK which if set to yes will sync the hardware clock after successfully calling ntpdate. So, my guess is that you only need to change that script. The contents of mine are (note mine doesn't sync the hw clock):
    Code:
    # Drop root to id 'ntp:ntp' by default.
    OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid"
    
    # Set to 'yes' to sync hw clock after successful ntpdate
    SYNC_HWCLOCK=no
    
    # Additional options for ntpdate
    NTPDATE_OPTIONS=""
    I hope this helps.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...