From charlesreid1

This page covers methods of connecting to wifi from Linux.

The methods break down as follows:

  • Use /etc/network/interfaces
  • Use wpa supplicant
  • How to connect to encrypted vs. unencrypted wifi

Git Repos

As a note, there are some useful resources related to automatically joining wifi networks, mainly scripts developed for use with Raspberry Pis. These can be found here: https://git.charlesreid1.com/rpi/pi-join-wifi

Joining Wireless Networks

In Linux, joining a wifi network automatically, or from the command line, or otherwise without a visual desktop, is not a trivial task - even in 2017.

This page contains some notes on a few techniques for connecting to wifi networks, developed from experience with Raspberry Pis and other Linux computers with wireless devices built in (mostly laptops/virtual machines).

Using /etc/network/interfaces

Joining network with WPA encryption

NOTE: This method is working on Raspberry Pi platform as of April 2017.

Main Page: Linux/Wireless/2

To set the wireless network you want a Linux box to join, you can add the network name and passphrase to /etc/network/interfaces. Better yet, you can create one file for each network you want to have ready to go, and swap them in and out by sourcing them or not from the /etc/network/interfaces file.

First, put the wifi configuration information into a file. This will be called mynetwork.cfg, and will be stored in /etc/network/interfaces.d/mynetwork.cfg.

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
    wpa-ssid NetName
    wpa-psk NetPassword

The next step is to reference this configuration file from the /etc/network/interfaces file. Here is what that file looks like:

source /etc/network/interfaces.d/mynetwork.cfg

The /etc/network/interfaces.d/ folder would contain credentials for several networks, and could be swapped out by editing /etc/network/interfaces.

Now bring the interface down and back up:

$ ifdown wlan0
$ ifup wlan0

It should print information about the network it joins and its new IP. If you aren't assigned an IP address, try this:

$ dhclient wlan0

Joining an open network

Repeat the above steps, but this time your wifi network's config file will look a little different for the open network:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid LocalCoffeeShop

WPA Supplicant Method

NOTE: This is for WPA- and WPA2-encrypted networks only.

Main Page: Linux/Wireless/1

This method uses wpa_supplicant, and has been tested and works on a Rasbperry Pi.

The One Time Wham Bam Thank You Maam

Assuming your wireless device is wlan0,

$ wpa_supplicant -D nl80211,wext -i wlan0 -c <(wpa_passphrase "MyRouter" "MyPassword")


Set wireless network configuration

First add network configuration to /etc/wpa_supplicant/wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="Your SSID Here"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP TKIP
    group=CCMP TKIP
    psk="YourPresharedKeyHere"
}

Then edit /etc/network/interfaces and modify the wireless device to set the interface, and the wpa supplicant configuration file. Here is an example for a network with dynamic DHCP. This is the /etc/network/interfaces file:

# ------- DHCP ------------
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Reset network device

Bring network device down and back up:

ifdown wlan0
ifup wlan0

You should see the wireless network you specified in your wpa supplicant file when you run iwconfig:

iwconfig

You should also see an IP address when you run ifconfig:

ifconfig

To start wpa_supplicant manually:

# sudo /sbin/wpa_supplicant \
  -i wlan0 \
  -P /var/run/wpa_supplicant.wlan0.pid \
  -D nl80211,wext -c /etc/wpa_supplicant/wpa_supplicant.conf

Joining Wifi Networks On Boot

If you want to join wifi networks on boot, you can add a config file for the network you want to join, and put it in /etc/network/interfaces.d/

Then add the line to /etc/network/interfaces to source the above wifi network config file. Your system should know how to get an IP address from the router next time it needs one.

If you can't wait until reboot, run:

$ dhclient wlan0

to flush and force dhcp to get a fresh IP address/connection.

Flags