<div dir="ltr"><div class="gmail_default" style="font-size:small;color:rgb(0,102,0)"><span style="font-family:courier new,monospace">​​​Hi there,<br><br>since this issue really bugged me as well (why generate tons of public NTP<br>traffic if you have a server in-house?! [1]) I proudly present my<br>/etc/default/ntpdate:<br><br><a href="https://gist.github.com/ste-fan/7b9b5bcf6656f59c280f">https://gist.github.com/ste-fan/7b9b5bcf6656f59c280f</a>   [2]<br><br>I tried to clean Jan's "one"-liner up a bit. Basically it still does the same<br>wonderful thing, but here's a quick changelog:<br><br>* updated DHCP lease folder to /var/lib/NetworkManager<br>  + of course there we must consider *.lease files only<br><br>* rewrote the whole method as a function (hopefully easier to read)<br><br>* reduced the use of redirection and temporary variables using slightly more<br>  complex sed scripts and utilizing more of find's capabilites<br><br>* added debug output that responds to ntpdate's -d option and will also be<br>  logged to syslog if the -s option is given (which is provided by<br>  /etc/network/if-up.d/ntpdate)<br><br>* nevertheless there is no namespace pollution, thanks to<br>  + exclusively local function variables<br>  + unsetting function definitions after execution<br><br>* the upper (default) config part can be left as is!<br>  + if NTP servers are found in the latest lease, NTPSERVERS will get updated<br>  + otherwise it won't be touched (keeping the above specified servers)<br>  + NTPDATE_USE_NTP_CONF can be left "yes" if you don't have any ntp(d).conf*<br>    files that could be read by ntpdate-debian<br>    (namely: /var/lib/ntp/ntp.conf.dhcp /etc/ntp.conf /etc/openntpd/ntpd.conf)<br><br>Well, now I hope someone finds this useful!<br><br>Cheers<br>~ste-fan<br><br><br><br>[1] OK, you could configure the in-house NTP server statically on each client.<br>    But ntpdate was made for PCs that do not run 24/7 and even Laptops that<br>    move around, right? So we need a dynamic approach with fallback NTP servers<br>    that are used if the current DHCP server does not supply NTP server<br>    information.<br><br><br>[2]<br><br># The settings in this file are used by the program ntpdate-debian, but not<br># by the upstream program ntpdate.<br><br># Set to "yes" to take the server list from /etc/ntp.conf, from package ntp,<br># so you only have to keep it in one place.<br>NTPDATE_USE_NTP_CONF=yes<br><br># List of NTP servers to use  (Separate multiple servers with spaces.)<br># Not used if NTPDATE_USE_NTP_CONF is yes.<br>NTPSERVERS="<a href="http://ntp.ubuntu.com">ntp.ubuntu.com</a>"<br><br># Additional options to pass to ntpdate<br>NTPOPTIONS=""<br><br># extract NTP servers from latest DHCP lease (if option was given by server)<br>get_debug_opts() {<br>    local opt<br>    debug_opt=false<br>    debug_syslog=false<br>    for opt in "$@" $( xargs -0 < /proc/$$/cmdline ); do<br>        case "$opt" in<br>            -d) debug_opt=true ;;<br>            -s) debug_syslog=true ;;<br>        esac<br>    done<br>    return 0<br>}<br><br>dhcp_ntp_debug_msg() {<br>    if $debug_opt; then<br>        echo "$(LC_ALL=C date +'%_d %b %H:%M:%S')" "ntpdate[$$]:" \<br>             '(DCHP)' "$@"<br>        if $debug_syslog; then<br>            logger --tag "ntpdate[$$]" '(DCHP)' "$@"<br>        fi<br>    fi<br>    return 0<br>}<br><br>get_dhcp_ntp_servers() {<br>    local debug_opt debug_syslog leasefiledir recentleasefile ntpservers<br><br>    leasefiledir=/var/lib/NetworkManager<br><br>    # check whether we are in debug mode and logging to syslog<br>    get_debug_opts $NTPOPTIONS<br><br>    # get most recent lease file that is not older than 1 minute<br>    recentleasefile=$( find $leasefiledir ! -type d -iname '*.lease' \<br>                            ! -mmin +1 -printf '%T@ %p\n' 2>/dev/null \<br>                         | sort -k 1nr | sed 's/^\S\+ //; 1q' )<br><br>    if [ -n "$recentleasefile" ]; then<br>        dhcp_ntp_debug_msg 'Found a recent lease file:' \<br>                           $( basename $recentleasefile )<br>        # extract NTP servers, if any  (check only latest lease in file)<br>        ntpservers=$( sed '1,/lease\s\+{/d<br>                           /option ntp-servers/!d<br>                           s/^.*ntp-servers\s\+\(.\+\);$/\1/; s/,/ /g' \<br>                        $recentleasefile )<br>        if [ -n "$ntpservers" ]; then<br>            dhcp_ntp_debug_msg 'Found NTP server(s) in latest lease:' \<br>                               $ntpservers<br>            NTPSERVERS="$ntpservers"<br>        else<br>            dhcp_ntp_debug_msg 'Latest lease does not contain any NTP servers.'<br>        fi<br>    else<br>        dhcp_ntp_debug_msg 'There is no recent lease file.'<br>    fi<br><br>    return 0<br>}<br><br>get_dhcp_ntp_servers<br><br>unset get_debug_opts dhcp_ntp_debug_msg get_dhcp_ntp_servers<br>​</span></div><span style="font-family:courier new,monospace"><br></span></div>