13

I am currently trying to set up my Raspberry Pi as an Access Point using hostapd using the following directions:

RPI Wireless Hotspot

Unfortunately, my static IP Address refuses to set at startup. Below is the contents of my /etc/network/interfaces:

auto lo

iface lo inet loopabck
iface eth0 inet dhcp

iface wlan0 inet static
address 192.168.42.1
broadcast 192.168.42.255
netmask 255.255.255.0

up iptables-restore < /etc/iptables.ipv4.nat

Oddly enough, if I run the following command after boot, everything works fine:

sudo ifconfig wlan0 192.168.42.1

Does anybody see any glaring issues that I may be missing? It's been so long since I've worked with Linux configuration that I feel that I'm missing something easy.

12
  • As a quick workaround you could just run the ifconfig as a boot script after interfaces are connected or the last boot scripts. Did you upgrade the firmware to latest? Not aptitude update but raspi script for firmware. That fixes a few interface issues.
    – Piotr Kula
    Sep 19, 2013 at 19:24
  • @ppumkin - I'm still a Raspberry Pi n00b. What's the command to run the raspi script to update the firmware? Sep 19, 2013 at 19:29
  • 4
    github.com/Hexxeh/rpi-update Go there and read it. Also after restart do an aptitude update too.
    – Piotr Kula
    Sep 19, 2013 at 19:40
  • @ppumkin - I've run both updates and neither took care of the issue. Looks like I'm going to have to go with the workaround for now. Sep 19, 2013 at 20:24
  • The last resort is trying to burn the Rasbpian from fresh using the latest image. Do the updates again and see if that helps. That helped with mi WiFi problems.
    – Piotr Kula
    Sep 19, 2013 at 20:51

9 Answers 9

4

My interfaces file look a bit different but works for me all the time.

auto lo

iface lo inet loopback
iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0

iface wlan0 inet static
    address 192.168.1.123
    netmask 255.255.255.0
    network 192.168.1.1
    broadcast 255.255.255.255
    gateway 192.168.1.1
    wpa-ssid "SSID"
    wpa-psk "PASSWORD"

Maybe this might help

2
  • Shouldn't the broadcast IP look like this: 192.168.1.255, the 255 is just on last place because otherwise you don't broadcast on all devices of your router.
    – XandruCea
    Dec 31, 2013 at 17:34
  • I believe the missing piece from the original question was the above "auto wlan0" line. You may also want to add a "dns-nameservers 8.8.8.8" (as an example) after the broadcast line.
    – joatd
    Jan 25, 2014 at 22:53
2

Disable ifplugd, then static IP address should work.

1
  • works for me! /etc/default/ifupd, change auto/all => eth0
    – flotto
    May 31, 2015 at 15:36
2

I suspect that hostapd reinitialises the interface which is what causes this to fail. You can manually add the ifconfig command to the /etc/rc.local file which will auto-execute it on boot.

2

It is definitely a race condition in raspbian jessie, which has stopped using runlevel based init in favour of a package-tree-based "start everything at the same time" approach. I think I might have got round it by putting these lines in /etc/rc.local:

/sbin/ifdown wlan0
/sbin/ifup wlan0
/sbin/ifconfig wlan0 192.168.42.1
/usr/sbin/service isc-dhcp-server start

Time will tell I suppose.

