Setting Up Multiple Wifi Connections with nmcli

ImaginaryTango

New Member
Joined
Apr 29, 2026
Messages
3
Reaction score
1
Credits
52
I've asked about this in multiple forums and get partial answers, but not full ones. I've seen some posts here about nmcli, but even after reading the docs, I still have questions I can't find the answers to.

My goal is to be able to set up multiple wifi connections in one shot - ultimately, I'd like to just use a batch file to do the work.

I'm using a Pi Zero 2W as a prototype and I'll be doing this same thing with some more of them for my use (and a few friends and people I work with will likely find them useful, too). I'm using Debian Trixie, Raspberry Pi OS, and OctoPrint for testing this out and they are all using Network Manager.

I'm having trouble finding clear information on multiple factors of adding new wifi connections. Every tutorial (including this one, on this forum) specifies to connect to the wifi network THEN to add the connection to Network Manager. If you look at the post I linked to as an example, it has the same steps as other tutorials (I think it's just copied and pasted around). The first step is connecting to the wifi network, then the 2nd step is to add the connection. Note the first step includes providing the wifi password. I've tried adding a wifi connection and never get it working. First, when I used the keyword "password" and a value to add, it doesn't work. (I also tried using "psk" instead. No help there.)

So I can't create a connection with nmcli unless I'm currently connected to the wifi. In my case, that would mean the firs time I take this Pi Zero someplace to use it, I have to have access to a keyboard and monitor to set up the connection. I see directions to add an ethernet connection, but that doesn't require an SSID and password/psk. I also tried to create one file and use it as a model for the other connections by copying the file and changing the connection information and the filename, but Network Manager didn't read it as a new connection. (I listed connections in nmcli and it didn't show up.)

I'll number my questions to make it it easier, in case people can provide answers to at least one question:

1) How do I create a new wifi connection if I don't have access to that wifi network at the time I'm setting things up?
2) How do I specify the password or psk while adding a wifi connection (again, when I don't have access to that network at the time of setup)?
3) Is the UUID created automatically by Network Manager when I create a new connection?
4) How is the password/psk encrypted in the file? I have seen some nmconnection files with this in plaintext in the file, and some where it's encrypted.
5) When I tried to make a new connection by copying and edting an nmconnection file, it didn't show up. Why? Do I have to do something to say, "Here's a new nmconnection file - read it!" or something similar?
 


I've asked this question in multiple forums and nobody seems to know. The best I was getting was "RTFM," but the manual didn't have all the info I needed.

I finally asked it on SE (my last resort because they can be so pedantic about question formats and care more about form than useful information), and did get some good answers, including one that uses a one line command to make it work: https://unix.stackexchange.com/q/805694/101743
 
I do this quite frequently.

Code:
nmcli device wifi list

If you don't see anything, sometimes you need to refresh.

Code:
nmcli device wifi rescan

Now to actually create the connection.
Code:
nmcli device wifi connect "SSID_Name" password "your_passphrase"

SSID_Name = the name of your wi-fi network.
"your_passphrase" = the password to connect to your wi-fi router.

NetworkManager creates a persistent connection profile automatically. That profile will reconnect at boot without any further configuration.

You can manually connect and disconnect if you want to.

Code:
nmcli connection show

You will likely see a connection with the name of your wi-fi network.

Code:
nmcli connection up "SSID_Name"
nmcli connection down "SSID_Name"

If you want to delete a profile you don't use anymore.
Code:
nmcli connection delete "SSID_Name"
 
To check the radio device status (the wi-fi device in your computer, of a USB dongle adapter.)
Code:
nmcli radio wifi          # shows enabled/disabled state
nmcli device status       # shows all devices and their states

To turn your radio on or off.
Code:
nmcli radio wifi on
nmcli radio wifi off

Bash shell tip, if you're trying to script this in bash.
Code:
nmcli -t -f SSID,SIGNAL device wifi list
 
Now to actually create the connection.
nmcli device wifi connect "SSID_Name" password "your_passphrase"
SSID_Name = the name of your wi-fi network.
"your_passphrase" = the password to connect to your wi-fi router.

NetworkManager creates a persistent connection profile automatically. That profile will reconnect at boot without any further configuration.

The big question is if this will work with a wifi network that isn't there. This is one of the things I tried, but could never get it to work right to create the nmconnection file with the proper info in it. I did use the offline switch and that made things work. I don't know if I was using, for example, one bad keyword (although I was checking that), or did something else wrong. I just could not get it to create the connection for an out-of-range wifi.

If this is working to create a connection to a non-local wifi for you, please let me know, since I might have been doing something wrong.

And thank you for all the other items you included that can be quite useful!
 
If you know the SSID and password in advance, just run the command line.
The problem is.. how do you trigger the command? I wouldn't recommend putting it
in a time loop or cron job. Because it will keep trying to connect to a network that it's already connected to.

Write a bash script, make it executable, and run it when you are in range of the network.
The bash script will have the password viewable in plain text, there are ways around that,
but this is the quick and simple way.
 


Follow Linux.org

Members online


Top