Connect to Wi-Fi From Terminal on Ubuntu 22.04/20.04 with WPA Supplicant

2023-01-22 1423 words 7 mins read

How to Connect Ubuntu to Wi-Fi card.

This article describes how to connect your Linux Ubuntu server to wifi, primarily if you are using a wifi adaptor card. In my example, I will be connecting my W-iFi card from Gen Basic to my Lebre “Le Potato” s905x-cc single board computer server.


Le Potato “s905x-cc” GenBasic Wi-Fi Card
Le Potato. GenBasic Wirless Adapter.

We will be storing our Wi-Fi network and password in a wpa_supplicant configuration file. Please note that you will need an existing internet connection to install the wpa_supplicant software before connecting to Wi-Fi. You can achieve this by connecting your server through a Wired Ethernet first, which is done for just one time. This is because Ubuntu doesn’t ship wpa_supplicant on a clean install.


Step 1: Locate The Device Name of Your Wireless Interface And Wireless Network

First, update and upgrade your system, the “y” flag confirms the updates.

   
   sudo apt -y update && sudo apt -y upgrade 
   

You may also have to install wire-lesstools

sudo apt -y install wireless-tools net-tools wpasupplicant

Then run the command iwconfig command to find the name of your wireless interface.

iwconfig

You are looking for your wireless network interface named something like wlx3c3300206956. You can also see that it’s offline and not associated with any access point right now.

Run iwconfig and find your wireless network interface name.

Bring up your wireless interface with the following command, replace wlx3c3300206956 with your wireless interface:

sudo ifconfig wlx3c3300206956 up

Then you can find your wireless network name by scanning nearby networks with this command below. Replace wlx3c3300206956 with your own wireless interface name. Your network name identifier is the ESSID.

sudo iwlist wlx3c3300206956 scan | grep ESSID
Wifi Networks.

Step 2: Connect to Wi-Fi Network Using WPA_Supplicant

With wpa_supplicant installed on Ubuntu 22.04/20.04, we need to create a file named wpa_supplicant.conf using the wpa_passphrase utility.

wpa_supplicant.conf is the configuration file detailing all networks that the user wants the Ubuntu server 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_supplicant config, ESSID and Wi-Fi passphrase.

Note: If your ESSID contains whitespaces make sure to wrap it with double quotes when you pass it into the command above.

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

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

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

WiFi Connected Sucessfully.

Ubuntu Desktop

If you are using an Ubuntu desktop, then you need to stop Network Manager with the following command, otherwise, it will cause a connection problem when using wpa_supplicant.

sudo systemctl stop NetworkManager

To disable NetworkManager auto-start at boot time execute the following command.

sudo systemctl disable NetworkManager-wait-online NetworkManager-dispatcher NetworkManager

Step 3: Running WPA_Supplicant In Backround

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

iwconfig

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

Connection Success.

Switch back to your wpa_supplicant screen, and 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 wlx3c3300206956

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

sudo dhclient wlx3c3300206956

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

ip addr show wlx3c3300206956
Connection Success.

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

sudo dhclient wlx3c3300206956 -r

Bonus: Connecting to Hidden Wireless Network

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

scan_ssid=1

Example below:


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

Step 4: Auto-Connect At Boot Time

To have your computer server to automatically connect to wireless network at boot time, we need to edit the wpa_supplicant.service file. It’s generally a good idea to copy the file from /lib/systemd/system/ directory to /etc/systemd/system/ directory, then edit the file content. This is because we don’t want a 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 like 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 config file and the wireless interface name to the ExecStart command.

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

It’s pertenant that you always try to restart wpa_supplicant when failure is detected. Add the following right below the ExecStart line.

Restart=always

If you can find the following line in this file, comment it out by adding the “#” character at the beginning of the line.

Alias=dbus-fi.w1.wpa_supplicant1.service

Which should then look like:

# Alias=dbus-fi.w1.wpa_supplicant1.service

Save and close the file. (In Nano text editor, to save a file press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X.) Then reload systemd with this command:

sudo systemctl daemon-reload

Run this next command to enable wpa_supplicant service to start at boot time.

sudo systemctl enable wpa_supplicant.service

We will also need to start dhclient at boot time so we can obtain a private IP address from DHCP server. You can do this 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=forking
ExecStart=/sbin/dhclient wlp4s0 -v
ExecStop=/sbin/dhclient wlp4s0 -r
Restart=always
 
[Install]
WantedBy=multi-user.target

Save and exit the file. Then enable this service.

sudo systemctl enable dhclient.service

How to Obtain a Static IP Address

If you want to get a static IP address, then you need to disable dhclient.service.

sudo systemctl disable dhclient.service

We first need to use netplan to configure static IP address on Ubuntu 22.04/20.04. To do this create a configuration file under /etc/netplan/.

sudo nano /etc/netplan/10-wifi.yaml

Next, add the following lines to this file. Make sure to replace 192.168.0.102 with your preferred IP address. Also be careful about the indentation. An extra space will make the configuration invalid.


network:
    ethernets:
        wlp4s0:
            dhcp4: no
            addresses: [192.168.0.102/24]
            gateway4: 192.168.0.1
    version: 2

Save and exit the file. Then apply the configurations.

sudo netplan apply

You can also enable the –debug option if this command it doesn’t work as expected.

sudo netplan --debug apply

If there are other .yaml files in the /etc/netplan/ directory, then netplan will automatically combine configurations from different files. netplan uses systemd-networkd as the backend network renderer. It’s recommended you configure the wpa_supplicant.service to run before systemd-networkd.service, so the system first associates with a Wi-Fi access point, then obtain a private IP address.

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

Find the following line:

Before=network.target

Change it to:

Before=network.target systemd-networkd.service

Save and exit the file.

A second way to get a static IP address is by logging into your router’s management interface and assigning a static IP to the MAC address of your wireless card, if your router supports this feature.

How to Obtain a Static IP Address

You don’t technacally need a static IP address for your Ubuntu box. Ubuntu can use mDNS (Multicast DNS) to broadcast its hostname to the local network and clients can access services on your Ubuntu box with that hostname. This hostname can always be translated to the IP address of your Ubuntu box, even if the IP address changes.

In order to use mDNS, you must install avahi-daemon, which is an open-source implementation of mDNS/DNS-SD.

sudo apt install avahi-daemon

Start the service.

sudo systemctl start avahi-daemon

Enable auto-start at boot time.

sudo systemctl enable avahi-daemon

Avahi-daemon listens on UDP 5353, so you need to open this port in your firewall. If you use UFW, then run these commands.

sudo ufw allow 5353/udp
sudo ufw enable

Then you can set a unique hostname for your Ubuntu box with the

hostnamectl
command. Replace ubuntubox with your desired hostname, which cannot already be taken by other devices in the local network.

sudo hostnamectl set-hostname lepotato-main

Now restart avahi-daemon.

sudo systemctl restart avahi-daemon

If you check the status with

sudo systemctl status avahi-daemon

You can see the mDNS hostname, which ends with the

.local
domain.

Hostname.
avahi-daemon mdns hostname

On the client computer, you will also need to install an mDNS/DNS-SD software.

  • Linux users should install avahi-daemon.

  • Windows users need to enable the Bonjour service by either installing the Bonjour print service or installing iTunes.

  • macOS, Bonjour is pre-installed.

Now you can access services by using the ubuntubox.local hostname, without needing to check and type IP address.


Tags: Linux Ubuntu

author

Authored By Is-Rael Landes

Is-Rael Landes, a good man living on the earth, loving making website, teaching others and coding.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it