Changing network at runtime - questions

Hello,
My device will have a possiblity to connect via ethernet, wifi, and lte modem. I need to change the configuration of wifi and lte modem from time to time at runtime. I’ve read the documentation but have some questions.

  1. Is this " Changing the Network at Runtime" permanent, or need to be changed after every reboot, because after reboot it will be set to the default network configuration located in /mnt/boot/system-connections ?
  2. How about priority? Currently, my ethernet connection goes up automatically and it is a main connection. Wifi is a second choice, and the LTE is the last choice. I have named the files acordingly to doc, so the wifi is resin-wifi-01, and lte is resin-wifi-02, so the “01” has higher priority than “02”. I do not see such a mechanism (naming the connecton file) when using dbus and Network Manager.

Best regards
Jurand

Hi Jurand,

When you use some NetworkManager library, the NetworkManager D-Bus API or for example application like our wifi-connect to create/change a networking profile, all the changes will be stored persistently. These will be stored in /etc/NetworkManager/system-connections. If you change the networking setup at runtime, you usually do not have to store anything in /mnt/boot/system-connections, just create the profile dynamically on first boot and it will be stored in the right place. As for accessing NetworkManager, I suggest that you use some ready-to-use library instead of dealing with D-Bus directly.

As for the priority, it is set automatically by NetworkManager depending on the device type - so Ethernet has highest, WiFi is second, and LTE is last. That could be changed though if necessary by tweaking the particular NetworkManager connection profile.

Thanks,
Zahari

Thanks, but just to be completely sure. This documentation has an chapter “changing the network at runtime”, but the example is using python dbus, so it will be permanent, right?
Second thing, about priorities, what if we have for example ethernet, and two WIFIs? Now I can name them resin-wifi-01 and resing-wifi-02 and the priorities are set. Can I name them in the same way using python dbus API?

Yes, when the AddConnection or AddAndActivateConnection methods are called, a persistent connection profile will be created on the filesystem. The changes will be permanent.

Regarding the connection priorities, if you have two WiFi interfaces, by default they will have an equal priority. Both interfaces will have a route metric of 600. The higher the route metric, the lower the priority. To change the route metric for a particular NetworkManager connection, you can adjust the ipv4.route-metric setting, which defaults to 600 for WiFi interfaces under NetworkManager.

If one of those WiFi interfaces is not supposed to connect to the Internet, but only to an internal network, you can set ipv4.never-default to true. This will tell the OS to never use that connection for Internet access.

A useful command that will show you the route metrics for all interfaces and which interfaces are marked as default is ip route.

Hi, thanks again for the info. I have added the code for network configuration on working device and it generally works. The problem is when I try to test if it properly connects to the different network connections when I disconnect eth cable, disable wifi, and again reconnect cable, enable wifi etc. After some tests it sometimes disconnects from VPN in balena cloud, and I have to reboot the device. Do you have any advice to that?

Hello Jurand,
Glad the code provided by Zahari worked out for you. Regarding the VPN disconnect problem, what BalenaOS version and deviceType are you using? Does the device work and reconnect to VPN after reboot? If yes, I would suggest enabling persistent logging (in the device configuration) and trying to reproduce again. If the device gets online after a reboot, with persistent logging enabled, you can get relevant information in the device logs (that you can access with journalctl in a terminal session).