Friday, January 18, 2013

Raspberry Pi Wifi Static IP

My RPi is going to be a telepresence robot so it uses wifi. It also needs a static IP address. Here's how to configure Raspbian Wheezy with a static IP for wifi. Included are two useful techniques. Follow the series of steps and options below.

You can configure Wheezy for 1. Roaming with Static IP, below, or 2. Static IP With No Roaming, WPA2, PSK, below.

1. Roaming With Static IP

By default, Wheezy is configured for wireless roaming using wpa_supplicant, a daemon that manages roaming and authentication to access points with WPA, WPA2, WEP, or no encryption. With it, you can specify multiple access points and your RPi will join them automatically. First you'll need to add a Static IP Configuration, below.

a. Static IP Configuration

First, edit your /etc/network/interfaces and add the following to specify a static IP for demo which is any arbitrary name that we can use in the wpa_supplicant.conf to associate any network with this static IP configuration. You'll also make sure you have your wlan interface configured correctly.

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

iface demo inet static

Next, you'll configure wpa_supplicant. Add a specification for a network Without Encryption below, With Encryption, WPA2, PSK below, or Any Network Specification below.

b. Without Encryption

To specify a network without encryption, edit  /etc/wpa_supplicant/wpa_supplicant.conf, add your network specification to the end of the file as follows. Specifying key_mgmt=NONE says the AP does not require encryption. To use the static IP configuration that you added in your interfaces, set id_str="demo" or whatever name you chose.


Now proceed to the 3. Final Steps below.

2b. With Encryption, WPA2, PSK

To specify a network with WPA2 encryption using PSK (pre-shared key), edit /etc/wpa_supplicant/wpa_supplicant.conf, add a network specification to the end of the file as follows. To use the static IP configuration that you added in your interfaces, set id_str="demo" or whatever name you chose.


Now proceed to 3. Final Steps below.

2c. Any Network Specification

The bottom line is that you can add any network specification, following various online examples, to wpa_supplicant.conf and use a static IP for that access point simply by adding  id_str="demo" or whatever name you chose in interfaces.

In fact, you can have multiple strings, like home, work, traveling, demo, etc. Once you have edited wpa_supplicant.conf and interafaces, proceed to the 3. Final Steps below.

2. Static IP With No Roaming, WPA2, PSK 

This config does not use wpa_supplicant and is for WPA2 personal with PSK (Private Shared Key). The changes to your /etc/network/interfaces file will result in something like the following.

auto lo

iface lo inet loopback
iface eth0 inet static

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
wpa-passphrase yourpassphrasehere
wpa-ssid yourssidhere
wireless-channel yourchannelhere

#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp

Specify using static instead of manual for interface wlan0, then specify the address, netmask, and gateway, configure the WPA passphrase and SSID, and the wireless channel. Finally, you have to disable the wpa_supplicant configuration, and you have to disable the default dhcp behavior.

Proceed to 3. Final Steps below.

3. Final Steps

After making your configuration file changes, either reboot, or issue the following commands to restart the wlan0 interface:

sudo ifdown wlan0
sudo ifup wlan0

If you get no errors on the last command, except possibly some ioctl errors, you're a-ok.

pi@raspberrypi:/etc/network$ sudo ifup wlan0
ioctl[SIOCSIWAP]: Operation not permitted
ioctl[SIOCSIWENCODEEXT]: Invalid argument
ioctl[SIOCSIWENCODEEXT]: Invalid argument

Double-check by issuing iwconfig to make sure the RPi is joined to the access point, then use ifconfig wlan0, make sure it's configured correctly, then issue a ping to your gateway (ping and then ping or some other reliable website.

pi@raspberrypi:/etc/network$ ifconfig -a
eth0      Link encap:Ethernet  HWaddr b8:27:eb:fb:8d:50
          UP BROADCAST 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)

lo        Link encap:Local Loopback
          inet addr:  Mask:
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:36 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3480 (3.3 KiB)  TX bytes:3480 (3.3 KiB)

wlan0     Link encap:Ethernet  HWaddr 80:1f:02:86:ef:61
          inet addr:  Bcast:  Mask:
          RX packets:38 errors:0 dropped:1467 overruns:0 frame:0
          TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:841044 (821.3 KiB)  TX bytes:100717 (98.3 KiB)

