Configure Static IP Addresses Using NetworkManager on RHEL/CentOS
Configure a persistent static IP on RHEL or CentOS with `nmcli`, including gateway, DNS, activation, and rollback checks.
Configure Static IP Addresses Using NetworkManager on RHEL/CentOS
Networking is one of the first things you need to get right on a server. On RHEL and CentOS, NetworkManager can configure a persistent static IP address without hand-editing interface files, which is safer on newer Enterprise Linux releases.
This guide walks through the nmcli commands to set an address, gateway, and DNS servers, activate the profile, and verify that the change survives a reboot.
Understanding NetworkManager and nmcli
Before diving into the configuration steps, let's clarify the core components we'll be using:
What is NetworkManager?
NetworkManager is a daemon that handles network settings, making it easier to manage network connections. It can manage various connection types, including Ethernet, Wi-Fi, mobile broadband, and VPNs. It automatically detects and configures network devices and attempts to maintain an active network connection, switching between available connections as needed. For servers, it ensures that your network interfaces are configured correctly and persist across reboots.
What is nmcli?
nmcli is the command-line client for NetworkManager. It allows you to control NetworkManager and configure network connections from the terminal. It's particularly useful in headless server environments where a graphical interface isn't available. nmcli commands are intuitive and provide a robust way to interact with NetworkManager's full range of features, from displaying network device status to creating and modifying complex connection profiles.
Prerequisites
To follow this guide, ensure you have:
- Root Privileges: You'll need
sudoaccess or to be logged in as the root user to make network configuration changes. - System Information: The static IP address, subnet mask (or CIDR notation), default gateway, and DNS server IP addresses you intend to use.
- Identified Network Interface: Knowledge of the network interface name (e.g.,
enp0s3,eth0) you wish to configure. We'll cover how to find this.
Step-by-Step Configuration of a Static IP Address
Here’s how to configure a static IP address using nmcli.
Step 1: Identify Your Network Interface
First, you need to know the name of the network interface you want to configure. You can list all active network devices and their statuses using nmcli device or nmcli device status:
nmcli device status
Example Output:
DEVICE TYPE STATE CONNECTION
enp0s3 ethernet connected System enp0s3
lo loopback unmanaged --
In this example, enp0s3 is our Ethernet interface. Make a note of your interface name.
Step 2: Create a New Network Connection Profile
It's generally a good practice to create a new connection profile for your static IP configuration, rather than modifying an existing DHCP-managed one directly, especially if you might want to revert easily. A connection profile stores all the settings for a specific network connection.
To create a new Ethernet connection profile, use the nmcli connection add command. You'll specify the connection type, a descriptive connection name (con-name), and the interface name (ifname).
sudo nmcli connection add type ethernet con-name my-static-eth ifname enp0s3
type ethernet: Specifies an Ethernet connection.con-name my-static-eth: Assigns a name to this new connection profile. Choose something descriptive.ifname enp0s3: Links this connection profile to the physical network interfaceenp0s3.
Alternative: Modifying an Existing Profile
If you prefer to modify an existing connection (e.g., System enp0s3 from the nmcli device status output), you would use nmcli connection modify with its existing con-name.
sudo nmcli connection modify "System enp0s3" # replace with your connection name
Note: For simplicity and clarity, this guide will assume you've created a new connection profile named my-static-eth.
Step 3: Configure IPv4 Settings (IP, Gateway, DNS)
Now, let's configure the static IP address, gateway, and DNS servers for our my-static-eth connection profile. It's crucial to set the IPv4 method to manual to disable DHCP.
Setting the IP Address and Gateway
Use the ipv4.addresses and ipv4.gateway properties. The IP address should be in CIDR notation (e.g., 192.168.1.100/24).
sudo nmcli connection modify my-static-eth ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1
ipv4.addresses 192.168.1.100/24: Sets the static IP address to192.168.1.100with a 24-bit subnet mask (255.255.255.0).ipv4.gateway 192.168.1.1: Sets the default gateway for the network.
Setting DNS Servers
Specify one or more DNS server IP addresses. Separate multiple addresses with commas.
sudo nmcli connection modify my-static-eth ipv4.dns "1.1.1.1,8.8.8.8"
If your organization runs internal DNS, use those resolver IPs instead. For example, a private server might use 192.168.1.10 and 192.168.1.11.
Disable DHCP for IPv4
Set the IPv4 method to manual; otherwise NetworkManager may still try DHCP for this profile.
sudo nmcli connection modify my-static-eth ipv4.method manual
If the server should not use IPv6 on this connection, disable it explicitly:
sudo nmcli connection modify my-static-eth ipv6.method disabled
Only do this when your network does not rely on IPv6.
Step 4: Activate the Static Profile
Bring the new connection up. If you are working over SSH, run this from console access or schedule a rollback before changing the active interface.
sudo nmcli connection up my-static-eth
If an older DHCP profile is still active on the same interface, bring it down after confirming the static profile works:
sudo nmcli connection down "System enp0s3"
Replace "System enp0s3" with the actual old connection name from nmcli connection show.
Step 5: Verify the Address, Route, and DNS
Check the active connection:
nmcli connection show --active
Confirm the IP address:
ip addr show enp0s3
Confirm the default route:
ip route
You should see a route similar to:
default via 192.168.1.1 dev enp0s3
Check DNS settings managed by NetworkManager:
nmcli device show enp0s3 | grep DNS
Then test real connectivity:
ping -c 3 192.168.1.1
ping -c 3 1.1.1.1
getent hosts example.com
The first command checks the gateway, the second checks outbound IP connectivity, and the third checks DNS resolution.
Safer Remote Changes
Static IP changes can disconnect your SSH session if you mistype the address or gateway. Before applying the profile remotely, keep a second session open and consider scheduling a rollback:
sudo shutdown -r +5 "Network rollback reboot"
If the new static IP works, cancel the reboot:
sudo shutdown -c
On systems where a reboot is not acceptable, create a temporary at job or use out-of-band console access so you can recover if the route is wrong.
Takeaway
Use nmcli connection profiles for persistent RHEL and CentOS static IP configuration. The minimum working set is ipv4.addresses, ipv4.gateway, ipv4.dns, and ipv4.method manual; the safest workflow is to activate the profile, verify route and DNS behavior, then remove or disable the old DHCP profile only after the server is reachable at its new address.