7
  • OK, unix certainly had runlevels and telinit when I worked on it from 1984 to 1992, but have regretfully only returned recently. Maybe if we go through all the things it is not, someone will explain what it actually is. (My fix is still working, but I don't like it).
    – Colin Wray
    Nov 11, 2015 at 15:31
  • I decided it was a race condition because there are two possible outcomes of the boot sequence, and they occur randomly.
    – Colin Wray
    Nov 11, 2015 at 15:40
  • Yikes. I apologize for having misread this answer backward -- that it was a problem caused by SysV init fixed w/ systemd. In any case, neither of them can prevent a race condition, since by nature many of the services they start fork and persist. Misconfiguring anything can cause a problem and what I do object to here is the implication that systemd is by nature to blame (it could be argued it has a better methodology for resolving dependencies, and it does not simply "start everything at the same time" willy-nilly -- but this is going off on a tangent)...
    – goldilocks
    Nov 12, 2015 at 0:31
  • I have a slightly unorthodox attitude toward networking on linux boxes generally which leaves me with little perspective on problems like this, and if you have a solution which works for you, great, but your diagnosis seems very conjectural (looks alike a race condition, walks like a race condition...but the shadow of a duck is not a duck...etc).
    – goldilocks
    Nov 12, 2015 at 0:32
  • 1
    On reading my OP I can see why you thought I was blaming the system directly, and why it fired up a staunch defender like yourself. No, I think the problem must lie in the package dependency tree which they profess to be following, and which I know zero about. Somehow it must recognise the clash between ifup and hostapd.
    – Colin Wray
    Nov 13, 2015 at 6:36
1

I would be trying to add the following lines just above your current wirless interface config:

auto wlan0
allow-hotplug wlan0

This tells your interface config to start the wlan0 interface automatically, and that it may be inserted later (hotplug).

You may also need to add a gateway <IP Address> line as well to get routing working.

1
  • This was the thing that did the trick for me! Thanks.
    – johnboiles
    Sep 22, 2014 at 3:19
0

Not necessarily a great solution but since I had problems with my staticip when using /etc/network/interfaces I switched to using a static ip through my router instead? Have you tried it this way as an alternative?

0

I follow the same tutorial to setup the WAP on my Pi.

Unfortunately, the dhcp service doesn’t start at boot. After the boot, checking the service shows that it is not running : service isc-dhcp-server status Status of ISC DHCP server: dhcpd is not running

Actually, the interface wlan0 doesn’t get an IP address and that’s why the dhcp server doesn’t start.

I follow the instruction in the link below, but it doesn’t solve the issue : http://sirlagz.net/2013/02/10/how-to-use-the-raspberry-pi-as-a-wireless-access-pointrouter-part-3b/

Then, I follow the instruction in this post : http://www.novitiate.co.uk/?p=183

nano /etc/rc.local

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

# Print the IP address
sleep 5
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
      sudo ifup wlan0                           #start wlan0 with ip adress in  /etc/network/interfaces
#      sudo service hostapd start               #not necessary if you already start hostapd as a service
      sudo service isc-dhcp-server start        #start dhcp server
exit 0

After the reboot, the DHCP server is running.

Also a possible another solution that I don't test : oops, I don't have enough reputation to post more than 2 links.

0

I have had the same problem. wlan0 loses it's static ip address somehow when the dhcp server starts. Edit /etc/default/ifplugd and change to this:

INTERFACES="eth0"
HOTPLUG_INTERFACES="eth0"
2
  • This answer is corrent! don't know why voted down
    – flotto
    May 31, 2015 at 15:35
  • I was having the pi boot, and sometimes it would end up with a 169.xx.xx.xx address, and sometimes the static address I wanted. This is was made it reliably get the static address. Thanks @Stef! Aug 8, 2015 at 10:12
0

Alternatively, you can create a mini-service which restarts the wlan0, making sure it starts at boot before hostapd and isc-dhcp-server.

Create the new script /etc/init.d/wlan0-restart

sudo vi /etc/init.d/wlan0-restart

with the following content:

#!/bin/sh
#
#

### BEGIN INIT INFO
# Provides:          wlan0-restart
# Required-Start:    $network
# Required-Stop:     $network
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Restarts wlan0 interface
# Description:       Restarts wlan0 interface to fix problem with static address in WiFi AP
# X-Start-Before:    hostapd isc-dhcp-server
### END INIT INFO

PATH=/sbin:/usr/sbin:$PATH

do_start() {
  ifdown wlan0
  ifup wlan0
}

case "$1" in
  start)
    do_start
    ;;
  restart|reload|force-reload)
    echo "Error: argument '$1' not supported" >&2
    exit 3
    ;;
  stop)
    ;;
  status)
    ;;
  *)
    echo "Usage: $0 start|stop" >&2
    exit 3
    ;;
esac
exit 0

Then execute the following commands:

sudo chmod 755 /etc/init.d/wlan0-restart
sudo update-rc.d –f hostapd remove
sudo update-rc.d –f isc-dhcp-server remove
sudo update-rc.d wlan0-restart defaults
sudo update-rc.d hostapd defaults
sudo update-rc.d isc-dhcp-server defaults

Reboot and check that wlan0 has its static IP address and that isc-dhcpd-server is running:

pi@raspberrypi:~$ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr e8:de:27:07:92:6a  
          inet addr:192.168.10.1  Bcast:192.168.10.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

pi@raspberrypi:~$ pgrep -l dhcpd
2315 dhcpd

Not the answer you're looking for? Browse other questions tagged or ask your own question.