Linux WiFi Setup

This is a step-to-step guide for connecting to a WPA/WPA2 WiFi network via the Linux command line interface. The tools are:

  • wpa_supplicant - is the wireless tool for connecting to a WPA/WPA2 network.

  • iw - is the basic tool for WiFi network-related tasks, such as scanning for access points.

  • ip - is used for enabling/disabling devices, and finding out general network interface information.

  • ping

The steps for connecting to a WPA/WPA2 network are:

1. Device Name

ip link show

The result should be something like this:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: can0: <NOARP40000> mtu 16 qdisc noop qlen 10
3: can1: <NOARP40000> mtu 16 qdisc noop qlen 10
4: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:c6:7e:fd:0c brd ff:ff:ff:ff:ff:ff
5: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
    link/ether 00:0c:c6:7e:b4:0f brd ff:ff:ff:ff:ff:ff
6: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
    link/ether 00:07:80:a9:50:e5 brd ff:ff:ff:ff:ff:ff

The above output showed that the system has 1 physical WiFi card, designated as wlan0.

2. Bring up the Device

ip link show wlan0
6: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
    link/ether 00:07:80:a9:50:e5 brd ff:ff:ff:ff:ff:ff

Look for the word “UP” inside the brackets in the first line of the output. In the above example, wlan0 is not UP. Execute the following command to bring it up:

ip link set wlan0 up

If you run the show link command again, you can tell that wlan0 is now UP.

6: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq qlen 1000
    link/ether 00:07:80:a9:50:e5 brd ff:ff:ff:ff:ff:ff

3. Scan WiFi

iw wlan0 scan
wlan0     Scan completed :
          Cell 01 - Address: D3:4D:B3:3F:E0:03
                    ESSID:"Test AP"
                    Frequency:2.437 GHz (Channel 6)
                    Quality=26/40  Signal level=-21 dBm  Noise level=-47 dBm
                    Encryption key:on
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
                              48 Mb/s; 54 Mb/s
                    IE: Unknown: 000B5854313033392033303630
                    IE: Unknown: 010482848B96
                    IE: Unknown: 030106
                    IE: Unknown: 2A0100
                    IE: Unknown: 32080C1218243048606C
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK
                    IE: Unknown: 2D1A2C0103FF00000000000000000000000000000000000000000000
                    IE: Unknown: 3D1606000000000000000000000000000000000000000000
                    IE: Unknown: DD180050F2020101810003A4000027A4000042435E0062322F00
          Cell 02 - Address: 80:1F:02:8E:B9:4A

The 2 important pieces of information from the above are the SSID and the security protocol (WPA/WPA2 vs WEP). The SSID from the above example is ‘Test-AP’. The security protocol is WPA2-PSK (Pre-Shared Key). The security protocol is important because it determines what tool you use to connect to the network.

4. Connect

This is a 3 step process.

  1. As the karo-image-minimal is mounted read-only by default, the user has to (re-)mount the filesystem writeable:

mount -o remount,rw /
  1. Generate a configuration file for wpa_supplicant that contains the pre-shared key (a.k.a. “PSK” or “passphrase”) for the WiFi network.

wpa_passphrase "Test AP" >> /etc/wpa_supplicant.conf

Type in the passphrase -> press enter

wpa_passphrase takes the SSID as the single argument. The user types in the passphrase for the WiFi network ‘Test AP’ after running the command. Using above command the output of wpa_passphrase is piped and appended (‘>>’) to the wpa_supplicant configuration file located at ‘/etc/wpa_supplicant.conf’, the user may freely choose the name and location of the file.

cat /etc/wpa_supplicant.conf
    ssid="Test AP"
  1. Run wpa_supplicant with the new configuration file.

wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf

5. Optain IP

dhcpcd wlan0

Use the ip command to verify the IP address assigned by DHCP. The IP address is from below.

ip addr show wlan0
6: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq qlen 1000
    link/ether 00:07:80:a9:50:e5 brd ff:ff:ff:ff:ff:ff
    inet brd scope global wlan0
       valid_lft forever preferred_lft forever

6. Test Connection

PING ( 56 data bytes
64 bytes from seq=0 ttl=47 time=97.929 ms
64 bytes from seq=1 ttl=47 time=88.048 ms
64 bytes from seq=2 ttl=47 time=97.654 ms
64 bytes from seq=3 ttl=47 time=127.304 ms
64 bytes from seq=4 ttl=47 time=96.964 ms

--- ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 88.048/101.579/127.304 ms