Ubuntu is one of the easiest Linux distributions to use thanks to its powerful and well-designed graphic interface. But what happens on servers where there isn’t one? How do I connect to a Wifi network to have the Internet from the terminal? Well, that’s the objective of this post, to learn how to connect to Wifi from the terminal on Ubuntu.
As I mentioned before, the great graphic interface of Ubuntu makes many tasks easy to do such as connecting to a network. However, in the case of servers with Ubuntu installed these do not have a graphical interface and you have to pull the terminal. But don’t worry it is much simpler than you might think.
So, let’s get started.
Learning how to connect to Wifi from terminal on Ubuntu
1.- How to get the name of the network interface of the Wifi card
One of the first things we have to do is to find out the name of the network interface of the Wifi card. In case you already know it, you can skip this step, but if you don’t, you can use the following command:
ls /sys/class/net
And in my case I get the following:
enp6s0f0 the wlp5s0
Usually, in the case of wireless cards, the name of the network interface starts with W. So in my case, it’s wlp5s0
.
If you don’t see one, it’s probably not connected or you have some driver problem.
Now we can continue.
2.- The connecting to wifi process
When no graphical interface is installed on Ubuntu, all network configuration is done by Netplan. That’s why the Netplan configuration file must be modified to establish the wifi connection.
So, first create a backup of the file before editing it:
sudo cp /etc/netplan/50-cloud-init.yaml /etc/netplan/50-cloud-init.yaml.bak
Now we can edit it with the confidence of knowing that we have a backup done.
The file is in a YAML format so you have to be careful with white spaces and editing in general.
sudo nano /etc/netplan/50-cloud-init.yaml
And, under the first configuration, add the basic information about the network you want to connect to as the SSID and the password:
wifis: wlp5s0: dhcp4: true optional: true access-points: "SSID": password: "password"
Then, save the changes and before applying them, make sure that the wpa_supplicant
service is running to avoid problems:
sudo systemctl start wpa_supplicant sudo systemctl status wpa_supplicant ● wpa_supplicant.service - WPA supplicant Loaded: loaded (/lib/systemd/system/wpa_supplicant.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2020-10-08 12:12:28 -04; 58min ago Main PID: 963 (wpa_supplicant) Tasks: 1 (limit: 4414) CGroup: /system.slice/wpa_supplicant.service └─963 /sbin/wpa_supplicant -u -s -O /run/wpa_supplicant
With the service running correctly, it now applies the changes in Netplan.
sudo netplan generate
sudo netplan apply
And now hopefully you should be able to connect to the WiFi network.
Alternative method: using nmtui
Alternatively, it is possible to use a tool called nmtui
. This tool works in some systems where Network Manager manages the networks.
This method is quite convenient to use when for some reason the Ubuntu graphical interface fails.
To do this, just type the following command in the terminal:
nmtui
And you will see the following screen where you can choose the Activate Connection option:
Then, you will see a screen where you can see the connections available in the different network interfaces. Only, choose the WiFi network you want to connect to.
Now you have to set the password in case the network is protected.
In the end, you will connect to the network. You can check this because a “*” will appear next to the connected network.
This method is quite easy and ideal for beginners.
Conclusion
It may seem that connecting to Wifi from a terminal in Ubuntu is something quite complicated but thanks to this post, you have been able to verify that it is not. So this can help you a lot in both home and production servers.
Also you can read:
How to set a static ip address on Ubuntu 20.04?
So, share this post and join our Telegram channel and our Facebook page. Also, buy us a coffee 😉
Great post! NMTUI was one I hadn’t even thought about…