Update system time using ntp and NetworkManager (Gentoo)

On my laptop I use NetworkManager to control network interfaces (LAN/WLAN).

Here is a simple script that will sync your clock with Internet time when LAN or WLAN interface is up and stop ntp-client when the interface is down. Put the script in /etc/NetworkManager/dispatcher.d and make it executable (chmod +x ).

#!/bin/bash
IF=$1;
STATE=$2;
 
if [ "$IF" = "eth0" ] || [ "$IF" = "wlan0" ] && [ "$STATE" = "up" ]; then
        /etc/init.d/ntp-client start
else
        /etc/init.d/ntp-client stop
fi

In order for the script to work you need net-misc/ntp package.

One thought on “Update system time using ntp and NetworkManager (Gentoo)

  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.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*