pi@raspberrypi:/etc/network$ ping
PING ( 56(84) bytes of data.
64 bytes from icmp_req=1 ttl=64 time=3.07 ms
64 bytes from icmp_req=2 ttl=64 time=1.90 ms
--- ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 1.905/2.491/3.077/0.586 ms

Et voila, you have a wifi static IP. Congratulations!

Did this help you? If so, do me a favor and share via redit, twitter, Google+, etc. Thanks!


  1. Hi man! I am new to raspberry pi, Wheezy also making me more frustrate. I am really confused with Ethernet. please provide me a brief tutorial for Ethernet.

    1. Ethernet is just the low level standard/protcols that connect your computer to a hub/switch with wires. What are you having problems with?

  2. Many thanks! After many searches on the Internet, you're the only one who provides the right solution to configure two static IP addresses for both eth0 and wlan0 (and that allows the WiFi interface to work without the Ethernet cable to be connected). You made my evening...
    Philippe (France)

    1. @Philippe It makes my day to hear that! Really happy I could help. It may help others find this if you +1 / like / whatever else. :) Take care!

  3. Great job!

    Can I set the IP addresses the same for wired and wireless?

    1. As long as you don't have ethernet and wifi active at the same time, each adapter could be assigned the same IP address. I'd have to do some research to do anything more than that.

  4. great post! i hope this will solve my problem when i got home tonight.


  5. Hi - After trying about 10 sets of solutions your post has fixed it for me as well. Thanks.

  6. Thank you thank you thank you!!!

    I've been trying to get this to work all day and this is the only solution that has worked, I owe you a pint! You truly are a saint.

  7. Like the other Anonymous of Feb 11, I'd done several searches and yours were the only instructions that worked - many thanks!

  8. Just wanted to say thanks. Works sweet with Occidentalis v0.2

  9. Just wanted to say after trying quite a few things both online and just tinkering, this just worked, no messing with it, just save it and go. So, uh, thanks! =D

  10. After a lot of searching on the net and even more trail and error. Nothing worked

    This one does!!!

    Thanx a lot, this just made my day :)

  11. Thanks man, great of you.

  12. Works perfect, great to have both eth0 and wlan configured with static ip's on the same lan.

  13. Finally a tutorial that worked. You're the best, Cheers!

  14. Very helpful ... works as advertised, and straightforward to do. My complication was that I had a couple of network={} profiles in the wpa_supplicant.conf file that conflicted (associated with a newer router in a different location but with the same ssid): I had to comment one of those out to get it to work. For those that follow, if it doesn't work for you first time, look for conflicting definitions [you can comment them out with a leading '#']. I tried a couple of other methods before finding Michael's, but this one works.

  15. The best explanation I found! Thanks a lot man!

  16. The best tutorial I found on internet by far! Thanks a lot man!

  17. Is there a way to configure a static wireless IP while at the same time using wpa_supplicant.conf.
    If I do that then, I get an error saying, "Cannot use Static method with wpa_supplicant.conf"

  18. Thanks for your article.
    Is there a way to specify a static ip address for wlan0 while at the same time using wpa_supplicant.conf.
    If I do that, I get an error to the effect .... "Cannot use static method with wpa_supplicant.conf"

  19. Hello Thanks for the Tutorial it's a good one but I am having a few problems.

    I tried to set up according to: 2. STATIC IP WITH NO ROAMING, WPA2, PSK

    The two problems are that I don't know how to enter my SSID and my wireless channel because they both have whitespaces in them. I entered "2.4 GHz" for the channel (because that is the frequency on this network).

    This is the output I am seeing:

    pi@raspberry-torrentbox ~ $ sudo ifdown wlan0
    Ignoring unknown interface wlan0=default.
    pi@raspberry-torrentbox ~ $ sudo ifup wlan0
    iwconfig: unknown command "GHz"
    wpa_supplicant: wpa_action is managing ifup/ifdown state of wlan0
    wpa_supplicant: execute `ifdown --force wlan0' to stop wpa_action
    run-parts: /etc/network/if-pre-up.d/wpasupplicant exited with return code 1
    RTNETLINK answers: File exists
    Failed to bring up wlan0.

    Any help appreciated!


    1. @Jimmy either put in quotes or if that doesn't work escape the space with a \ character. If that still doesn't work it's probably easier to just use roaming. Thats what I do now.

  20. This comment has been removed by the author.


Note: Only a member of this blog may post a comment.