Automatically Connect Raspberry Pi to Wi-Fi
12 Feb 2015The Internet. It’s big and exciting but you don’t want to load up a GUI in order to connect to it. There are a couple of ways you can configure your Pi to connect over Wi-Fi when it boots up.
There are just a few things you’ll need:
- Wi-Fi USB adapter for your Raspberry Pi
- Wireless Router
- Working internet connection
It’s probably also a good idea to update your OS if you haven’t done that.
sudo apt-get update
sudo apt-get upgradeNow go ahead and shutdown your Pi
sudo shutdown -h nowPlug in your Wi-Fi adapter and boot up the Pi.
One way to set up your connection is to configure you network interface manually. Choose your text editor and open your settings. I use vi here, but nano may be more to your liking. If so, just replace ‘vi’ with nano where you see it below.
sudo vi /etc/network/interfacesEnter in your base configuration for a DHCP connection:
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcpNow you’ll need to enter your network connection information. Open up your WPA configuration.
sudo vi /etc/wpa_supplicant/wpa_supplicant.confHere’s an example using a WPA protocol:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="ENTER NETWORK NAME"
proto=WPA # (or RSN for WPA2)
key_mgmt=WPA-PSK
pairwise=TKIP # (or CCMP for WPA2)
psk="ENTER WPA KEY"
auth_alg=OPEN
}If you’re not using DHCP or if you’re wanting to set up multiple connections, you’ll have to do a bit more configuration. You’ll be changing
iface wlan0 inet dhcp => iface wlan0 inet manualand
wpa-conf => wpa-roamThis also shows an example configuration for a static IP.
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
# For ssid 'work':
iface work inet static
address XXX.XXX.X.X
network XXX.XXX.X.X
gateway XXX.XXX.X.XYou would need to enter your work network in your WPA configuration:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="ENTER NETWORK NAME"
proto=WPA
key_mgmt=WPA-PSK
pairwise=TKIP
psk="ENTER WPA KEY"
auth_alg=OPEN
}
network={
ssid="work"
proto=WPA
key_mgmt=WPA-PSK
pairwise=TKIP
psk="ENTER WPA KEY"
auth_alg=OPEN
}Now you can reboot
sudo shutdown -r nowIf your settings are correct you’ll have an internet connection. You can test it out by running
ifconfigUnder you wlan0 output you should see an assigned IP like ‘inet addr:XXX.XXX.XXX.XXX’
If you want to test your settings without rebooting, you can release your connection with
sudo ifdown wlan0And reconnect with new settings using
sudo ifup wlan0WICD-CURSES
There’s an arguably easier route to pursue with wicd-curses. It will give you an interface to select your Wi-Fi connection. It will also allow you to select a network to connect to automatically and enter custom DNS/address/network/gateway settings. Just run
sudo apt-get install wicd-curses
sudo wicd-cursesYou can navigate with the keyboard and it will provide you with a menu for various options.
If you really don’t want to use the command line, you can always run one or both of the following:
sudo apt-get install wicd
sudo apt-get wpaguiThen start your GUI
startxUnder your internet menu there should be a wpa_config or Wicd Network Manager program to manage these settings.