Setting Up a New WiFi Dongle in Linux from the Command Line

I ran into a problem recently. I’d bought a new Wi-Fi USB dongle for my computer. Windows picked it up immediately and connected to my router. My Linux installs did not.

Oh, the dongle worked. My desktop and my notebook (a rehabbed Chromebook) could see the router it had connected to before. But I had to enter my password and create a new connection because the dongle was different.

What was preventing the connection on the old dongle from working with the new dongle? I decided to do some digging.

Digging into the old connection’s .nmconnection file, what I discovered was that there’s a line that defines the interface-name of the connection. Linux hardwired the connection to a specific device. If I deleted that line from the file, then the new dongle worked without creating a whole new connection for it.

There had to be a better way! Maybe an nmcli command could delete that line?

I looked at manuals online, and I experimented for about half an hour, creating and deleting connections, until I hit on the right command that worked.

sudo nmcli connection modify CONNECTION-NAME connection.interface-name ""

I created the connection — to do it from the command line, nmcli dev wifi connect CONNECTION-NAME password PASSWORD worked — then ran the second second command to modify the connection, and the interface-name line of the file was taken out. The connection now worked with any and all of my USB dongles.

(Why did I buy a new dongle? Because I thought my old dongle was failing. No, my USB hub was failing. I replaced the hub, and everything was good again. But now I also had a new dongle.)

Put that together, and you have a little script to create a universal wifi connection in Linux:

nmcli dev wifi connect CONNECTION-NAME password PASSWORD
sudo nmcli connection modify CONNECTION-NAME connection.interface-name ""

Even though I had a solution — and a short script of the two commands above that I could deploy — I wanted to investigate a cleaner solution. Could I create a wifi connection that works from the jump, without needing to edit it?

I started from the script I wrote to create my work VPN connection in Linux — .nmconnection files have the same basic structure, so it seems like a good starting point — and, again with a manual online as reference, experimented. It took a longer than I’d have liked and thought it would take, but it’s clean and it works.

The script I came up:

nmcli connection add \
type wifi \
connection.id CONNECTION-NAME \
connection.autoconnect true \
wifi.mode infrastructure \
wifi.ssid ROUTER-NAME \
wifi-sec.auth-alg open \
wifi-sec.key-mgmt wpa-psk \
wifi-sec.psk PASSWORD \
&

The nice thing about this script is that this will create a wifi connection without the USB dongle being plugged in or the router being turned on. The two-line script that creates the connection and then edits it to make it universal requires an active connection and an active router; if I didn’t have my USB dongle turned on, the script failed and Linux Mint was grumpy. The longer script merely creates a connection in the system; it doesn’t require the connection to actually be there. In other words, it will create a wifi connection on an offline machine. I can see that being useful — and it was, as I used the script with my rehabbed Chromebook last night to update it to the new dongle. 🙂

Published by Allyn Gibson

A writer, editor, journalist, sometimes coder, occasional historian, and all-around scholar, Allyn Gibson is the writer for Diamond Comic Distributors' monthly PREVIEWS catalog, used by comic book shops and throughout the comics industry, and the editor for its monthly order forms. In his over ten years in the industry, Allyn has interviewed comics creators and pop culture celebrities, covered conventions, analyzed industry revenue trends, and written copy for comics, toys, and other pop culture merchandise. Allyn is also known for his short fiction (including the Star Trek story "Make-Believe,"the Doctor Who short story "The Spindle of Necessity," and the ReDeus story "The Ginger Kid"). Allyn has been blogging regularly with WordPress since 2004.

Leave a Reply

Your email address will not be published. Required fields are marked *