Skip to content

Instantly share code, notes, and snippets.

@TheRealJunior
Created February 22, 2020 13:49
Show Gist options
  • Save TheRealJunior/b891abaadc09c4fbbe03b3ca61c4f92d to your computer and use it in GitHub Desktop.
Save TheRealJunior/b891abaadc09c4fbbe03b3ca61c4f92d to your computer and use it in GitHub Desktop.
wifi terminal ubuntu server
Connect to Wi-Fi From Terminal on Ubuntu 18.04/19.10 with WPA Supplicant
Last Updated: February 3, 2020 Xiao Guoan (Admin) 24 Comments Ubuntu
In this tutorial, we are going to learn how to connect to Wi-Fi network from command line on Ubuntu 18.04/19.10 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.10
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
If your wireless interface isn’t shown, perhaps you need to bring it up with the following command.
sudo ifconfig wlp4s0 up
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.10 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
Note that in the above screenshot, I wrapped my ESSID with double-quotes, because my ESSID contains a whitespace.
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-wait-online NetworkManager-dispatcher 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment