3

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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user5265160
  • 409
  • 1
  • 8
  • 19
  • Stack Overflow is for programming questions. General Linux usage questions should be posted to [Unix & Linux](https://unix.stackexchange.com/), [Server Fault](https://serverfault.com/) or [Super User](https://superuser.com/) instead. – John Kugelman Jun 21 '21 at 00:26

2 Answers2

3

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!

QuickPrototype
  • 833
  • 7
  • 18
2

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.

ofirule
  • 4,233
  • 2
  • 26
  • 40