#627343 network-manager doesn't use ntp from dhcp

Package:
network-manager
Source:
network-manager
Description:
network management framework (daemon and userspace tools)
Submitter:
sergio
Date:
2014-10-17 07:21:05 UTC
Severity:
normal
#627343#5
Date:
2011-05-19 18:21:20 UTC
From:
To:
network-manager's dhcp script (/usr/lib/NetworkManager/nm-dhcp-client.action)
doesnt't get ntp server as /sbin/dhclient-script from isc-dhcp-client does.
And doesn't restart ntp.

#627343#10
Date:
2011-05-19 18:32:56 UTC
From:
To:
Am 19.05.2011 20:21, schrieb sergio:

Could you be more specific what the problem is?

Why should NM restart ntp?


I'd say this is a duplicate of
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=537358 , right?

Michael

#627343#15
Date:
2011-05-19 21:03:48 UTC
From:
To:
The problem is that ntp server offered by dhcpd not used by client with
network manager.
NM should get original ntp config, replace servers with offered by dhcpd
and restart ntp with new config, as dhclient does this.

#627343#20
Date:
2013-10-01 05:08:03 UTC
From:
To:
Hi All,


finally got irritated enough to make a workaround for this long
standing issue: https://bugs.launchpad.net/ubuntu/+s...er/+bug/267891
and http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=627343.


My issue with this is obvious: it does not work as expected.


Other sugested workarounds are to install ntpd on every system using
dhcp client. Since dhcp clients are on average desktop clients (not
allowed to run any network facing services) or servers in initial
installation stages, someone who suggest this should have their head
examined.


Apparently, it's below NetworkManager's standing to call ntpdate with
DHCP supplied ntp-servers, just hard configured ones in
/etc/default/ntpdate.


Every workaround has, ideally, to fit in CMDB managed environments,
which means you can't, again ideally, change core files of packages
but got to work with files you are allowed to change. In this case it
is possible to do it through /etc/default/ntpdate.


Background: NetworkManager does not call
/etc/dhcp/dhclient-exit-hooks.d/ntpdate but does call
/etc/network/if-up.d/ntpdate, (hard coded in
/etc/NetworkManager/dispatcher.d/01ifupdown) allthough in
/etc/NetworkManager/NetworkManager.conf, section [ifupdown], "managed"
is set to false, which implies not using it. This file,
/etc/network/if-up.d/ntpdate, does call /usr/sbin/ntpdate-debian,
another Frank Sinatra solution. This script sources
/etc/default/ntpdate.


Other attempts, for instance trying to intercept the new_ntp_servers,
failed on the requirement not to change core files.


Yes, it's one long line, it is overkill written, but in my personal
belief system ("All hail the cpu!") it is better to write
understandable code. Be glad I didn't CamelCase it.


It allows for still using the static ntp servers, only takes action if
there is a recent (1 minute) lease file which contains a non-zero
ntp-servers option.


/etc/default/ntpdate:


# The settings in this file are used by the program ntpdate-debian, but not
# by the upstream program ntpdate.


# Set to "yes" to take the server list from /etc/ntp.conf, from package ntp,
# so you only have to keep it in one place.
NTPDATE_USE_NTP_CONF=no


# List of NTP servers to use (Separate multiple servers with spaces.)
# Not used if NTPDATE_USE_NTP_CONF is yes.
# NTPSERVERS="ntp.ubuntu.com"


NTPSERVERS=$(STATICNTPSERVERS="ntp.ubuntu.com" ;
LEASEFILEDIR=/var/lib/dhcp ; NUMLEASEFILES=$(find $LEASEFILEDIR !
-type d 2>/dev/null|wc -l) ; if [ $NUMLEASEFILES != 0 ] ; then
MOSTRECENTLEASEFILE=$(ls -tr $LEASEFILEDIR | tail -n 1) ; if test
$(find $LEASEFILEDIR/$MOSTRECENTLEASEFILE ! -mmin +1 2>/dev/null) ;
then DHCPNTPSERVERS=$(grep 'option ntp-servers'
$LEASEFILEDIR/$MOSTRECENTLEASEFILE|tail -n 1|awk '{printf $3"\n"}'|tr
',' ' '|tr -d ';') ; if [ ! -z "$DHCPNTPSERVERS" ] ; then echo
$DHCPNTPSERVERS ; else echo $STATICNTPSERVERS ; fi ; else echo
$STATICNTPSERVERS ; fi ; else echo $STATICNTPSERVERS ; fi)


# Additional options to pass to ntpdate
NTPOPTIONS=""

#627343#25
Date:
2014-10-06 22:07:29 UTC
From:
To:
​​​Hi there,

since this issue really bugged me as well (why generate tons of public NTP
traffic if you have a server in-house?! [1]) I proudly present my
/etc/default/ntpdate:

https://gist.github.com/ste-fan/7b9b5bcf6656f59c280f   [2]

I tried to clean Jan's "one"-liner up a bit. Basically it still does the
same
wonderful thing, but here's a quick changelog:

* updated DHCP lease folder to /var/lib/NetworkManager
  + of course there we must consider *.lease files only

* rewrote the whole method as a function (hopefully easier to read)

* reduced the use of redirection and temporary variables using slightly more
  complex sed scripts and utilizing more of find's capabilites

* added debug output that responds to ntpdate's -d option and will also be
  logged to syslog if the -s option is given (which is provided by
  /etc/network/if-up.d/ntpdate)

* nevertheless there is no namespace pollution, thanks to
  + exclusively local function variables
  + unsetting function definitions after execution

* the upper (default) config part can be left as is!
  + if NTP servers are found in the latest lease, NTPSERVERS will get
updated
  + otherwise it won't be touched (keeping the above specified servers)
  + NTPDATE_USE_NTP_CONF can be left "yes" if you don't have any
ntp(d).conf*
    files that could be read by ntpdate-debian
    (namely: /var/lib/ntp/ntp.conf.dhcp /etc/ntp.conf
/etc/openntpd/ntpd.conf)

Well, now I hope someone finds this useful!

Cheers
~ste-fan



[1] OK, you could configure the in-house NTP server statically on each
client.
    But ntpdate was made for PCs that do not run 24/7 and even Laptops that
    move around, right? So we need a dynamic approach with fallback NTP
servers
    that are used if the current DHCP server does not supply NTP server
    information.


[2]

# The settings in this file are used by the program ntpdate-debian, but not
# by the upstream program ntpdate.

# Set to "yes" to take the server list from /etc/ntp.conf, from package ntp,
# so you only have to keep it in one place.
NTPDATE_USE_NTP_CONF=yes

# List of NTP servers to use  (Separate multiple servers with spaces.)
# Not used if NTPDATE_USE_NTP_CONF is yes.
NTPSERVERS="ntp.ubuntu.com"

# Additional options to pass to ntpdate
NTPOPTIONS=""

# extract NTP servers from latest DHCP lease (if option was given by server)
get_debug_opts() {
    local opt
    debug_opt=false
    debug_syslog=false
    for opt in "$@" $( xargs -0 < /proc/$$/cmdline ); do
        case "$opt" in
            -d) debug_opt=true ;;
            -s) debug_syslog=true ;;
        esac
    done
    return 0
}

dhcp_ntp_debug_msg() {
    if $debug_opt; then
        echo "$(LC_ALL=C date +'%_d %b %H:%M:%S')" "ntpdate[$$]:" \
             '(DCHP)' "$@"
        if $debug_syslog; then
            logger --tag "ntpdate[$$]" '(DCHP)' "$@"
        fi
    fi
    return 0
}

get_dhcp_ntp_servers() {
    local debug_opt debug_syslog leasefiledir recentleasefile ntpservers

    leasefiledir=/var/lib/NetworkManager

    # check whether we are in debug mode and logging to syslog
    get_debug_opts $NTPOPTIONS

    # get most recent lease file that is not older than 1 minute
    recentleasefile=$( find $leasefiledir ! -type d -iname '*.lease' \
                            ! -mmin +1 -printf '%T@ %p\n' 2>/dev/null \
                         | sort -k 1nr | sed 's/^\S\+ //; 1q' )

    if [ -n "$recentleasefile" ]; then
        dhcp_ntp_debug_msg 'Found a recent lease file:' \
                           $( basename $recentleasefile )
        # extract NTP servers, if any  (check only latest lease in file)
        ntpservers=$( sed '1,/lease\s\+{/d
                           /option ntp-servers/!d
                           s/^.*ntp-servers\s\+\(.\+\);$/\1/; s/,/ /g' \
                        $recentleasefile )
        if [ -n "$ntpservers" ]; then
            dhcp_ntp_debug_msg 'Found NTP server(s) in latest lease:' \
                               $ntpservers
            NTPSERVERS="$ntpservers"
        else
            dhcp_ntp_debug_msg 'Latest lease does not contain any NTP
servers.'
        fi
    else
        dhcp_ntp_debug_msg 'There is no recent lease file.'
    fi

    return 0
}

get_dhcp_ntp_servers

unset get_debug_opts dhcp_ntp_debug_msg get_dhcp_ntp_servers
​

#627343#30
Date:
2014-10-17 06:36:25 UTC
From:
To:
ste-fan,

Glad to see someone else is, again, trying to get this resolved.

I think, though that any solution involving ntpdate is dead-on-arrival, since
ntpdate is no longer developed upstream and is deprecated in favor of
"ntpd -g -q".

As to Jan's objection to running an NTP daemon on a DHCP client: Concerns
about network-facing services should be addressed with proper firewalling,
part of any correct system installation.  And running an NTP client
continuously makes perfect sense to me, since client machines often need
their clocks closely synchronized to their servers.  So I see no reason to
force the "-q" option onto ntpd.  And "-g" is already used by default.

I agree with Michael that this is a duplicate of http://bugs.debian.org/537358
(and should be marked as such).

I still don't understand why NM upstream replaced the default call to
/sbin/dhclient-scipt with nm-dhcp-client.action, and all the D-bus stuff.
Some enlightenment there would be appreciated.

I haven't converged on my solution yet, but it so far appears to involve
installing ntp, commenting out the "server" lines in /etc/ntp.conf, and
probably using the script from bug 537358.