Connect to Wi-Fi From Terminal on Ubuntu 18.04/19.04 with WPA Supplicant

In this tutorial, we are going to learn how to connect to Wi-Fi network from command line on Ubuntu 18.04/19.04 server and desktop using wpa_supplicant. In a modern home wireless network, communications are protected with WPA-PSK (pre-shared key) as opposed to WPA-Enterprise, which is designed for enterprise networks. WPA-PSK is also known as WPA-Personal. wpa_supplicant is an implementation of the WPA supplicant component. A supplicant in wireless LAN is a client software installed on end-user’s computer that needs to be authenticated in order to join a network.

Step 1: Enable Wireless Card on Ubuntu 18.04/19.04

First, make sure your wireless card is enabled. You can use rfkill.

sudo apt install rfkill

To check the status of wireless card, run

rfkill list

Sample output:

ubuntu-18.04-19.04-connect-to-wireless-network-command-line

As you can see, my wireless card is blocked by software, meaning that wireless is disabled on my Ubuntu OS. To unblock it, use the following command:

rfkill unblock wifi

Step 2: Find The Name of Your Wireless Interface And Wireless Network

Run iwconfig command to find the name of your wireless interface.

iwconfig

wlan0 used to be a common name for wireless network interface on Linux systems without Systemd. Because Ubuntu uses Systemd, you are going to find that your wireless network interface is named something like wlp4s0. You can also see that it’s not associated with any access point right now.

ubuntu server connect to wifi terminal

Then find your wireless network name by scanning nearby networks with the command below. Replace wlp4s0 with your own wireless interface name. ESSID is the network name identifier.

sudo iwlist wlp4s0 scan | grep ESSID

ubuntu 19.04 connect to wifi command line wpa supplicant

Step 3: Connect to Wi-Fi Network With WPA_Supplicant

Now install wpa_supplicant on Ubuntu 18.04/19.04 from the default software repository.

sudo apt install wpasupplicant

We need to create a file named wpa_supplicant.conf using the wpa_passphrase utility. wpa_supplicant.conf is the configuration file describing all networks that the user wants the computer to connect to. Run the following command to create this file. Replace ESSID and Wi-Fi passphrase with your own.

wpa_passphrase your-ESSID your-wifi-passphrase | sudo tee /etc/wpa_supplicant.conf

wpa_passphrase

The output of wpa_passphrase command will be piped to tee, and then written to the /etc/wpa_supplicant.conf file. Now use the following command to connect your wireless card to wireless access point.

sudo wpa_supplicant -c /etc/wpa_supplicant.conf -i wlp4s0

The following output indicates your wireless card is successfully connected to an access point.

Successfully initialized wpa_supplicant
wlp4s0: SME: Trying to authenticate with c5:4a:21:53:ac:eb (SSID='LinuxBabe.Com Network' freq=2437 MHz)
wlp4s0: Trying to associate with c5:4a:21:53:ac:eb (SSID='LinuxBabe.Com Network' freq=2437 MHz)
wlp4s0: Associated with c5:4a:21:53:ac:eb
wlp4s0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
wlp4s0: WPA: Key negotiation completed with c5:4a:21:53:ac:eb [PTK=CCMP GTK=CCMP]
wlp4s0: CTRL-EVENT-CONNECTED - Connection to c5:4a:21:53:ac:eb completed [id=0 id_str=]

Note that if you are using Ubuntu desktop edition, then you need to stop Network Manager with the following command, otherwise it will cause connection problem when using wpa_supplicant.

sudo systemctl stop NetworkManager

And disable NeworkManager auto-start at boot time by executing the following command.

sudo systemctl disable NetworkManager

By default, wpa_supplicant runs in the foreground. If the connection is completed, then open up another terminal window and run

iwconfig

You can see that the wireless interface is now associated with an access point.

enable wifi on ubuntu using terminal command

You can press CTRL+C to stop the current wpa_supplicant process and run it in the background by adding the -B flag.

sudo wpa_supplicant -B -c /etc/wpa_supplicant.conf -i wlp4s0

Although we’re authenticated and connected to wireless network, but we don’t have an IP address yet. To obtain a private IP address from DHCP server, use the following command:

sudo dhclient wlp4s0

Now your wireless interface has a private IP address, which can be shown with:

ip addr show wlp4s0

ubuntu dhclient obtain private ip address

Now you can access the Internet. To release the private IP address, run

sudo dhclient wlp4s0 -r

Connecting to Hidden Wireless Network

If your wireless router doesn’t broadcast ESSID, then you need to add the following line in /etc/wpa_supplicant.conf file.

scan_ssid=1

Like below:

