Configure WiFi from command line (Ubuntu 18.04)


sudo su
killall wpa_supplicant
wpa_passphrase "your_essid" "your_password" >> /etc/wpa_supplicant/wpa_supplicant.conf
wpa_supplicant -B -i wlp2s0  -c /etc/wpa_supplicant/wpa_supplicant.conf
dhclient eth1
  • wpa_supplicant and dhclient require sudo

# figure out what's your WiFi interface called
iwlist scan

# scan for wifi networks
iwlist wlp2s0 scan
#iwlist wlp2s0 s

# generate a 'network' entry for wpa_supplicant.conf file
# replace MyWifiNetwork with the name of the wifi network you want to connect to
# enter the wifi password when prompted
sudo su
wpa_passphrase MyWifiNetwork >> /etc/wpa_supplicant/wpa_supplicant.conf
# you'll get a blinky cursor, enter the password now

# connect to the network now
wpa_supplicant -B -i wlp2s0  -c /etc/wpa_supplicant/wpa_supplicant.conf

# obtain an IP address
dhclient wlp2s0


  • i don’t have ifconfig avaialble because i don’t have net-tools installed. And i can’t installed net-tools because i don’t have WiFi access!
  • i can’t SSH in from another machine because i don’t have WiFi

Find out what’s your interface called

On the Raspberry Pi it might be wlan0. On a laptop it could be wlp2s0. You could have multiple WiFi interfaces if you use built-in WiFi card and have attached a dongle as well.

  • iwconfig will give you the inetrfaces and the networks they might already be connected to
# TODO: add interface output here..
# the output for iwconfig for when it is connected and when it is not connected to a network

Scan for available networks

iwlist scan wlp2s0

the ESSID is the name of a wifi network.

A network will look like this:

# TODO: add scanned wifi network here..

Add a network entry

sudo su
wpa_passphrase MyWifiNetwork >> /etc/wpa_supplicant/wpa_supplicant.conf
  • You need sudo to be able to add/edit files in /etc/wpa_supplicant
  • Redirection >> will also create the file wpa_supplicant.conf
  • You will get an empty prompt with blinky cursor after running the command. You have to enter the password here (i thought the command failed because i got no output, but it didn’t)
  • Once you have the network entry added, edit the file and remove the plain text password from it, it’s not needed.
  • Add country code for good measure. The Raspberry Pi 3 Model B+ requires it to set the correct frequency bands..

Your resulting entry will look like this:

# This is the entry for MyWifiNetwork with WPA2
  priority=2 # higher will get more priority
  country=PK # needed on Raspberry Pi models with builtin WiFi

When there are multiple network entries, the network with the highest priority will get connected. If it fails then the one with next highest priority will be connected and so on.. In my case, the hotspot is not always available, but when it is i want to always connect to it rather than the home network as the hotspot provides faster speeds. When the hotspot is off it’ll automatically connect to the home network.

Connect to the network

This assumes you already have a wpa_supplicant.conf file with your network entries

# this one will give you command line output for what's happening
# you can disable the output by passing -B
wpa_supplicant -iwlp2s0 -c/etc/wpa_supplicant/wpa_supplicant.conf -Dwext
  • the -Dwext part doesn’t seem all that necessary. Will connect without it too. FYI, the -D if for driver, you find it out by with the wpa_supplicant command

i kept getting a CTRL-EVENT-DISCONNECTED error

# with -Dwext
wlp2s0: CTRL-EVENT-DISCONNECTED bssid=xx:xx:xx:xx:xx:xx reason=0

# without -Dwext
wlp2s0: CTRL-EVENT-DISCONNECTED bssid=xx:xx:xx:xx:xx:xx reason=3

The error could be because an instance of wpa_supplicant is already running and interfering with the connection.

Got rid of it by running killall wpa_supplicant and running the command again. Connected successfully this time, but wpa_supplicant didn’t leave the prompt. So ran the command again, this time with -B to run the daemon background and get the prompt back.

You can now confirm that you are connected with the iwconfig command.

Now you also need to obtain an IP address (reason=3 usually comes up when failing to get a dhcp lease)

dhclient wlp2s0

From now on you should be connected to WiFI (and internet).

If you want to know what your IP is (if you want to SSH, and you still don’t have ifconfig) you can use the following command

ip add
ip addr show
ip addr show wlp2s0

Both ip add and ip addr are valid commands

Alternate Command 1

iwconfig wlp2s0 essid MYNETWORK key MYPASSWORD

Alternate Command 2

wpa_cli -i wlp2s0 reconfigure
  • -i is for interface
  • reconfigure will force wpa_supplicant to re-read its configuration file

Run this command after you have configured your wpa_supplicant.conf file. I kept getting a FAIL. I ran killall wpa_supplicant and tried again, but then it was unable to connect to wpa_supplicant and i got the following error:

Failed to connect to non-global ctrl_ifname: wlp2s0  error: No such file or directory


  • If your configuration is wrong in wpa_supplicant you’ll get an error with line numbers pointing out where the issue is
Failed to read or parse configuration `/etc/spa_supplicant/spa_supplicant.conf`

Set power management to off permanently

# create or edit a configuration file that will override the default powermanagement behavior
gksudo gedit /etc/pm/power.d/wireless

Add the follownig to the file:

/sbin/iwconfig wlan0 power off