30

Raspberry Pi Model B
OS: Raspbian

I am running my Pi headless (currently through a direct Ethernet connection). I have a USB WiFi dongle. I would like to have the WiFi start up at boot and connect by DHCP.

http://rpi.tnet.com/project/faqs/headlessportablewifi suggests that I don't need to actually change /etc/network/interfaces from the default:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

I have configured /etc/wpa_supplicant/wpa_supplicant.conf for my network.

When I boot the Pi, WiFi does not immediately connect. But if I SSH in and run sudo ifup wlan0, the Pi connects fine.

Isn't this something the Pi should do in its own boot process? I could presumably write tell some dotfile to run this command, but I would like to do it the proper way (and learn how my system works).

I know I can also set a static IP address for each WiFi network I want to use: How to setup multiple WiFi networks?. But I would prefer to have a simple solution where I only have to enter a couple lines on /etc/wpa_supplicant/wpa_supplicant.conf to add a new WiFi network.

What is the piece I am missing here?

katriel
  • 681
  • 1
  • 6
  • 11

3 Answers3

25

The default Raspbian /etc/network/interfaces configuration does not connect to WiFi on boot. The key lines of the interfaces man-page are:

Lines beginning with the word "auto" are used to identify the physical interfaces to be brought up when ifup is run with the -a option. (This option is used by the system boot scripts.)

...

Lines beginning with the word "auto" are used to identify the physical interfaces to be brought up when ifup is run with the -a option. (This option is used by the system boot scripts.)

The first missing piece was the line auto wlan0. As far as I know, it can go anywhere in the file.

That works until you try booting without the ethernet cable plugged in. Then, WiFi doesn't connect on boot and has to be manually triggered by running sudo ifup wlan0 or unplugging and reinserting the Wi-Fi dongle.

The problem is caused by the ifplugd daemon, which turns network devices on and off when you plug them in or remove them. You can reconfigure it, or you can simply remove it with sudo apt-get purge ifplugd.

If you have removed ifplugd, you should add the following lines to /etc/network/interfaces so that the ethernet connection will also connect when plugged in:

auto eth0
allow-hotplug eth0

I'm not sure if the auto lines are strictly necessary or if hotplug handles booting correctly in the absence of ifplugd, but this worked for me.

My working file reads:

auto lo
iface lo inet loopback

auto eth0
allow-hotplug eth0
iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
katriel
  • 681
  • 1
  • 6
  • 11
0

Now you can enable WiFi on boot by just creating a wpa_supplicant.conf file in the boot partition, which will be copied into the /etc/wpa_supplicant directory and used by the wpa_supplicant daemon so that it connects to the specified network(s) . This can be done when preparing the SDcard on another machine before insertion into the Pi. The wpa_supplicant.conf should contain something like this (for more details see the Raspberry Pi site):

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB

network={ ssid="YourWiFi" psk="YourPassword" }

Pierz
  • 844
  • 10
  • 8
0

I had the same problem. Check the r/w settings of your /etc/network/interfaces.

I did chmod 600 /etc/network/interfaces and my problem was gone.

Falko
  • 119
  • 8
Gero
  • 11