With a title like that, I should get some hits!
Note: Updated 8th February to cover the new Raspberry Pi 3 with built in WiFi and Bluetooth.
Since Jessie came along, networking have changed slightly, and there are numerous people having problems getting Jessie to connect to the internet, or just to WiFi. This might help. Oh, by the way, this is only a problem if the package named “raspberrypi-net-mods” has been installed – or so it seems.
If the above package has been installed, at least it backs up your old config files in /etc/network/*-old
. So you can revert, if necessary. However, we want to use the latest and greatest, so read on.
There are a couple of files that need to be updated:
File /etc/network/interfaces
This file should resemble the following:
auto lo iface lo inet loopback auto eth0 allow-hotplug eth0 iface eth0 inet manual auto wlan0 allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
In case you are wondering, the “auto” lines mean that the Pi will attempt to bring up these networks at boot time. If you don’t want this, remove the lines and use sudo ifup eth0
or sudo ifup wlan0
to bring them up manually.
It’s probably best not to touch the “lo” device though, just in case!
In the old days, the various iface
lines would be similar to one of the following:
iface wlan0 inet static iface wlan0 inet dhcp
But now, we leave them as manual.
There would, depending on the settings required, also be a few more lines to set the netmask
, static IP address
, gateway
and such like. These live elsewhere now, so keep reading!
So far, so good. For a WiFi network, we need to update the wpa_supplicant.conf
file.
File /etc/wpa_supplicant/wpa_supplicant.conf
Edit the file /etc/wpa_supplicant/wpa_supplicant.conf
and make it look like the following example, similar that is, not identical.
country=GB ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="YourNetworkSSID" psk="YourVerySecretPassword" # scan_ssid=1 # priority=1 }
Ok, points to be very careful of:
- The
country=GB
is required from the 26/02/2016 Raspbian release. It probably does no harm to have it in with earlier versions, your mileage may vary of course. It’s definitely required with the new Raspberry Pi 3 with built in WiFi and Bluetooth. If you are not sure of your country code, go to the Menu-> Preferences->Raspberry Pi Configuration utility and set it on the Localisation tab. You can pick from a list. - There are no spaces between names, equal signs and parameters etc. If you have any spaces, networking will not start. Ask me how I know!
- Lines with a ‘#’ as the first character are comments. They are ignored.
- You need one
network={...}
entry for each WiFi network that you might use. Home, work, college, whatever. Each will have a different name in thessid
entry. - The
scan_ssid
andpriority
entries are optional. Priority
determines the order in which your Pi will connect if more than one of the networks is available. It’s best to give each one a different priority in that case! If more than one networks have the same priority, then the signal strength and/or security policy may be used to choose the best one to connect to.Scan_ssid
determines how the network will be scanned for. From the docs, and if you follow this, you are better than I am!
SSID scan technique; 0 (default) or 1. Technique 0 scans for the SSID using a broadcast Probe Request frame while 1 uses a directed Probe Request frame. Access points that cloak themselves by not broadcasting their SSID require technique 1, but beware that this scheme can cause scanning to take longer to complete.
I have two WiFi networks that the Pi can connect to, so I have two different entries in my configuration file.
So, replace "YourNetworkSSID"
above, with the name of your WiFi network. If the network doesn’t broadcast the SSID, then you need to know exactly what it is.
Replace "YourVerySecretPassword"
with your own network’s password.
Save the changes. My own file looks like this:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="HomeWIFI" psk="YoullNeverGuessThisPassword" } network={ ssid="OfficeWIFI" psk="YoullNeverGuessThisPasswordEither" }
I have a home and office Wifi in the same place. I work from home and can use either.
In the past, you would set up your static IP addresses, gateways, netmasks etc in the /etc/network/interfaces
file. No longer! It is now held in the /etc/dhcpcd.conf
file. (Which I find very difficult to (a) remember and (b) type!
File /etc/dhcpcd.conf
Only do the following if you wish to assign a static IP address to your Pi when it’s on wired (eth0) and/or WiFi (wlan0) networks. I have mine set to have a static IP on both, so my file looks similar (ok, identical) to the example below.
Edit the file /etc/dhcpcd.conf
and search for the interface eth0
or interface wlan0
lines. They may not be present. If you don’t find the entries, scroll to the end of the file.
Add the following configuration lines:
# Static IP configuration for eth0. interface eth0 static ip_address=192.168.1.50/24 static routers=192.168.1.1 static domain_name_servers=8.8.4.4 8.8.8.8 # Static IP configuration for wlan0. interface wlan0 static ip_address=192.168.1.51/24 static routers=192.168.1.1 static domain_name_servers=8.8.4.4 8.8.8.8
Some more points to be very careful of:
- As mentioned above, you only need to do this if you wish to allocate a static IP address to your Pi when it’s on wired (eth0) and/or WiFi (wlan0) networks. If you don’t care what IP it has, simply ignore this section.
- Again, there are no spaces between names, equal signs and parameters etc. If you have any spaces, networking will not start. Ask me how I know!
- You need one
interface ...
entry for eachiface ...
that you have defined in/etc/network/interfaces
– don’t set one up for the “lo” device though! The names must match too. - Yes, you do need to repeat the information for routers etc in each entry. They don’t get shared, which is a shame. It would be nice to have a global section where I could define these once, then only define the changeable stuff in each
interface
entry. Still, c’est la vie, as they say in Wales.
Ok, the static ip_address line defines that I’m using an IP address of 192.168.1.50 when on eth0, or wired network, and 192.168.51 when on WiFi. This allows me to use both, at the same time, if I wish, without an IP clash.
The /24
means that the first 24 bits of the IP address are my netmask, or 255.255.255.0. This identifies my network as 192.168.1.xxx and my devices as 50 and 51, in this case.
My router is on address 192.168.1.1, so that’s what goes into the routers entry.
I use Google’s name servers for my DNS name resolution, they are usually always available, but you can add others if you wish. Your ISP might have some that you can use. Separate each one with a space.
By the way, don’t use a name here, always use an IP address. You can’t resolve the names you use for the DNS servers if you don;t have a DNS server to resolve them! (No, I didn’t do that one myself, thanks for asking!)
Save the file.
That’s should do it. A quick reboot is probably the best way to make sure everything is set up correctly, so insert a slight pause here while we reboot.
sudo reboot
When the system comes back up, run the following command to see what appears:
hostname -I ## That's a capital 'eye' not a 'one'.
You should see the two static IP addresses you configured, if you did so, and if you have both eth0 and wlan0 up and running, like I do. If you are only connected to WiFi, for example, you should only see the one IP address for that.
If you see an extra IP address, then you probably still have some dhcp or static entries in your /etc/network/interfaces
file, in the old style format, that need to be removed. Make sure that your file looks like mine above to get rid of the ghost IP address.
HTH
Cheers.