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
address 192.168.0.4
netmask 255.255.255.0
gateway 192.168.0.1

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.

network={
 ssid="BotThoughts"
 scan_ssid=0
 key_mgmt=NONE
 priority=5
 id_str="demo"
}

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.

network={
 ssid="your-ssid-here"
 scan_ssid=0
 psk="your-pre-shared-key-here"
 proto=RSN
 key_mgmt=WPA-PSK
 pairwise=CCMP
 auth_alg=OPEN
 priority=1
 id_str="demo"
}

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
address 192.168.0.5
netmask 255.255.255.0
gateway 192.168.0.1

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.0.4
netmask 255.255.255.0
gateway 192.168.0.1
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
pi@raspberrypi:/etc/network$

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 192.168.0.1) and then ping google.com 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:127.0.0.1  Mask:255.0.0.0
          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:192.168.0.4  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          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 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_req=1 ttl=64 time=3.07 ms
64 bytes from 192.168.0.1: icmp_req=2 ttl=64 time=1.90 ms
^C
--- 192.168.0.1 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
^C

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!

25 comments:

  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.

    ReplyDelete
    Replies
    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?

      Delete
  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)

    ReplyDelete
    Replies
    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!

      Delete
  3. Great job!

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

    ReplyDelete
    Replies
    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.

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

    jonel

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

    ReplyDelete
  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.

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

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

    ReplyDelete
  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

    ReplyDelete
  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 :)

    ReplyDelete
  11. Thanks man, great of you.

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

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

    ReplyDelete
  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.

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

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

    ReplyDelete
  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"

    ReplyDelete
  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"

    ReplyDelete
  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!

    Jimmy

    ReplyDelete
    Replies
    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.

      Delete
  20. This comment has been removed by the author.

    ReplyDelete

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