How to Connect WiFi from Terminal on Ubuntu 16.04

connect wifi command line

WiFi is nowadays the most popular way of getting an internet connection, be it at the office, home, restaurants e.t.c. This tutorial is intended to help those who would like to connect WiFi from the terminal on Ubuntu 16.04. It doesn't matter whether you're using the Desktop edition or the server edition of Ubuntu.

The only requirements are:

  • Working wireless card
  • Internet connection to install some packages
  • Interactive shell session

There are two common ways you can connect Wireless network from the terminal. These are:

  1. Using iwconfig command
  2. Using wpasupplicant method

Method 1: Using iwconfig command

You can configure your Ubuntu to connect to WiFi by following steps given below. If you don't know the name of your wifi device, use:

$ ip link sho | grep w
2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000

Open the terminal and type ifconfig interface and press Enter.

$ sudo ifconfig wlan0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet netmask broadcast
 inet6 fe80::557e:6563:6f86:f8b2 prefixlen 64 scopeid 0x20<link>
 ether e4:a7:a0:ef:27:d9 txqueuelen 1000 (Ethernet)
 RX packets 4025786 bytes 5253861542 (4.8 GiB)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 2997242 bytes 563513781 (537.4 MiB)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Byt default, this command just turns your wireless card on. If your wireless card has a different name, replace wlan0 with a correct name.  Now that the card is on, you can connect to your WiFi network by specifying its ESSID name:

$ iwconfig wlan0 essid <name> key <password>

Replace <name> with the actual name of your access point, and password with the actual security key for the wireless network. If your wireless network does not require a security key, do not enter a key password. Just use:

$ iwconfig wlan0 essid <name>

For the ASCII password, use:

$ iwconfig wlan0 essid <name> key s:<password>

Now confirm that you are connected. You can as well manually request an IPv4 address using.

$ dhclient wlan0

Then try to check your ip and try to ping outside.

$ ip addr
$ ping
 connect WiFi from the terminal on Ubuntu 16.04

Method 2: Using wpasupplicant

wpa_supplicant is a cross-platform supplicant with support for WEP, WPA, and WPA2 (IEEE 802.11i / RSN (Robust Secure Network)). It is suitable for desktops, laptops, and embedded systems. For this method, you first need to install wpasupplicant package which includes the main program wpa_supplicant, the passphrase tool wpa_passphrase, and the text front-end wpa_cli

$ sudo apt-get install wpasupplicant

This package will provide a wpa_supplicant command which we'll use to manage our Wireless network. The file to configure is /etc/wpa_supplicant.conf.

# cat /etc/wpa_supplicant.conf

You can as well generate a basic configuration from cli:

# wpa_passphrase MYSSID passphrase > /etc/wpa_supplicant.conf

Now rescan for available devices:

# iwlist scan

Make sure you can see the wireless network you intend to connect to. Now start wpa_supplicant with:

# wpa_supplicant -B -i interface -c /etc/wpa_supplicant.conf
Successfully initialized wpa_supplicant

Commonly used arguments are:

-B - Fork into the background.
-c filename - Path to a configuration file.
-i interface - Interface to listen on.
-D driver - Optionally specify the driver to be used. For a list of supported drivers see the output of wpa_supplicant -h.
nl80211 is the current standard, but not all wireless chip's modules support it.

Using wpa_cli

At this point run:

# wpa_cli

This will present an interactive prompt (>), which has tab completion and descriptions of completed commands. Some important commands to note when working on wpa_cli include:

  • Scan for available access points
> scan
  • List available Access points
> scan_results
bssid / frequency / signal level / flags / ssid
11:22:33:44:55:66 2462 -33 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] MYSSID1
00:11:22:33:44:55 2412 -73 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] MYSSID2

To associate with MYSSID, add the network, set the credentials and enable it:

> add_network
> set_network 0 ssid "MYSSID1"
> set_network 0 psk "passphrase"
> enable_network 0
<2>CTRL-EVENT-CONNECTED - Connection to 11:22:33:44:55:66 completed (reauth) [id=0 id_str=]

For access points with no authentication, use:

> set_network 0 key_mgmt NONE

Finally, save this network in the configuration file:

> save_config

To get a DHCP lease, first release whatever leases you're still holding onto (as root):

# dhclient -r

And request a new address

# dhclient interface

You should now have a working internet connection through an access point which you connected from cli. This method is suitable for minimal installation of Ubuntu Distributions or Servers with Wireless card. These methods are easy to script, with more effort you can write a cron job which connects to it at reboot. Thank you for reading our guide on how to connect WiFi from the terminal on Ubuntu 16.04. If you encounter any issues, let us know through the comment section.

4 Comments... add one

  1. I am unable to connect to WiFi router using dhclient command. It gets struck on dhcpdiscover command. Please tell some solution.

  2. I'd like to know how to do this on Ubuntu Server Edition *without installing any additional packages*! wpa_supplicant is not in the default 16.04 installation.


Leave a Comment