6

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.

Thanks.

4 Answers 4

4

Use nmcli command line to create connection profile and linked to the interface.

nmcli connection add con-name staticx-03 connection.autoconnect-priority -1 connection.autoconnect-retries -1 ifname enp0s25 type ethernet ip4 192.168.62.74/22 gw4 192.168.60.1 ipv4.dns 208.67.222.222

create another connection profile and linked to the same interface but with higher priority.

nmcli connection add con-name dhcpx-02 connection.autoconnect-priority 1 connection.autoconnect-retries 2 ifname enp0s25 type ethernet

Then check the connection created.

nmcli con show

Use other commands such as below to troubleshoot, up/down connection, remove connection.

nmcli con reload
nmcli con del profilename
nmcli con up profilename 
nmcli con down profilename

Always remember to remove the default connection, for my case is

'Wired connection 1'

or in the /etc/NetworkManager/system-connections it will be

'Wired connection 1.nmconnection'
3

You can solve this by creating two profiles (aka. connection).

  1. A DHCP config with high priority that tries for some time
  2. A static config with lower priority that connects if DHCP fails

In config files (e.g. /etc/NetworkManager/system-connections/*.nmconnection) :

The DHCP profile

[connection]
id=dhcp
uuid=<an UUID>
type=ethernet
interface-name=eth0
autoconnect-priority=1
autoconnect-retries=2

[ipv4]
dns-search=
method=auto
dhcp-timeout=10
may-fail=false

The static profile

[connection]
id=static
uuid=<an UUID>
type=ethernet
interface-name=eth0
autoconnect-priority=0
autoconnect-retries=-1

[ipv4]
address1=192.168.1.10/24,192.168.1.1
dns=192.168.1.1
dns-search=
method=manual

Note : by default, autoconnect=true when not specified, otherwise it wouldn't work.

2
  • Does this approach handle "hot-swapping" between DHCP and static address? I.e. boot the PC without the cable connected. Connect a cable to a laptop (no DHCP). Then remove the cable and connect to a router (with DHCP) and so on.
    – Mark
    Jan 16, 2022 at 8:15
  • I don't know exactly. You should check the docs here: developer-old.gnome.org/NetworkManager/stable. AFAIK, the connections state change are triggered by the "device" state change. This means that if the "device" (e.g. eth0) is (un)plugged, it will check again all profiles with eth0 as interface-name, check their priorities and try to connect them. Easiest would be to test.
    – cmr
    Jan 18, 2022 at 8:31
2

On Ubuntu 20.04 LTS, the configuration settings below worked for me.

FWIW: I have two virtual machines (VM) that are connected to a private Ethernet network (192.168.1.x/24). One VM provides the DHCP server, the other VM is a DHCP client. When the DHCP server VM is not running, I want the DHCP client VM to attempt to obtain an IPv4 address via DHCP, and when that fails, to fall back to assigning itself an IPv4 address. Additionally, on the DHCP client I want to reduce the DHCP timeout to 2 seconds, and to limit the number of DHCP attempts to two attempts. This internal network does not use IPv6, so the configuration below disables IPv6.

:: IMPORTANT ::

The IPv4 and IPv6 subsystems both affect the DHCP timeout duration. To achieve a 2 second timeout on DHCP requests, the IPv4 and IPv6 subsystems must both be configured for a 2 second timeout. (Alternatively, you can disable the IPv6 subsystem if it's not used.) Otherwise, one subsystem uses the specified 2 second timeout (e.g., ipv4), and the other subsystem continues to use its default timeout (e.g., 30 seconds).

sudo nmcli connection add \
    con-name Private_DHCP \
    ifname ens37 \
    type ethernet \
    autoconnect true \
    connection.autoconnect-priority 1 \
    connection.autoconnect-retries 2 \
    ipv6.method disabled \
    ipv4.method auto \
    ipv4.dhcp-timeout 2 \
    ipv4.may-fail no \
    save yes

sudo nmcli connection add \
    con-name Private_Static \
    ifname ens37 \
    type ethernet \
    autoconnect true \
    connection.autoconnect-priority 0 \
    connection.autoconnect-retries -1 \
    ipv6.method disabled \
    ipv4.method manual \
    ip4 192.168.1.201/24 \
    gw4 192.168.1.254 \
    ipv4.dns <dns_server_#1_ipv4> \
    +ipv4.dns <dns_server_#2_ipv4> \
    save yes

sudo nmcli connection reload

Of course, you'll need to replace the strings <dns_server_#?_ipv4> with the IPv4 addresses of your own DNS servers.

1

I've also tried the same configuration on Ubuntu 18.04 - it just doesn't work (played with ipv4.addresses, connection.autoconnect, connection.autoconnect-retries, ipv4.dhcp-timeout, ipv4.method, ipv4.may-fail, etc.)

The only workable option was:

1) disable NetworkManager

2) /etc/network/interfaces.d/some_name

    auto lo eth0
    iface lo inet loopback
    iface eth0 inet dhcp

3) /etc/dhcp/dhclient.conf

    timeout 10;
    alias {
      interface "eth0";
      fixed-address 192.168.1.1;
      option subnet-mask 255.255.255.0;
    } 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .