Update system time using NTP and NetworkManager (Gentoo)

·1 min read·paskov

On my laptop, I use NetworkManager to manage the LAN and WLAN interfaces.

The dispatcher script below starts ntp-client to synchronize the system clock when eth0 or wlan0 comes up. It stops the service when either interface goes down and ignores other interfaces and events. Replace eth0 and wlan0 with your interface names if they differ.

As root, save the following script as /etc/NetworkManager/dispatcher.d/10-ntp-client:

#!/bin/bash
IF=$1
STATE=$2

if [ "$IF" = "eth0" ] || [ "$IF" = "wlan0" ]; then
    if [ "$STATE" = "up" ]; then
        /etc/init.d/ntp-client start
    elif [ "$STATE" = "down" ]; then
        /etc/init.d/ntp-client stop
    fi
fi

Then make it executable:

chmod +x /etc/NetworkManager/dispatcher.d/10-ntp-client

The script requires Gentoo’s net-misc/ntp package and its /etc/init.d/ntp-client service.

Comments 1

  1. Dawid

    I have corrected the script a little

    #!/bin/bash
    IF=$1;
    STATE=$2;
    
    if [ "$IF" = "wlp3s0" ]; then
    	if [ "$STATE" = "up" ]; then
    		rc-service ntp-client restart;
    	elif [ "$STATE" = "down" ]; then
    		rc-service ntp-client stop;
    	fi
    fi

    But in general it will not work when configured with more interfaces; you don’t want to stop ntp-client when any of your interfaces goes down but just when all of them are down.