Sunday, March 15, 2020

Switch connection between two interfaces using nmcli

A way to switch network interface from console is using nmcli. Going through the man page you can see a small set of examples. For example to create a new connection you can run

$ nmcli dev wifi con "Cafe Hotspot 1" password caffeine name "My cafe"

Some Basic Background

What you need is:
- A valid network device
Make sure that the device has recognized and the drivers have been loaded. After that you should be able to find the interface with nmcli

$ nmcli device 
or
$ nmcli device status

should give you an output like this(if you have no active connection yet)

DEVICE             TYPE      STATE           CONNECTION         
wlp2s0                wifi          disconnected  --

- A connection configuration that can be found under /etc/sysconfig/network-scripts/ (at least in fedora). The configuration file is created by nm when ever for example you run the command that we saw above first to create a new connection.

to check the connection from nmcli

$ nmcli connection show "My Cafe"
Here is good to check some things
- The connection.autoconnect. Make sure that it has "yes" value
- The connection.interface-name. This might have the name of the interface.

Use nmcli to change the interface of the connection

In case you have two interfaces and you want to switch between each other you should be able to do so with

$ nmcli connection up "My Cafe" ifname wlp2s0

This is not always easy as that but it is usually easy to fix. To make this work you might change a few things in the connection configuration. For instance if i have the following error, personally

Error: Connection activation failed: No suitable device found for this connection (device enp1s0 not available because profile is not compatible with device (mismatching interface name)). 
or
Error: device 'wlp0s29u1u3' not compatible with connection '1925_www.radlice.net': The MACs of the device and the connection didn't match..

For the first i solve the problem as i have already say above, removing the you connection.interface-name. For the other one, i checked the the 802-11-wireless.mac-address. it had the MAC from the other interface. I made sure that it is also not set. Easy modify any variable with

$ nmcli connection modify "My Cafe"  802-11-wireless.mac-address ""

No comments: