Connect Raspberry Pi to WiFi and give static IP from command line

First off, let me tell you I am no NOOB to linux, but finding a configuration that worked for me here was a bit time consuming. It looks as though there are numerous ways to go about doing this, and I think I tried about a dozen before I found one that worked with my RPi in my configuration. So I can’t guarantee this will work for you, all I can say is it works for me and it will hopefully help you out too.

 

All I really did was edit /etc/network/interfaces

 

You can let the first few lines I show below be, they have to do with the loopback interface and the wired ethernet port on the RPi.

auto lo

iface lo inet loopback
iface eth0 inet dhcp

After those lines we get into the config for the wireless device. In this case, and probably most, this is named “wlan0”. I changed the “iface” line to say static instead of dhcp, and I commented out the wpa_supplicant line. I was never able to get anything to work using the wpa_supplicant.conf file. Then I added in the lines to tell it what wireless network to connect to and what IP address to use, as well as the rest of the needed network config.

 

auto wlan0

allow-hotplug wlan0

iface wlan0 inet static

        wpa-ssid [your-wifi-name]

        wpa-psk [your-wifi-password]

        address x.x.x.x

        netmask 255.255.255.0

        gateway x.x.x.x

#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

Make sure to change the above to use your actual WiFi name and password. Also change the address to what you want your static IP address to be, and change the gateway to the IP address of your router.

Add the “#” to the wpa-roam line to comment it out, or delete that line.

You can then save and close the file. At this point you can reboot or type the following commands to restart the wlan0 interface:

sudo ifdown wlan0

sudo ifup wlan0

You should now have your wifi connection and it should be using the IP address you gave it.

For you security minded people, yes I know I have my wifi password in a plain text file. I’m not that concerned about it because this is just a RPi at my home I have for testing and learning purposes.  If you would like to keep your password more secure look into creating a hash file using wpa_passphrase.

Tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *