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
	
   (
     sleep 5 && 
     service ntp stop && 
     ntpd -qg ; 
     service ntp start; 
     touch /tmp/force_update
   ) &  

Finally make sure the file is readable:
chmod a+r /etc/dhcp/dhclient-exit-hooks.d/ntp_force_update

Local testing at the prompt:
dhclient


-- PeterZumbruch - 18 Feb 2016
-- PeterZumbruch - 2016-02-18
Topic attachments
I Attachment Action Size Date Who Comment
_etc_dhcp_dhclient-exit-hooks.d_hostnamed_hostname _etc_dhcp_dhclient-exit-hooks.d_hostname manage 0.8 K 2016-02-11 - 10:35 PeterZumbruch /etc/dhcp/dhclient-exit-hooks.d/hostname
Edit | Attach | Print version |  PDF | History: r3 < r2 < r1 | Backlinks | View wiki text | Edit WikiText | More topic actions...
Topic revision: r1 - 2016-02-18, 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)