How to up a Static IP on Your Wireless Network Connection


13 posts
by bannbino » Thu Jan 24, 2013 7:32 pm
Hi All,

this is my first post in these forums, but it's about a subject I spent a couple of evenings trying to get to work:

How to setup my Raspberry Pi's wireless network connection to use a static IP address.

I searched around quite a bit, but couldn't find anyone that could tell me how to do it. So, with a bit of experientation and by using different bits of tutorials I managed to get it working.

I'm using the latest Rasbian "Wheezy" image, 2012-12-16-wheezy-raspbian.

I'm going to make several assumptions that you have a supported wifi dongle (you can check the list here: http://elinux.org/RPi_VerifiedPeripherals#USB_Wi-Fi_Adapters%20 - I use a TP-Link TL-WN723N which is a cute little dongle (white with an under-the-skin green led activity light) and nice and cheap at £6.96 from the Dabs ebay store), it's plugged into your RPi, and you have basic linux skills (like I do) so know your way around the terminal. I'm also assuming that you have a basic understanding of IP addresses, and how to configure your router.

After setting up your SD card with the image and booting your RPi for the first time and being presented with the raspi-config menu. You can get this menu back at any time from the command line using
Code: Select all
sudo raspi-config
Make sure the option for booting into the desktop is enabled.

After the obligatory reboot, open the WIFI Config GUI (there should be a shortcut on the desktop, or it's listed as in the menu as Internet>wpa_gui or Other>wpa_supplicant user interface).

Click on Scan and look for your wireless network in the results. Double click the entry for your wireless network and enter your PSK (pre shared key) in the field provided (all the other elevant fields should have auto-populated). At the bottom of the window is a field for IDString under Optional Settings. Enter a string here that you can remember and doesn't contain any spaces (I use 'home_static').

Now we need to tell your RPi what static IP address to use for this network.

For that, we need to edit the /etc/network/interfaces file.

I use 'vi' for my command line editing. It can be a little fiddley but a good list of the commands can be found here; http://www.cs.colostate.edu/helpdocs/vi.html.

To open the file for editing, open an LXTerminal session (again, there should be a shortcut on the desktop, or else it's under Accessories>LXTerminal or Other>LXTerminal in the menu), and type:
Code: Select all
sudo vi /etc/network/interfaces
At this stage there should be several lines in the file already and it should look something like:
Code: Select all
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
Now we need to add a series of lines to the bottom of the file as follows:
Code: Select all
iface home_static inet static
   address 192.168.0.53
   netmask 255.255.255.0
   gateway 192.168.0.1
The address, netmask and gateway are based on my own home network and router setup, which you may need to change based on your own setup, router manufacturer etc. Try to make sure that you use a static IP address that is outside the DHCP scope of your router (mine uses address above 192.168.0.100 for DHCP, so I use addresses from 192.168.0.50 for static. I find it a good idea to maintain a spreadsheet of all the static IP address I've asigned, and what I've assigned them too - xbox, webserver, RPi etc). In the top line, home_static has to match whatever IDString you used in the gui when you added your wireless network. If the IDString contains a space, it may not work as the config might think the IDString has ended and it's moving onto the next configuration item.

Once you've saved the file, you need to restart your network services. I've found with the RPi that simply restarting the network services on their own is not enough, so I usually carry out a full reboot. It doesn't take long.

This can be done for multiple networks, and you can have more than one entry for each network by giving them a different IDString (not sure why you'd want to though) and if you don't add an entry in /etc/network/interfaces then it will use DHCP as default.

For info, the wireless network information is stored in /etc/wpa_supplicant/wpa_supplicant.conf and you can manually edit it using
Code: Select all
sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
from the command line.

Hope this helps and saves a few people a couple of evenings of head scratching (although that's half the fun, right?)...

Steve
Posts: 2
Joined: Thu Jan 24, 2013 6:12 pm
by repton » Thu Jan 24, 2013 8:20 pm
I did it a different way. I simply added the following lines to /etc/network/interfaces:

Code: Select all
auto wlan0
iface wlan0 inet static
        address 192.168.0.21
        netmask 255.255.255.0
        gateway 192.168.0.1
        wpa-essid not-telling-you
        wpa-psk not-telling-you-this-either


That was it. No raspi-config, no wpa_supplicant.conf, no anything else, I literally plugged the USB wlan in, added those lines, rebooted, and it worked.

HTH,
Paul
UK Supplier of 1-Wire components, kits and modules:
http://www.sheepwalkelectronics.co.uk/
Posts: 84
Joined: Sat Mar 17, 2012 6:06 pm
Location: North Yorkshire, UK.
by bannbino » Fri Jan 25, 2013 9:37 am
repton wrote:I did it a different way. I simply added the following lines to /etc/network/interfaces:

Code: Select all
auto wlan0
iface wlan0 inet static
        address 192.168.0.21
        netmask 255.255.255.0
        gateway 192.168.0.1
        wpa-essid not-telling-you
        wpa-psk not-telling-you-this-either


That was it. No raspi-config, no wpa_supplicant.conf, no anything else, I literally plugged the USB wlan in, added those lines, rebooted, and it worked.

HTH,
Paul



Hi Paul,

what version of debian are you using? The reason I ask is that i tried to do it that way but because WPA_Supplicant is preinstalled, if I changed the wlan0 to static it wouldn't do anything because it still wanted to get the settings from wpa_supplicant,conf. Unless you also removed the line
Code: Select all
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

The other thing with doing it that way is that you'd have to reconfigure the /etc/network/interfaces file every time you used a different wireless network, should you want to!

Cheers

Steve
Posts: 2
Joined: Thu Jan 24, 2013 6:12 pm
by repton » Fri Jan 25, 2013 1:14 pm
bannbino wrote:what version of debian are you using?


It's whatever the latest Raspbian was about 2 or 3 weeks ago. The file I used to make the SD card is "2012-12-16-wheezy-raspbian.img".

bannbino wrote:The other thing with doing it that way is that you'd have to reconfigure the /etc/network/interfaces file every time you used a different wireless network, should you want to!


True enough but that isn't an issue for me as the RasPi in question is destined to be screwed into a box in the rafters of a barn where it will monitor the weather using 1-Wire devices so I won't be wanting to change networks very often...

If I did want to use it on multiple networks you could simply put multiple pairs of lines in that file with the essid and keys and simply comment out all the ones you're not using.

Paul
UK Supplier of 1-Wire components, kits and modules:
http://www.sheepwalkelectronics.co.uk/
Posts: 84
Joined: Sat Mar 17, 2012 6:06 pm
Location: North Yorkshire, UK.
by repton » Fri Jan 25, 2013 1:17 pm
bannbino wrote:The reason I ask is that i tried to do it that way but because WPA_Supplicant is preinstalled, if I changed the wlan0 to static it wouldn't do anything because it still wanted to get the settings from wpa_supplicant,conf. Unless you also removed the line
Code: Select all
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf


I've just checked and I have indeed also commented out that line, so my initial post was slightly misleading. Sorry if I have confused anyone...

Paul
UK Supplier of 1-Wire components, kits and modules:
http://www.sheepwalkelectronics.co.uk/
Posts: 84
Joined: Sat Mar 17, 2012 6:06 pm
Location: North Yorkshire, UK.
by lesiu » Thu Feb 21, 2013 10:35 pm
I had similar problem but I found out that hidden network need additional parameter:

wpa-scan-ssid 1

Please add this to your /etc/network/interfaces if you have problem which connect to your hidden network.
Posts: 3
Joined: Thu Feb 21, 2013 10:24 pm
by SN » Thu Feb 21, 2013 10:53 pm
you do know you can get your router to PIN IP Addresses to MAC addresses so you get the same DHCP supplied IP address every time without all this messing around?

Here's a cut of the config table on my NetGear Router showing, amongst others a Toaster NAS Drive, an HP Touchpad 8-) , an Apple TV unit, an HP Printer and a Raspberry Pi - the XBMC unit behind my telly as it happens :D

Image
Steve N – binatone mk4->intellivision->zx81->spectrum->cbm64->cpc6128->520stfm->pc->raspi ?
User avatar
Posts: 1008
Joined: Mon Feb 13, 2012 8:06 pm
Location: Romiley, UK
by jonhale » Mon Mar 25, 2013 10:01 pm
Steve N.
Unfortunately, not all wifi routers provide this functionality (much to my disappointment).
Posts: 1
Joined: Mon Mar 25, 2013 9:55 pm
by SkinDaNFC » Fri Apr 05, 2013 2:08 pm
SN wrote:you do know you can get your router to PIN IP Addresses to MAC addresses so you get the same DHCP supplied IP address every time without all this messing around?

Here's a cut of the config table on my NetGear Router showing, amongst others a Toaster NAS Drive, an HP Touchpad 8-) , an Apple TV unit, an HP Printer and a Raspberry Pi - the XBMC unit behind my telly as it happens :D

Image


I haven't done it myself since I'm still waiting on my wifi dongle but I have been looking into all of this a lot. Maybe this is what your talking about & looking for (in regards to DHCP):

http://n00blab.com/how-to-set-up-raspbe ... t-monitor/
Posts: 2
Joined: Fri Apr 05, 2013 1:56 am
Location: United States
by pjc123 » Fri Apr 05, 2013 7:19 pm
SkinDaNFC wrote:
I haven't done it myself since I'm still waiting on my wifi dongle but I have been looking into all of this a lot. Maybe this is what your talking about & looking for (in regards to DHCP):

http://n00blab.com/how-to-set-up-raspbe ... t-monitor/


No, that is not what that table is referring to; it allows you to assign a static address to a device using DHCP. If you do not use the table, then the address can be different when your device requests a DHCP lease, and you would have to use the method shown in that url you are pointing to, or one of several other methods to find the address assigned to your device.

This is how static ips via DHCP and that table works. Every network interface has a unique MAC address assigned to it (You can think of it as a serial number that is readable over the network), and in the router you can assign a static ip address of your choosing to that mac address by filling in a table similar to the one that was shown. So you can leave the pi set up to use DHCP and it will get that same static assigned address from the router every time you boot it up or request a lease. The advantage of this is that if you then want to use a router somewhere else, DHCP is still set up on the device and it works as usual. I use this method to issue static addresses to the LAN and wifi connections on 2 different pis; so I am issuing 4 different static addresses with my router and when my pis boot up I know exactly what each interface is set to without having to hunt around to find the address.

The other method is to not use DHCP and assign a static address on the each interface on the pi itself in the /etc/network/interfaces file, but then you have lost the capability to use DHCP in other places or with other access points.
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm
by kevdotbadger » Wed Apr 24, 2013 6:56 pm
Thank you! Just got my Pi and this was a great and clear article.
Posts: 2
Joined: Wed Apr 24, 2013 6:55 pm
by Vingtoft » Mon Sep 30, 2013 4:53 pm
Thanks a lot, it worked perfectly for me. Cheers :D :D
Posts: 1
Joined: Mon Sep 30, 2013 4:52 pm
by shanepisko » Tue Nov 12, 2013 4:09 pm
great help thanks, couldnt find how to do this for the wifi anywhere
Posts: 1
Joined: Tue Nov 12, 2013 4:06 pm