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 upgrade
Now go ahead and shutdown your Pi
sudo shutdown -h now
Plug 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/interfaces
Enter 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 dhcp
Now you’ll need to enter your network connection information. Open up your WPA configuration.
sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
Here’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 manual
and
wpa-conf => wpa-roam
This 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.X
You 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 now
If your settings are correct you’ll have an internet connection. You can test it out by running
ifconfig
Under 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 wlan0
And reconnect with new settings using
sudo ifup wlan0
WICD-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-curses
You 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 wpagui
Then start your GUI
startx
Under your internet menu there should be a wpa_config or Wicd Network Manager program to manage these settings.