AlmaLinux 9: pre-generate a static IP keyfile with nmcli --offline
Test scope
Section titled “Test scope”Executed in an almalinux:9 container (AlmaLinux 9.8, NetworkManager 1.54.3-5.el9_8): nmcli --version, nmcli general status, and nmcli --offline connection add … for a static and a DHCP profile. Log: plans/content-expansion/evidence/logs/alma-network-basics.log.
nmcli connection up, nmcli device reapply and autoconnect after reboot were not tested here (the container has no NetworkManager daemon) and are marked E3.
Why generate offline
Section titled “Why generate offline”nmcli --versionnmcli general statusnmcli tool, version 1.54.3-5.el9_8Error: Could not create NMClient object: Could not connect: No such file or directory.Regular nmcli subcommands talk to the NetworkManager daemon over D-Bus, which is unavailable in containers, rescue environments, or hosts where NM is not running yet.
nmcli --offline needs no daemon: it builds the profile in memory and prints the keyfile, which is exactly what you want to prepare a configuration elsewhere and carry it to the target host.
Generate the static-IP keyfile
Section titled “Generate the static-IP keyfile”nmcli --offline connection add type ethernet ifname enp1s0 con-name static-enp1s0 \ ipv4.method manual ipv4.addresses 192.0.2.10/24 \ ipv4.gateway 192.0.2.1 ipv4.dns 192.0.2.53[connection]id=static-enp1s0uuid=bd58671c-def9-4f00-b5fe-4cc6c41d09fctype=ethernetinterface-name=enp1s0
[ethernet]
[ipv4]address1=192.0.2.10/24dns=192.0.2.53;gateway=192.0.2.1method=manual
[ipv6]addr-gen-mode=defaultmethod=autoField notes:
| Field | Meaning |
|---|---|
ipv4.method=manual | Static address (auto means DHCP) |
ipv4.address1 | Address/prefix; additional addresses become address2=… |
ipv4.gateway | Default route |
ipv4.dns | DNS servers, semicolon separated |
Save the output directly:
nmcli --offline connection add type ethernet ifname enp1s0 con-name static-enp1s0 \ ipv4.method manual ipv4.addresses 192.0.2.10/24 ipv4.gateway 192.0.2.1 ipv4.dns 192.0.2.53 \ | sudo tee /etc/NetworkManager/system-connections/static-enp1s0.nmconnectionsudo chmod 600 /etc/NetworkManager/system-connections/static-enp1s0.nmconnectionThe mode must be 600: NetworkManager ignores a keyfile with wider permissions.
Enable it (needs a real system)
Section titled “Enable it (needs a real system)”sudo nmcli connection reloadsudo nmcli connection up static-enp1s0nmcli -p connection show static-enp1s0reload makes NM re-read the directory, up actually applies the profile to the interface. Changing networking can drop your session — have console access ready.
Verify
Section titled “Verify”ip -4 addr show enp1s0ip route show defaultcat /etc/resolv.confping -c 2 192.0.2.1Address present, default route pointing at the gateway, a nameserver in resolv.conf, and the gateway answering ping — all four before you call it done.
Common traps
Section titled “Common traps”- Keyfile not mode 600: NM silently ignores the file and it looks like “the configuration did nothing”;
reloadwithoutup: the profile is loaded but never applied to the device;- Hand-written ifcfg files: EL9 defaults to the keyfile backend, so
/etc/sysconfig/network-scripts/ifcfg-*may have no effect at all.
Practice: pre-generate a profile for another host
Section titled “Practice: pre-generate a profile for another host”nmcli --offline connection add type ethernet ifname enp1s0 con-name app01-static \ ipv4.method manual ipv4.addresses 10.0.0.10/24 ipv4.gateway 10.0.0.1 ipv4.dns 10.0.0.2 \ | sudo tee /etc/NetworkManager/system-connections/app01-static.nmconnectionsudo chmod 600 /etc/NetworkManager/system-connections/app01-static.nmconnectionsudo nmcli connection reloadsudo nmcli connection up app01-staticChecks:
nmcli -f ipv4.method,ipv4.addresses connection show app01-static→manualand the target addressip -4 addr show enp1s0→ the address is activeip route show default→ the gateway is correct- After reboot,
nmcli connection show --active→ the connection is still active
常见问题
Can the keyfile from `nmcli --offline` be used as-is?
Yes, it is a standard keyfile. Save it under /etc/NetworkManager/system-connections/, chmod 600, then nmcli connection reload and up. Make sure the uuid and con-name are unique on the target host.
Why does nmcli report Could not create NMClient object?
There is no running NetworkManager daemon (container, rescue mode, or the service is stopped). Regular subcommands need D-Bus; use nmcli --offline when you only need to generate configuration.
Can a keyfile carry several addresses and DNS servers?
Yes. Addresses become address1=, address2=… and DNS servers are semicolon separated (dns=1.1.1.1;8.8.8.8;). Read the result back with nmcli --offline connection show to confirm how it was parsed.
Next steps
Section titled “Next steps”- Opening ports: firewall-cmd rules
- Post-install checks: 7 checks after install
- Auditing sources: Package source audit