
A fresh CentOS install usually gets its address from DHCP. Servers that host websites, mail or game services need a fixed (static) IP instead, so that DNS records and firewall rules keep pointing to the right place. This guide shows three ways to set or change the IP address on CentOS 7: editing the ifcfg file, using the nmtui menu, and using nmcli. It also explains what changed on CentOS Stream 9 and 10, Rocky Linux and AlmaLinux 9 and later, which store network settings differently.
Quick answerWith NetworkManager (CentOS 7 and every newer release), set a static IP in one go:
sudo nmcli con mod eth0 ipv4.method manual ipv4.addresses 203.0.113.10/24 ipv4.gateway 203.0.113.1 ipv4.dns "1.1.1.1 8.8.8.8"
sudo nmcli con up eth0
Replace eth0 with your connection name and use the addresses from your provider.
On a VPS, check with your provider first. The IP addresses you can use are assigned to your server. Setting an address that is not routed to your VPS will cut it off the network, and you will need the provider's console (VNC) to fix it.
Step 1: Find your interface and current IP
ip a
ip route
nmcli con show
ip a lists interfaces (for example eth0, ens3 or enp1s0) with their addresses. ip route shows the default gateway. nmcli con show shows the NetworkManager connection name for each interface, which is what nmcli and nmtui use.
Note the interface name, the new IP and prefix (for example /24 for a 255.255.255.0 netmask), the gateway, and the DNS servers you want.
Method 1: Edit the ifcfg file (CentOS 7)
On CentOS 7, each interface has a file in /etc/sysconfig/network-scripts/. Back it up, then edit it:
sudo cp /etc/sysconfig/network-scripts/ifcfg-eth0 /root/ifcfg-eth0.bak
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
A static configuration looks like this:
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=203.0.113.10
PREFIX=24
GATEWAY=203.0.113.1
DNS1=1.1.1.1
DNS2=8.8.8.8
Change BOOTPROTO=dhcp to none (or static) and keep ONBOOT=yes so the interface comes up at boot. Leave any UUID or HWADDR lines that are already there. Then apply the change:
sudo nmcli con reload
sudo nmcli con up eth0
On a minimal CentOS 7 without NetworkManager, use sudo systemctl restart network instead.
Method 2: Use nmtui (text menu)
nmtui is a simple menu that works over SSH. If it is missing, install it with sudo yum install NetworkManager-tui (or dnf on newer releases).
- Run
sudo nmtuiand choose Edit a connection. - Select your connection and press Enter.
- Set IPv4 CONFIGURATION to Manual, then choose Show.
- Add the address with its prefix (for example
203.0.113.10/24), the gateway and the DNS servers. - Make sure Automatically connect is ticked, then choose OK.
- Back in the main menu, choose Activate a connection, then deactivate and reactivate the connection to apply it.
If you are connected over SSH to the address you are changing, your session will drop at this point. Reconnect to the new IP.
Method 3: Use nmcli (best for scripts)
nmcli makes the same change in a single command, which is handy for automation:
sudo nmcli con mod eth0 ipv4.method manual \
ipv4.addresses 203.0.113.10/24 \
ipv4.gateway 203.0.113.1 \
ipv4.dns "1.1.1.1 8.8.8.8"
sudo nmcli con up eth0
To add a second IP address to the same interface instead of replacing the first one, use +ipv4.addresses:
sudo nmcli con mod eth0 +ipv4.addresses 203.0.113.11/24
sudo nmcli con up eth0
To switch back to DHCP:
sudo nmcli con mod eth0 ipv4.method auto ipv4.addresses "" ipv4.gateway ""
sudo nmcli con up eth0
Step 2: Check the new address
ip a show eth0
ip route
ping -c 3 1.1.1.1
ping -c 3 google.com
The first ping tests routing; the second also tests DNS. Reboot once (sudo reboot) to confirm the address survives a restart.
CentOS Stream 9/10, Rocky and AlmaLinux 9+
From the 9 series onward, NetworkManager stores connections as keyfiles in /etc/NetworkManager/system-connections/ instead of ifcfg files. The legacy network-scripts service is gone. nmtui and nmcli (Methods 2 and 3) work exactly the same, so use those. If you edit a keyfile by hand, run sudo nmcli con reload afterwards.
CentOS Linux 7 itself reached end of life in June 2024. The steps above still apply to it, but new servers should run a supported release.
Frequently asked questions
How do I change the IP address in CentOS 7 from the command line?
Use nmcli con mod <connection> ipv4.method manual ipv4.addresses IP/prefix ipv4.gateway GW and then nmcli con up <connection>. Alternatively, edit /etc/sysconfig/network-scripts/ifcfg-<interface> and reload the connection.
Do I need to reboot after changing the IP?
No. Bringing the connection up again applies the change immediately. A reboot is only a good way to confirm the setting is permanent.
Why did I lose SSH access after changing the IP?
Your SSH session was connected to the old address. Reconnect to the new one. If that fails, the address, prefix or gateway is wrong; log in through your provider's VNC console and restore the backup file.
Where do I set DNS servers on CentOS?
Set them on the connection (DNS1/DNS2 in the ifcfg file or ipv4.dns with nmcli). NetworkManager then writes /etc/resolv.conf for you, so do not edit that file directly.


