nmcli Guide: Static IP with NetworkManager Connections - 夜莺博客

nmcli Guide: Static IP with NetworkManager Connections

NetworkManager is the default network stack on RHEL, Fedora, Rocky, AlmaLinux and most desktop distributions, and nmcli is its scriptable command-line front end. Editing /etc/sysconfig/network-scripts by hand is obsolete - the supported workflow is one nmcli connection modify command followed by nmcli connection up, and the profile is written to disk automatically so the change survives reboot. This guide shows how to configure a static IP, gateway, DNS and extra routes with nmcli, following the Red Hat documentation.

Step 1: Find the Existing Connection Profile

nmcli connection show

NetworkManager creates one profile per NIC (for example Wired connection 1 on enp1s0) that uses DHCP by default. You can modify that profile, but it is cleaner to create a dedicated one:

nmcli connection add con-name Internal-LAN ifname enp1s0 type ethernet

Step 2: Switch the Profile to a Static IPv4 Address

nmcli connection modify Internal-LAN   ipv4.method manual   ipv4.addresses 192.0.2.1/24   ipv4.gateway 192.0.2.254   ipv4.dns "192.0.2.200 8.8.8.8"   ipv4.dns-search example.com

ipv4.method manual is what disables DHCP; leaving it at auto while setting an address is the most common nmcli mistake. DNS servers are space-separated - quote the whole list. For IPv6 use the parallel ipv6.method manual ipv6.addresses 2001:db8::1/64 properties.

Step 3: Activate and Verify

nmcli connection up Internal-LAN
nmcli connection show Internal-LAN | grep ipv4
ip -4 addr show enp1s0
ip route show

Useful Variations

# add an extra IP address to the profile
nmcli connection modify Internal-LAN +ipv4.addresses 192.0.2.101/24

# add a static route
nmcli connection modify Internal-LAN +ipv4.routes "10.0.0.0/8 192.0.2.254"

# switch back to DHCP
nmcli connection modify Internal-LAN ipv4.method auto ipv4.addresses "" ipv4.gateway ""

# make sure it comes up at boot
nmcli connection modify Internal-LAN connection.autoconnect yes

# reload profiles after manual file edits
nmcli connection reload

If you edit files in /etc/NetworkManager/system-connections/ directly, run nmcli connection reload afterwards. For temporary, non-persistent changes use nmcli device modify enp1s0 ipv4.address 192.168.1.23/24.

Related Guides on This Site

Compare with the file-based alternative in systemd-networkd configuration, bond the uplinks first with Linux bonding and LACP, then verify neighbors via ip neigh ARP table management.

原文链接:https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/configuring_and_managing_networking/configuring-an-ethernet-connection_configuring-and-managing-networking