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

interface=wlan0    
driver=rtl871xdrv    
ssid=my ssid
hw_mode=g
channel=6
auth_algs=1
ieee80211n=1

this is my interfaces file

auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet static
  address 192.168.0.1
  netmask 255.255.255.0

dhcpd.conf

ddns-update-style none;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;
subnet 192.168.0.0 netmask 255.255.255.0 {
  range  192.168.0.10 192.168.0.50;
  option broadcast-address 192.168.0.255;
  option routers 192.168.0.1;
  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:192.168.0.33  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          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

address: 192.168.0.1
netmask: 255.255.255.0
broadcast: 192.168.0.255

ping to 192.168.0.1 was changed to 192.168.0.33 and got destination unreachable
ping to 192.168.0.33 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.