sinbad
Posts: 11
Joined: Sun Mar 16, 2014 8:48 pm

DHCP lease not renewing automatically

Sun Mar 16, 2014 9:21 pm

I have an Iogear usb wifi in my rpi, that connects just fine with my d-link router. When I boot the rpi it gets an IP via DHCP. If I ifdown/ifup wlan0, it also gets an IP address via DHCP. And if I enter

dhclient -v wlan0

while the lease is still up, that appears to work as well, according to the output.

Here's the problem:

If I do not do anything with dhclient until the lease expires, which is usually between 32000 and 42000 seconds, it expires and does not automatically get renewed as it should. Typing: "dhclient -v wlan0" does not have any effect. I have to type: "ifup wlan0" to get it to renew the lease.

And if I type: "dhclient -r wlan0", that releases the lease, but also puts wlan0 in an apparent down state. Again, the only way to get a new lease is to type "ifup wlan0". Typing "dhclient wlan0" does not have any effect.

The real issue is that I need this thing to stay connected with an active lease as long as it and the router are up and running ok. It is going to run unattended and inaccessible, so I will not be able to get to it to type "ifup" when this happens.

It is my understanding that the dhcp client is supposed to renew the lease when half the expiration time has gone by, and several times after that if it needs to.

What do I have to do to get it to renew automatically?

And is there a way to request a specific lease time?

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: DHCP lease not renewing automatically

Mon Mar 17, 2014 6:22 am

sinbad wrote:And if I type: "dhclient -r wlan0", that releases the lease, but also puts wlan0 in an apparent down state. Again, the only way to get a new lease is to type "ifup wlan0". Typing "dhclient wlan0" does not have any effect.
This is weird. The interface should not go down when you send a DHCPRELEASE to the dhcp server. With that request you just deactivates the ip from that interface and tell the dhcp server to make it available to other clients. Nothing to do with the state of your interface.

Investigate this first by looking at any strange messages in syslog.

You can also try to manually deactivate the IP from wlan0 and see if it goes down:

Code: Select all

$ sudo ip address del 192.168.0.66/24 dev wlan0
$ sudo ifconfig wlan0
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

sinbad
Posts: 11
Joined: Sun Mar 16, 2014 8:48 pm

Re: DHCP lease not renewing automatically

Mon Mar 17, 2014 3:14 pm

Yeah, that's what I thought too -- strange that it should go down. If I do a ifconfig it shows as UP, but then if I do "ifup wlan0" it does not tell me that it is already up, but it actually gets a new dhcp address. I will try your suggestions, and also try the same things on the eth0 interface for comparison. It's hard to believe that there is a problem with DHCP code, which should be mature. I'll try some more stuff to try to figure out what is going on.

Thanks for the feedback!

sinbad
Posts: 11
Joined: Sun Mar 16, 2014 8:48 pm

Re: DHCP lease not renewing automatically

Tue Mar 18, 2014 4:02 am

OK, did some more research. First off, "ip address del a.b.c.d/nn dev wlan0" has the same net effect as "dhclient -r wlan0", so I stuck with the dhclient command.

Second, when I have both eth0 and wlan up and configured, things work somewhat differently, in fact better, but that isn't how I need this beast to work, so I unplugged eth0, and have only wlan0 active.

So, starting with wlan0 up and with an IP, here is the series of commands and results, trimming unnecessary stuff:

Show wlan0 status:

$ ifconfig

wlan0 Link encap:Ethernet HWaddr 00:21:79:c5:45:96
inet addr:192.168.0.106 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

Release wlan0 IP:

$ sudo dhclient -v -r wlan0

Internet Systems Consortium DHCP Client 4.2.2
Copyright 2004-2011 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/00:21:79:c5:45:96
Sending on LPF/wlan0/00:21:79:c5:45:96
Sending on Socket/fallback
DHCPRELEASE on wlan0 to 192.168.0.1 port 67


Show wlan0 status:

$ ifconfig

wlan0 Link encap:Ethernet HWaddr 00:21:79:c5:45:96
UP BROADCAST MULTICAST MTU:1500 Metric:1


Get new wlan0 IP:

$ sudo dhclient -v wlan0

Internet Systems Consortium DHCP Client 4.2.2
Copyright 2004-2011 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/00:21:79:c5:45:96
Sending on LPF/wlan0/00:21:79:c5:45:96
Sending on Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 5
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 6
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 13
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 20
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 17
No DHCPOFFERS received.
Trying recorded lease <null address> <=== looks like a clue
Error: an inet prefix is expected rather than "<null". <=== looks like a clue
bound: immediate renewal.
DHCPREQUEST on wlan0 to 255.255.255.255 port 67 <=== that's all there is


Try again:

$ sudo dhclient -v wlan0

