Connect to WiFi from command line in Ubuntu Server

Ubuntu has became one of the most popular Linux distribution in the market. It is available in various forms : server, desktop, minimal editions as well as many other Ubuntu flavors such as Ubuntu Kylin, Ubuntu Cloud, Xubuntu, Kubuntu, etc.

Ubuntu Server is the slimmed down, optimized for server usage edition of Ubuntu. The biggest difference an ordinary user can tell is that Ubuntu Server doesn’t come with any GUI or desktop manager. This is both a blessing and a curse. On one hand, commands is undeniably a versatile tool to do so many complex tasks, but on the other hand, simple actions like connecting to WiFi networks may take further steps to be done.

In this article, we will show you how to connect to a WiFi network using command line interface in Ubuntu Server. This may be helpful in case you’re using any other edition of Ubuntu and cannot boot into the graphical interface but needing the internet to upgrade or install additional software.

Scan for WiFi networks from command line

The first thing you need to do is running a wireless scan to list all the available WiFi networks. In order to do that, run the following command :

nmcli d wifi list

You would see a list of all WiFi networks in range as well as information about their channels, transfer rate, signal, security protection type.

[email protected]:~$ nmcli d wifi list
IN-USE  BSSID              SSID                MODE   CHAN  RATE        SIGNAL  BARS  SECURITY  
        90:FD:73:D7:30:D7  BDADTXQLDDCTNNVTNT  Infra  3     130 Mbit/s  59      ▂▄▆_  WPA1 WPA2 
        C4:E9:84:B9:9F:5C  PCKLDJFCQC          Infra  1     270 Mbit/s  57      ▂▄▆_  WPA2      
*       04:95:E6:BD:44:B0  PHRGCSXOP           Infra  6     270 Mbit/s  54      ▂▄__  WPA1 WPA2 

In case you don’t get the output similar to what’s shown above, make sure the WiFi radio is on by running nmcli r wifi on. You can verify that the wifi radio or device is enabled by running nmcli d :

[email protected]:~$ nmcli d
DEVICE                   TYPE      STATE         CONNECTION         
enp3s0                   ethernet  connected     Wired connection 1 
tailscale0               tun       connected     tailscale0         
wlxc4e98417f9a2          wifi      connected     PrxdfOP          
docker0                  bridge    connected     docker0            
lo                       loopback  unmanaged     --     

Notice the wlxc4e98417f9a2 device with wifi type in the output, that’s the wireless device.

Connect to a WiFi network from command line

Given that you know the SSID and the password of the WiFi network you’re going to establish a connection, run the following command to tell Ubuntu Server to connect to it.

nmcli d wifi connect wifi_name password the_password

Remember to replace wifi_name with the SSID of your network and the_password with the actual password. The password should be 8-63 characters or 64 hexadecimal characters to specify a full 256-bit key.

Connect to hidden WiFi networks from command line

A hidden network is a normal wireless network that simply does not broadcast it’s SSID unless solicited. You cannot do a regular WiFi scan as it doesn’t give you a name, instead, you have to manually create a connection associated to the network using a random name, then modify its password and then bring up the interface. Run the following commands to do so:

$ nmcli c add type wifi con-name <a_name> ifname wlan0 ssid <a_ssid>
$ nmcli c modify <a_name> wifi-sec.key-mgmt wpa-psk wifi-sec.psk <a_password>

After that, bring up the connection by typing:

$ nmcli c up <a_name>

We hope that the information above helped you learn how to connect to WiFi from command line in Ubuntu Server.

You may be interested in our Linux software roundups, including 8 Best Open Source CMDB software, Best Linux Video Converters, Best Linux Home Security Software, or best SNES emulator for Linux.

Leave a Comment