Search code examples
dhcpstatic-ip-addressnetworkmanager

Using Network Manager (nmcli) to configure DHCP, with a fallback static IP address in case DHCP fails


I am working with an embedded linux device, which we typically connect to by static IP address during development. However, on-site we have a requirement to connect using DHCP and so, I would like to setup a primary DHCP connection, with a fallback static IP address on a different network. Is this possible?

I can use nmcli to configure a DHCP connection (IPV4.method=manual) and can successfully add an additional static IP address. Both will exist when simultaneously when the DHCP server is present, however when there is no DHCP server, the connection is disabled including the static IP address.

I have also tried setting up multiple connections, assigned to eth0, i.e. one static and one DHCP, and can manually enable them using

nmcli con up ConnectionName

but this does not meet the requirement because of the need to manually enable them in the event of the other failing. I can, of course run a script to check the connection status and enable the other if required but thought this would be a realistic expectation of a network manager.

Is there a way to tell network manager to attempt one connection and if this fails try another?

I am using Ubuntu 18.04.


Solution

  • You need to set two different connections, setting them both to connection.autoconnect yes and also setting connection.autoconnect-priority according to the wanted priority.