Internet Systems Consortium DHCP Client 4.2.2
Copyright 2004-2011 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/00:21:79:c5:45:96
Sending on LPF/wlan0/00:21:79:c5:45:96
Sending on Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 5
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 6
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 10
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 16
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 13
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 11
No DHCPOFFERS received.
Trying recorded lease <null address> <=== same stuff
Error: an inet prefix is expected rather than "<null".
bound: immediate renewal.
DHCPREQUEST on wlan0 to 255.255.255.255 port 67


Now do ifup instead of dhclient:

$ sudo ifup -v wlan0

wpa_supplicant: ctrl_interface socket located at /var/run/wpa_supplicant/wlan0

dhclient -v -pf /run/dhclient.wlan0.pid -lf /var/lib/dhcp/dhclient.wlan0.leases wlan0
Internet Systems Consortium DHCP Client 4.2.2
Copyright 2004-2011 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/00:21:79:c5:45:96
Sending on LPF/wlan0/00:21:79:c5:45:96
Sending on Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 7
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 14
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 7
DHCPREQUEST on wlan0 to 255.255.255.255 port 67
DHCPOFFER from 192.168.0.1
DHCPACK from 192.168.0.1
bound to 192.168.0.106 -- renewal in 42689 seconds.
run-parts --verbose /etc/network/if-up.d
run-parts: executing /etc/network/if-up.d/mountnfs
run-parts: executing /etc/network/if-up.d/openssh-server
run-parts: executing /etc/network/if-up.d/upstart
run-parts: executing /etc/network/if-up.d/wpasupplicant


$ ifconfig

wlan0 Link encap:Ethernet HWaddr 00:21:79:c5:45:96
inet addr:192.168.0.106 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1


So ... ifup works, but dhclient does not -- why???????

One question is, what is it trying to do here, and where is it trying to find something?

Trying recorded lease <null address> <=== same stuff
Error: an inet prefix is expected rather than "<null".

The only file I can find that might constitute a "recorded lease" is this one, which looks ok to me:

$ cat /var/lib/dhcp/dhclient.wlan0.leases

lease {
interface "wlan0";
fixed-address 192.168.0.106;
option subnet-mask 255.255.255.0;
option routers 192.168.0.1;
option dhcp-lease-time 86400;
option dhcp-message-type 5;
option domain-name-servers 192.168.0.1;
option dhcp-server-identifier 192.168.0.1;
renew 2 2014/03/18 03:33:48;
rebind 2 2014/03/18 03:33:48;
expire 2 2014/03/18 03:33:48;
}
lease {
interface "wlan0";
fixed-address 192.168.0.106;
option subnet-mask 255.255.255.0;
option routers 192.168.0.1;
option dhcp-lease-time 86400;
option dhcp-message-type 5;
option domain-name-servers 192.168.0.1;
option dhcp-server-identifier 192.168.0.1;
renew 2 2014/03/18 15:31:16;
rebind 3 2014/03/19 00:39:46;
expire 3 2014/03/19 03:39:46;
}

Looks fine to me. The errors must be coming from some other file, but ... what???

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: DHCP lease not renewing automatically

Tue Mar 18, 2014 7:56 am

Your interface does apparently not go down on renewal. But there is still something strange with your dhcp client or server. Restart the server and rm all files in /var/lib/dhcp/ directory. If the problem persists, search syslog and/or syslog.1 for any clues as it logs the dhcp requests: $ grep DHCP /var/log/syslog

Back to your questions
sinbad wrote:What do I have to do to get it to renew automatically?
And is there a way to request a specific lease time?
  1. Normally nothing. It's automatic when the dhcp client reaches its renewal time but your renewal request seems to fail for some reason.
  2. You have the choice here:
    • set a longer lease time on the DHCP server
    • on the server: provide a reserved IP to your Pi based on its mac address
    • On the Pi's /etc/dhcp/dhclient.conf file: add or uncomment send dhcp-lease-time xxxxxxxx; (don't forget the final semi-column)
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

sinbad
Posts: 11
Joined: Sun Mar 16, 2014 8:48 pm

Re: DHCP lease not renewing automatically

Tue Mar 18, 2014 3:02 pm

I activated the send dhcp-lease-time line, made it 60 seconds just so I can get fast results, and it is now working just fine.

I changed it to 600, and I'll keep an eye on it for a day, then change it to 86400 and see how it does.

Maybe the pi just wasn't getting the dhcp server's time, so it didn't know when to renew?

I'll update this when I'm sure it's working ok.

Thanks for all your help!
Sinbad

User avatar
emgi
Posts: 357
Joined: Thu Nov 07, 2013 4:08 pm
Location: NL

Re: DHCP lease not renewing automatically

Tue Mar 18, 2014 5:03 pm

Your D-Link router may not be fully compliant to the RFC; many of these home-routers have crappy implementations of DHCP.
Normally you don't notice that because it works more or less OK with a PC and of course your lease hardly ever expires.
Actually I don't think the problem is on the linux-side.

It would be interesting to see how this behaves with a proper DHCP server. (either Microshit or Linux)
Yes, I mean to disable dhcp on the D-Link and get DHCP from another box, just for testing.
This may not be considered as a permanent solution but at least you would have a hunch where to look.

/emgi

sinbad
Posts: 11
Joined: Sun Mar 16, 2014 8:48 pm

Re: DHCP lease not renewing automatically

Tue Mar 18, 2014 5:23 pm

Good idea, thanks. I agree about the crappy home routers. I've been meaning to build my own, but ... time.

Do you have any recommendations for good router / firewall software? I think pfsense looks pretty good, but haven't tried it yet. And I need it to be a wifi access point. Or maybe just hang my crappy d-link on the local side to do that job, and take dhcp from pfsense...?

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: DHCP lease not renewing automatically

Wed Mar 19, 2014 5:53 am

If you want to replace your router/firewall/dhcp server, I strongly recommend any compatible router flashed with OpenWrt. It is Open Source, compliant and rock stable. I personally have a Buffalo WZR-HP-G300NH with OpenWrt "Attitude Adjustment", the latest release. Lots of ram and oompf for a router.

If you don't need a router with WiFi, don't bother building your own, it could be cheaper to buy something like this:
http://www.ubnt.com/edgemax#edge-router-lite

Alternatively, you could keep your crappy D-link for routing and firewall and use a... RPi as DHCP server running either isc-dhcp-server or dnsmasq. The latest being easier to install and featuring also a nice and useful DNS cache.
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

sinbad
Posts: 11
Joined: Sun Mar 16, 2014 8:48 pm

Re: DHCP lease not renewing automatically

Wed Mar 19, 2014 1:46 pm

Good ideas. I really like the Ubiquiti equipment. But I can't figure out if they have what I need or not. I need to replace my D-link router with something that has the features of the D-link, and hopefully more/better features, and a wifi AP. These can be two separate units, no problem. Has to have a good firewall. D-link has a firewall, but when I run wireshark on my computer I see floods of packets I think should be filtered out by the firewall, so the D-link firewall doesn't really seem to work.

I like the "Ubiquiti AirRouter Indoor WiFi router"
http://www.amazon.com/Ubiquiti-AirRoute ... s=ubiquiti

But it is 802.11n only. Will that work with my existing laptops and smart phone around the house? What if they are 802.11g only?

So many decisions.

Anyhow, my raspi is getting and maintaining its dhcp ip address just fine now.; Love this little guy. Can't wait to build it into a real application.

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: DHCP lease not renewing automatically

Wed Mar 19, 2014 3:10 pm

sinbad wrote:Anyhow, my raspi is getting and maintaining its dhcp ip address just fine now.; Love this little guy. Can't wait to build it into a real application.
Use it as dhcp server with dnsmasq! And if you do, don't forget to deactivate dhcp on the d-link otherwise you will wreak havoc on your lan!
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

sinbad
Posts: 11
Joined: Sun Mar 16, 2014 8:48 pm

Re: DHCP lease not renewing automatically

Wed Mar 19, 2014 3:30 pm

Good idea.

For this one, I'm working on an app that will let me install the pi in a remote location, with wifi access to the internet, and have it sense things like motors running, temperature, etc., and report periodically by email to me, or on demand to my queries. I just need to make sure I can find a way through the local wifi router firewall in order to let the pi communicate with me. Should be no problem, but until I get there, I won't know what they have for firewall port/protocol restrictions. For sure port 80 is always available, so I might make use of that if all else fails.

girish_kumar
Posts: 31
Joined: Thu Sep 12, 2013 5:59 pm

Re: DHCP lease not renewing automatically

Wed Feb 10, 2016 5:58 pm

I had the same problem, it got fixed with I moved to Jessie image, which i available for free download at Raspberry Pi site

Regards
Girish

DirkS
Posts: 10214
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: DHCP lease not renewing automatically

Wed Feb 10, 2016 6:43 pm

girish_kumar wrote:I had the same problem, it got fixed with I moved to Jessie image, which i available for free download at Raspberry Pi site

Regards
Girish
Well, it wasn't when the problem was posted... 2 years ago :roll:

Acedrew
Posts: 1
Joined: Tue Oct 11, 2016 6:52 pm

Re: DHCP lease not renewing automatically

Tue Oct 11, 2016 6:56 pm

So even on my updated Jessie Pixel RPi I was still having this same issue. I've tried everything to do with the wifi dongle itself, power management, and more, but this is the first thing that seems to have similar issues with a plausible solution.

Return to “Troubleshooting”