network={
        ssid="LinuxBabe.Com Network"
        #psk="12345qwert"
        psk=68add4c5fee7dc3d0dac810f89b805d6d147c01e281f07f475a3e0195
        scan_ssid=1
}

Auto Connect At Boot Time

To automatically connect to wireless network at boot time, we need to edit the wpa_supplicant.service file. It’s a good idea to copy the file from /lib/systemd/system/ directory to /etc/systemd/system/ directory, then edit the file content, because we don’t want newer version of wpa_supplicant to override our modifications.

sudo cp /lib/systemd/system/wpa_supplicant.service /etc/systemd/system/wpa_supplicant.service

Edit the file with a command line text editor, such as Nano.

sudo nano /etc/systemd/system/wpa_supplicant.service

Find the following line.

ExecStart=/sbin/wpa_supplicant -u -s -O /run/wpa_supplicant

Change it to the following. Here we added the configuration file and the wireless interface name to the ExecStart command.

ExecStart=/sbin/wpa_supplicant -u -s -c /etc/wpa_supplicant.conf -i wlp4s0

If you can find the following line in this file, comment it out (Add the # character at the beginning of the line).

Alias=dbus-fi.w1.wpa_supplicant1.service

Save and close the file. (To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X.) Then enable wpa_supplicant service to start at boot time.

sudo systemctl enable wpa_supplicant.service

We also need to start dhclient at boot time to obtain a private IP address from DHCP server. This can be achieved by creating a systemd service unit for dhclient.

sudo nano /etc/systemd/system/dhclient.service

Put the following text into the file.

[Unit]
Description= DHCP Client
Before=network.target
After=wpa_supplicant.service

[Service]
Type=simple
ExecStart=/sbin/dhclient wlp4s0

[Install]
WantedBy=multi-user.target

Save and close the file. Then enable this service.

sudo systemctl enable dhclient.service

Wrapping Up

I hope this tutorial helped you connect Ubuntu 18.04/19.04 to Wi-Fi network from command line with WPA Supplicant. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks 🙂

Rate this tutorial
[Total: 1 Average: 4]

9 Responses to “Connect to Wi-Fi From Terminal on Ubuntu 18.04/19.04 with WPA Supplicant

  • UnoQueMadruga
    3 months ago

    Nice tutorial!!

    Are pass-phrases stored in clear on /etc/wpa_supplicant.conf?

    Cheers!

    • The /etc/wpa_supplicant.conf file contains an encoded passphrase and a clear text passphrase, which is commented out. You can remove the line that contains your clear text passphrase.

  • So that’s how it’s done! I was using nmtui, which works great from my desktop machine, but wouldn’t work on a smaller Linux device running Ubuntu 18.04 headless server.

  • Mysterion
    2 months ago

    Thanks. It worked like a charm 🙂

  • Didn’t work for me! All good till I try to enable the service at start with
    sudo systemctl enable wpa.supplicant.service
    where I get the response
    Failed to enable unit: File /etcsystemd/system/dbus-fi.wi.wpa_supplicant1.service already exists and is a smylink to /lib/systemd/system/wpa_supplicant.service

    • Comment out the following line in /etc/systemd/system/wpa_supplicant.service file.

      Alias=dbus-fi.w1.wpa_supplicant1.service

      Reload systemd.

      sudo systemctl daemon-reload

      Then enable wpa_supplicant.

      sudo systemctl enable wpa_supplicant.service
  • It works well for me, but in case I go to another place with Wi-Fi I have to do the same because, I try and I get the network of my house the name, what I want to know if I can connect to another Wi-Fi network that is not my home in this case the faculty and if that were the case I do the same or do I have to do something else?

  • Because Ubuntu uses Systemd, you are going to find that your wireless network interface is named something like wlp4s0

    You can maintain the old-style naming of interfaces in many ways. The best is probably editing ‘/etc/default/grub’ and make sure that you change

    GRUB_CMDLINE_LINUX=""

    to

    GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"

    and then run ‘update-grub’ with root privileges and then reboot. More details here:

    https://askubuntu.com/questions/689070/network-interface-name-changes-after-update-to-15-10-udev-changes

  • Joshua Shell
    4 weeks ago

    It should be noted that the default service file in ubuntu 18 has wpa_supplicant set to start before dbus which is usually before the network card drivers are even loaded. If you have this problem and need your network card to start on boot, change the line that looks like:
    After=dbus.service
    to
    After=network.service

    and also remove (or add a # at the beginning of) the line that looks like:
    #Before=network.target

    That way the service will give your wifi network card a chance to start up before it tries to start wpa_supplicant.

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • If you ask me more than 5 questions, I expect you to make a donation, or I would stop answering your questions.