Connecting to WiFi using nmcli

I am perfectly aware most of you know how to do this, but as I tried to connect my Raspberry PI 4 server to WiFi using the command line, standing there in front of the terminal I realized I have no idea how to do it. You see when your default network server connection is wired, you just plug it in and everything is configured to make it work like magic. Not so with WiFi, the sole reason being you need to select the WiFi network and enter the access credentials.

So let’s see how to do that using Fedora Server 36. For now, Fedora Server still uses Network Manager to configure network connections. I am indeed hoping it will soon be replaced by systemd-networkd but until then, we need to get acquainted with nmcli, the console command tool for Network Manager.

Basic wifi connection

To cut it short, I will just drop the connection line. At least in Fedora Server 36 everything is ready to connect so you won’t need any extra commands:

#sudo nmcli dev wifi connect "network" password "password"

So for example if the WiFi network is called /dev/null and the password is letmein, we would write:

#sudo nmcli dev wifi connect "/dev/null" password "letmein"

That’s all we need to know to connect. As good news, the changes we make with nmcli are automatically persisted in Network Manager, so next time we boot the WiFi network will connect automatically. There is one more option if you don’t want to just write the network password in the command line:

#sudo nmcli --ask dev wifi connect "/dev/null"

The above command will ask for the password instead of receiving it as a command line parameter so it won’t be saved in the bash history.

Other useful nmcli options

Besides connecting to WiFi, nmcli allows us to manage many useful network options. To remain in the WiFi area, we can check the WiFi radio using nmcli radio which will give us the WiFi status:

If we want to enable or disable WiFi we can simply say nmcli radio wifi on or nmcli radio wifi off. If we want to list all available WiFi networks, we can use nmcli dev wifi list:

We can also get a full network device status using nmcli dev status:

The above shows us all the installed network devices and their connection. We can see eth0 is unavailable which means there is no cable connected to it, and wlan0 is connected to the /dev/random WiFi network. Also, since Network Manager persists all settings and connections, we can see all the saved network connections using nmcli con show:

The above output indicates that even though there is no wired connection at this point, there was one present and it was saved under Wired connection 1. We know the connection is not working though as it is not assigned to any device. Now that we have the network identifiers, we can use nmcli con up and nmcli con down giving the required UUID or name to turn the connection on and off.

I didn’t intend for this article to be long, nor exhaustive. It’s just a remainder of the simple nmcli command to connect to a WiFi network. Maybe in the future Fedora will switch to systemd-networkd which has a more systemd kind of configuration available. But until then, I hope this will be useful to you. Thanks for reading and see you next time!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store