Getting dynamic IP although specified a static one

Posted on

Problem :

I have a Linux machine on raspberry that function as an access point.

Linux raspberrypi 3.18.7-v7+ #755 SMP PREEMPT Thu Feb 12 17:20:48 GMT 2015 armv7l GNU/Linux

And I have a DHCP server on it. in the /etc/network/interfaces I’ve defined a static IP address however whenever I start it I get an IP which is not the static one but rather from the DHCP server. This behavior is not consistent and sometimes I get the static IP.

this is a module as part of a project that function sometimes as an AP and sometimes as Client based on parameters in a script. some of the cases when changing back to AP it gets the IP wrong although the conf files are as below in all cases.

this is my hostapd.conf

ssid=my ssid

this is my interfaces file

auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet static


ddns-update-style none;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet netmask {
  option broadcast-address;
  option routers;
  default-lease-time 600;
  max-lease-time 7200;
  option domain-name "domain";

ifconfig wlan0

wlan0     Link encap:Ethernet  HWaddr 74:da:38:26:9f:d6
          inet addr:  Bcast:  Mask:
          RX packets:129 errors:0 dropped:5369 overruns:0 frame:0
          TX packets:0 errors:0 dropped:726 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:65760423 (62.7 MiB)  TX bytes:12737873 (12.1 MiB)

ifquery wlan0


ping to was changed to and got destination unreachable
ping to works

the eth0 didn’t get an IP.

Solution :

Make sure that your eth0 and wlan0 are on different IP address ranges, and that your DHCP server only listens on the wlan0 device. Also, make sure that it doesn’t contain a leased address for the wlan0 MAC address.

Then, make sure that no other services touching wlan0 are active (I am thinking of NetworkManager, wicd and similar services). Also, you need to somehow guarantee that, when switching from “Client Mode” to “AP Mode”, no race conditions can happen. Otherwise, a stray DHCP client from the client mode may decide to change the IP address back.

There was a race condition between the DHCP client and the “AP mode”. meaning that I got an IP from my own DHCP.

I fixed it by providing the static IP I wanted, to the DHCP server with a constraint to limit it to my MAC Address only.

Leave a Reply

Your email address will not be published.