How to connect to Wi-Fi using command-line with Ubuntu 18.04?

Wireless configuration using WPA2

Debian/Ubuntu linux uses wpa_supplication to support WPA/WPA2 connections. In this tutorial we are going to explain how to use the terminal to connect to w Wi-Fi network.

Step 1: Installation

To install wpasupplicant simply execute the following command with sudo:

sudo apt install wpasupplicant wireless-tools

Step #2: Check hardware is working

Use lspci to verify if the netowrk card was detected properly:

lspci | grep -i wlan  
lspci -nn | grep Network

To list all network interfaces use "-a" parameter

ifconfig -a

If you can't see you card, use the dmesg command and search for errors in the logs.

Step 3: Configure WPA2

Open the file /etc/network/interfaces file Use the following settings for the one you want to configure (wlan0 in this example):

    auto wlan0
    iface wlan0 inet dhcp
        wpa-ssid ACCESS-POINT-SSID-HERE
        wpa-psk ACCESS-POINT-SSID-HERE  

TO start the Wi-Fi connection execute:

ifup wlan0  

You can chek connectivity using the ping command against the router or any site on the internet.

Get Info About Your Network

You can use nmap to check other devices in the network:

nmap 192.168.0.0/24

Remember to replace with the correct ip address, some routers use 192.168.1.0/24 or any other valid private ip address range.

Scan for available Wi-Fi APs

Type the following command for scanning near access point in the command line:

iwlist wlan0 scan

Appendix

Troubleshooting wpa_supplicant

The command wpa_supplicant uses the log file: /var/log/wpa_supplicant..log*. If you have issue check always this log file.

You can try to execute wpa_supplicant and in other terminal execute tail to see realtime output:

tail -f /var/log/wpa_supplicant.wlan0.log  

NetworkManager problems

If you use NetworkManager and wpa_supplicant as we explain in this article, you will have problems since NetworkManager will overwrite /etc/network/interfaces file.