Epics@GSI Webhome

Tips Linux Dhclient Force Time Update

Introduction

Using dhclient exit hooks scripts allows to force an update of the system date using ntp/ntpd.

Similar to TipsLinuxDhclientDynamicHostname

dhclient and its scripting capabilities on enter and exit hooks allows to react on dynamic network environments.

Provided you have root privileges this can be achieved by creating a script file ntp_force_update created in /etc/dhcp/dhclient-exit-hooks.d/ (on Debian), which is sourced by dhclient after having updated/received its DHCP informations.

#!/bin/sh
   # Filename:     /etc/dhcp/dhclient-exit-hooks.d/ntp_force_update
   # Purpose:      Used by dhclient-script to set the time of the system
   #
       
   # Do not update hostname for virtual machine IP assignments
   if [ "$interface" != "eth0" ] && [ "$interface" != "eth1" ] && [ "$interface" != "wlan0" ]
   then
           return
   fi
       
   if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] && [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]
   then
      return
   fi
     
	service ntp status >/dev/null 2>&1 && 
   (
     sleep 5 && 
     service ntp stop && 
     ntpd -qg ; 
     service ntp start; 
     touch /tmp/force_update
   ) &
   
    systemctl status systemd-timesyncd.service >/dev/null 2>&1 && 
   (
     sleep 5 && 
     systemctl status systemd-timesyncd.service stop && 
     timedatectl ; 
     systemctl status systemd-timesyncd.service start; 
     touch /tmp/force_update
   ) &
Applying those code lines:
curl -s https://wiki.gsi.de/foswiki/pub/Epics/TipsLinuxDhclientForceTimeUpdate/_etc_dhcp_dhclient-exit-hooks.d_ntp_force_update -o /etc/dhcp/dhclient-exit-hooks.d/ntp_force_update && 
   dos2unix /etc/dhcp/dhclient-exit-hooks.d/ntp_force_update && 
   chmod a+r /etc/dhcp/dhclient-exit-hooks.d/ntp_force_update

Local testing at the prompt:
dhclient


-- PeterZumbruch - 2018-05-02
-- PeterZumbruch - 2018-05-02

Topic attachments
I Attachment Action Size Date Who Comment
_etc_dhcp_dhclient-exit-hooks.d_ntp_force_updated_ntp_force_update _etc_dhcp_dhclient-exit-hooks.d_ntp_force_update manage 0.9 K 2018-05-02 - 14:31 PeterZumbruch /etc/dhcp/dhclient-exit-hooks.d/ntp_force_update
Topic revision: r3 - 2018-05-02, PeterZumbruch
 
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding GSI Wiki? Send feedback
Imprint (in German)
Privacy Policy (in German)