1

I'm using 3 NetworkManager connection profiles in a Linux distribution and all the connection profiles refer to the same ethernet interface: enp3s0. I'm using these connections to manage:

  • the default static IP configuration
  • set a new static IP configuration
  • request a DHCP address

In the rest of the question I'll use the term connection to mean the term connection profile.

To change Ethernet configuration I change the priority of the connection, so if I would like to enable the default static IP configuration I set a priority for that greater than static IP configuration and DHCP configuration.
Read the paragraph "Set properties of the 3 connections" in the continuation of the question to get details about setting of priority of a connection.

The question

If I create connections at boot by a bash script started by a systemd service all works correctly, but if I execute the bash script after the boot has finished, the IP configuration is not correctly managed by NetworkManager and its connection profiles. Go to the paragraph "The problem" at the end of the question to get a detailed description of the error.

The rest of the question give all details needed to understand the problem.
I apologize for the length of the question but there is a lot of information needed.


Creation of 3 NetworkManager connection profiles

To create the connections I'm using the command nmcli as showed below:

# for default static
nmcli c add ifname enp3s0 type ethernet con-name ethernet_default_ipstatic
# for ethernet_ipstatic
nmcli c add ifname enp3s0 type ethernet con-name ethernet_ipstatic
# for ethernet_dhcp
nmcli c add ifname enp3s0 type ethernet con-name ethernet_dhcp

After the execution of previous commands in the path /etc/NetworkManager/system-connections, there will be present 3 files called:

  • ethernet_default_ipstatic.nmconnection
  • ethernet_ipstatic.nmconnection
  • ethernet_dhcp.nmconnection

Set properties of the 3 connections

Previous commands create 3 connections with default properties, so after creating them it is necessary to set their properties. For this goal I still used the command nmcli as showed below.

For the ethernet_default_ipstatic set the following properties for the connection:

nmcli con mod ethernet_default_ipstatic ipv4.method manual ipv4.addresses 192.168.1.1/24 ipv6.method disabled

Previous command set the following properties for the connection:

  • ipv4.method = manual (this set IP Static and not DHCP)
  • ip address 192.168.1.1, netmask 255.255.255.0 (no gateway)
  • IPV6 disabled
  • the autoconnection propriety remains to default yes
  • the priority property remains to default 0

For the ethernet_ipstatic the properties are:

nmcli con mod ethernet_ipstatic ipv4.method manual ipv4.addresses 192.168.1.1/24 ipv4.gateway 192.168.1.100 ipv4.may-fail no ipv6.method disabled connection.autoconnect no connection.autoconnect-priority -1

Previous command set the following properties for the connection:

  • ipv4.method = manual (this set IP Static and not DHCP)
  • ip address 192.168.1.1, netmask 255.255.255.0, gateway 192.168.1.100
  • IPV6 disabled, autoconnection no, priority -1

For the ethernet_dhcp the properties are:

nmcli con mod ethernet_dhcp ipv4.method auto ipv4.addresses '' ipv4.gateway '' ipv4.may-fail no ipv4.dhcp-timeout 20 ipv6.method disabled connection.autoconnect no connection.autoconnect-priority -1 connection.autoconnect-retries 3

Previous command set the following properties for the connection:

  • ipv4.method = auto (this set DHCP and not Static IP Address)
  • ip address '', gateway ''
  • IPV6 disabled, autoconnection no, priority -1

Default configuration

By previous commands the connection ethernet_default_ipstatic has a priority > than others so the ethernet interface enp3s0 is configured with an IP static 192.168.1.1/24.

Execution of nmcli commands by a service

All work correctly If I execute previous nmcli commands at boot by the following service (my_custom_nm_service.service):

[Unit]
Description=Init NetworkManager Ethernet Connections
Requires=NetworkManager.service
After=NetworkManager.service
Before=network.target

[Service]
Type=oneshot
ExecStart=/home/<username>/script_services/init_connections.sh
User=<username>

[Install]
WantedBy=multi-user.target

where init_connections.sh executes previous nmcli commands.

Note. In the unit file note the presence of the opsions:

  • Requires=NetworkManager.service
  • After=NetworkManager.service
  • Before=network.target

The problem

If after the boot has finished I remove all connections, by the command nmcli con del, and I execute the script init_connections.sh I obtain a unpredictable IP configuration for the system.
The typical misconfiguration that occurs is as follows:

  • the active connection (showed by nmcli con command) is ethernet_default_ipstatic so the system should have the static IP address 192.168.1.1/24
  • but the system has obtained an IP address by the DHCP server

When the script init_connections.sh is executed by the service, it is executed before network.target, but honestly I don't know if this difference could be important to understand the problem or not.

7
  • Instead of calling init_connections.sh, why not run systemctl restart my_custom_nm_service? You can then really compare with boot time since systemd will take care of the dependencies. Or just systemctl restart NetworkManager? since you are doing a nmcli con del.
    – xhienne
    Commented Nov 6, 2023 at 10:49
  • @xhienne My goal is not use the service my_custom_nm_service and create connections by a Python program which call nmcli commands as in the script init_connections.sh.
    – User051209
    Commented Nov 6, 2023 at 11:15
  • @xhienne I have tried to call systemctl restart NetworkManager after deleting connections but this not solves the problem; the same if I execute systemctl restart NetworkManager after creating the connections my system get the DHCP address and not IP Static default address.
    – User051209
    Commented Nov 6, 2023 at 11:18
  • @xhienne Thanks. Your hints have helped me to find the solution.
    – User051209
    Commented Nov 6, 2023 at 15:34
  • You're welcome. Congrats for the quality of both your question and your answer. Don't forget to accept it in a couple of days.
    – xhienne
    Commented Nov 6, 2023 at 15:54

1 Answer 1

1

I have found the problem: to set correctly the default configuration I need to execute the nmcli con up command as showed below:

nmcli con up "ethernet_default_ipstatic" iface enp3s0

as also indicated in this post.

Note. In this moment for me is not important to know why the execution of the script init_connections.sh works well at boot by the service my_custom_nm_service.service. My real interest is to make working the script at the end of the boot process.


For completeness I write the steps of my test.

1) remove the service

I have disabled the service my_custom_nm_service.service which started the execution of the script init_connections.sh

2) delete all connections

After the boot has finished I have deleted all the connection by the following script (delete_connections.sh):

nmcli c del ethernet_default_ipstatic
nmcli c del ethernet_ipstatic
nmcli c del ethernet_dhcp

3) creation of the 3 connections: the malfunction appears

Execute the script init_connections.sh:

nmcli con add ifname enp3s0 type ethernet con-name ethernet_default_ipstatic
nmcli con mod ethernet_default_ipstatic ipv4.method manual ipv4.addresses 192.168.1.1/24 ipv6.method disabled
nmcli con add ifname enp3s0 type ethernet con-name ethernet_ipstatic
nmcli con mod ethernet_ipstatic ipv4.method manual ipv4.addresses 192.168.1.1/24 ipv4.gateway 192.168.1.100 ipv4.may-fail no ipv6.method disabled connection.autoconnect no connection.autoconnect-priority -1
nmcli con add ifname enp3s0 type ethernet con-name ethernet_dhcp
nmcli con mod ethernet_dhcp ipv4.method auto ipv4.addresses '' ipv4.gateway '' ipv4.may-fail no ipv4.dhcp-timeout 20 ipv6.method disabled connection.autoconnect no connection.autoconnect-priority -1 connection.autoconnect-retries 3

After the execution of the script, my Linux system, presents the problem described in the question that is:

  • nmcli con command tells that the active connection is ethernet_default_ipstatic (because it has the greater priority)
  • the IP address has been assigned by the DHCP Server

4) solution: execute nmcli con up

The solution is execute the command:

nmcli con up "ethernet_default_ipstatic" iface enp3s0

and after that the interface enp3s0 has the default IP address

You must log in to answer this question.

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