Wireless CLI Configuration

Description

This page covers how to configure wireless via the command line (cli) using wpa_supplicant.

Prerequisites

  • A working ubuntu system
  • wpasupplicant

Installation

First, install the wpasupplicant package if not already installed. This package contains the tools necessary for communicating via wireless.
sudo apt-get install wpasupplicant

Configuration

After installing wpasupplicant, we need to configure the wireless network interface as well as build a configuration file for wpasupplicant to use.

Lets first start by creating the initial config file for wpasupplicant by specifying the SSID and Passphrase.
wpa_passphrase <SSID> <KEY>
This will yield something similar to the following.
network={
    ssid="SSID"
    #psk="KEY"
    psk=462bde495ad6157470de2d6964f0751aacb98322708b...
}
Copy this and paste into a new wpa_supplicant.conf file.
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
If you have a hidden SSID, you need to add the scan_ssid=1 to the network stanza. See the modified contents below.
network={
    scan_ssid=1
    ssid="SSID"
    #psk="KEY"
    psk=462bde495ad6157470de2d6964f0751aacb98322708b...
}
Now that we have the basic config in place, we need to tell out network scripts to activate and use this config.
First, we need to find out the device name assigned to our wireless interface.
iw dev
Your output may differ, but as you can see below, my wireless interface is named wlp4s0.
phy#0
    Unnamed/non-netdev interface
        wdev 0x2
        addr a4:34:d9:7c:0e:b2
        type P2P-device
    Interface wlp4s0
        ifindex 4
        wdev 0x1
        addr a4:34:d9:7c:0e:b1
        type managed
        channel 149 (5745 MHz), width: 80 MHz, center1: 5775 MHz

Now that we have the interface, we need to create a new entry for our network interface.
sudo nano /etc/network/interfaces.d/wlp4s0
Add the following lines, adjust as necessary the network/ip details and interface. The below is for a static IP configuration. You can also specify dhcp instead.
auto wlp4s0
iface wlp4s0 inet static
address 192.168.1.254
netmask 255.255.255.0
gateway 192.168.1.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
dns-nameservers 192.168.1.1
If all is configured correctly, rebooting the system should result in a wireless connection and network connectivity.

Testing

To verify our link has come up, we can run the following command.
iw wlp4s0 link
If this shows you are not connected, then try bringing down the interface and then up again.
sudo ifdown wlp4s0
sudo ifup -v wlp4s0
Watch the output for any messages indicating why it may be failing (like shared key incorrect, etc.).