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.nmconnectionethernet_ipstatic.nmconnectionethernet_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 concommand) isethernet_default_ipstaticso the system should have the static IP address192.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.
init_connections.sh, why not runsystemctl restart my_custom_nm_service? You can then really compare with boot time since systemd will take care of the dependencies. Or justsystemctl restart NetworkManager? since you are doing anmcli con del.my_custom_nm_serviceand create connections by a Python program which callnmclicommands as in the scriptinit_connections.sh.systemctl restart NetworkManagerafter deleting connections but this not solves the problem; the same if I executesystemctl restart NetworkManagerafter creating the connections my system get the DHCP address and not IP Static default address.