On any OS using NetworkManager, such as RHEL 10, when a new network interface is added without a static network configuration, the default metric values assigned are typically 101 and 102 and so on.
However, some cloud vendors (e.g., ECS, EC2) have implemented logic in cloudinit/sources/DataSourceEc2.py and cloudinit/sources/helpers/aliyun.py to increment the metric values of new interfaces by 100.
for nic_idx, nic_name in enumerate(orderd_nic_name_list):
nic_mac = nic_name_2_mac_map[nic_name]
nic_metadata = macs_metadata.get(nic_mac)
dhcp_override = {"route-metric": (nic_idx + 1) * 100}
dev_config = {
"dhcp4": True,
"dhcp4-overrides": dhcp_override,
"dhcp6": False,
"match": {"macaddress": nic_mac.lower()},
"set-name": nic_name,
}
if nic_metadata.get("ipv6s"): # Any IPv6 addresses configured
dev_config["dhcp6"] = True
dev_config["dhcp6-overrides"] = dhcp_override
But in practice, I have found that this implementation does not seem to take effect.

My initial idea is to add the following code to cloudinit/net/network_manager.py. Preliminary testing suggests this approach is feasible:
@@ -434,6 +455,9 @@ class NMConnection:
self.config[family]["gateway"] = subnet["gateway"]
for route in subnet["routes"]:
self._add_route(route)
+ # metric may apply to both dhcp and static config
+ if "metric" in subnet:
+ self.config[family]["route-metric"] = str(subnet["metric"])
# Add subnet-level DNS
if "dns_nameservers" in subnet:
After testing, it does work (a system reboot is required).
Could you please help evaluate this further? Thanks!
- the similar patch works in sysconfig: a399f4b
On any OS using NetworkManager, such as RHEL 10, when a new network interface is added without a static network configuration, the default metric values assigned are typically 101 and 102 and so on.
However, some cloud vendors (e.g., ECS, EC2) have implemented logic in cloudinit/sources/DataSourceEc2.py and cloudinit/sources/helpers/aliyun.py to increment the metric values of new interfaces by 100.
But in practice, I have found that this implementation does not seem to take effect.

My initial idea is to add the following code to cloudinit/net/network_manager.py. Preliminary testing suggests this approach is feasible:
After testing, it does work (a system reboot is required).
Could you please help evaluate this further? Thanks!