Remove ip address when cable is disconnected

Pieter Cardoen

New Member
Joined
Sep 16, 2019
Messages
6
Reaction score
0
Credits
39
Dear

We are building embedded devices which are sometimes hot plugged during development. We are configuring the network using /etc/network interfaces (dhcp). When the cable is disconnected and connected to another device, the IP address persists on the interface which results in inability to connect to the device.

Is it possible to give advice how to configure the network more decently so it removes the IP address when the cable is disconnected.

Thanks
Pieter Cardoen
 


The easiest way to do this, is the "lease timeout" on the dhcp server.
I don't know where your dhcp server is.. (usually it's your cable modem/router)

If the lease is a long period of time (often 3 days) that means the dhcp server will "reserve"
that IP address for the last MAC address that was using it. Even if the device is turned off, dhcp will keep holding onto it.

You can set the dhcp lease time to something very short ( 5 minutes or so). Usually dhcp servers will not let you set the lease below 2 minutes. The problem with this is, if you you don't reconnect within 5 minutes, you are almost guaranteed to get a different IP address. The dhcp server only "remembers" your MAC address for 5 minutes, and then it forgets about it.
 
Dear dos2unix

this is actually not really possible. We are developing train equipment. Sometimes during development something goes wrong with a device, we disconnect it and connect it to a computer while leaving powered. In this case we need to detect link down and forget our lease as client device.

Kind regards
Pieter
 
If it's only the client side you care about... and it's Linux, you can try something like this.

dhclient -r -v eth0 && rm /var/lib/dhcp/dhclient.* ; dhclient -v eth0

Your interface might not be called eth0, so you might have to change it slightly.
 

Members online


Top