Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 568
user5265160 Avatar asked Oct 25 '25 19:10

user5265160


2 Answers

The solution I managed to use is referenced from Static IP nmcli

Basically all you have to do is assign the interface with a static IP address/s with keeping the ipv4.method as "auto" for DHCP. For those like me, Yes it is very possible to have a single physical interface (ethernet port) that has multiple IP addresses all at once. To see the current static IPv4 addresses and mode set through NM use:

nmcli con show <ConnectionName> | grep "ipv4.addresses"
nmcli con show <ConnectionName> | grep "ipv4.method"

Now set 1 or more static IP addresses:

sudo nmcli con modify <ConnectionName> ipv4.addresses "10.10.10.10/24,192.168.88.88/24"

For completeness to remove static address/s set through NM use:

sudo nmcli con modify <ConnectionName> ipv4.addresses ""

Now the slightly confusing thing is that ifconfig will not show the multiple IP addresses. However, examine the output of plain nmcli command you will see that your connection has multiple inet4 addresses.

Also to confirm you can examine the output of your routing table route -n and you see that only the your static IP's will only route on local network. The DHCP IP will route packets externally.

Hope this helps!

like image 102
QuickPrototype Avatar answered Oct 29 '25 05:10

QuickPrototype


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.

like image 31
ofirule Avatar answered Oct 29 '25 05:10

ofirule