How To Configure a Static IP Address on CentOS 7/8
Introduction
This guide details howto configure a static IP on CentOS 7 or 8 on an operating system installed without a GUI. There are a number of options/methods available to do this outlined below.
Method 1 – Using Network Configuration File
1) First determinte the name of your network adapter by running the following:
sudo lshw -class network -short
2) Using your favourite text editor open the configuration file for your network adapter, replacing X with the name of your network adapter determined in the previous step.
nano /etc/sysconfig/network-scripts/ifcfg-ethX
3) Configure the variables in the file for your conneciton:
DEVICE=eth0 NAME=eth0 Type=Ethernet IPADDR=192.168.1.20 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 DNS1=192.168.1.1 DNS2=8.8.4.4 ONBOOT=yes BOOTPROTO=none USERCTL=no PEERDNS=yes
4) The most common configuration parameters with explanations are:
BOOTPROTO= none, bootp or dhcp
IPADDR=
DEVICE=<name> –> where <name> is the name of the physical device.
DNS{1,2}=<address> –> where <address> is a name server address to be placed in /etc/resolv.conf
GATEWAY=
MACADDR=<MAC-address> –> Where <MAC-address> is the hardware address of the Ethernet device in the form AA:BB:CC:DD:EE:F
NETMASK=
ONBOOT=<answer> –> Where <answer> is one of the following:
yes — This device should be activated at boot-time.
no — This device should not be activated at boot-time.
PEERDNS=
yes – Modify /etc/resolv.conf if the DNS directive is set. If using DHCP, then yes is the default.
no – Do not modify /etc/resolv.conf.
USERCTL=
yes – Non-root users are allowed to control this device.
no – Non-root users are not allowed to control this device.
4) If you have NetworkManager service running, you’ll need to instruct the network service that network manager doesn’t manage this interface (eth0). This is done by adding the line:
NM_CONTROLLED=no
5) Then you can stop NetworkManager service. For CentOS 7/8, this can be done using:
sudo systemctl stop NetworkManager
6) After saving the changes, the shut down the interface and bring it back:
sudo ifdown eth0 && sudo ifup ethX
7) Then you can stop NetworkManager service. For CentOS 7/8, this can be done using:
sudo systemctl stop NetworkManager
8) Check the current adapter settings:
ip addr show
Method 2 – Using ip and ifconfig commands
The ifconfig command is now depreciated in favour of ip tool however is still part of net-tools package and can be installded with:
yum -y install iproute
For clarity in the steps below both iptool and ifconfig commands are shown:
Show Adapters
To show all ip address related infoprmation is ip/config these commands are used:
ifconfig ip addr
Bring Up/Down an Interface
To show all ip address related infoprmation is ip/config these commands are used:
sudo ifconfig ethX down sudo ip link set dev ethX down
Setting a static IP
Static IP setting can be done using ip or ifconfig. But note that changes made with these commands are not persistent against reboots:
sudo ifconfig ethX 192.168.1.100 netmask 255.255.255.0 sudo ip addr add 192.168.1.100/24 dev ethX
Removing a static IP
Clearing IP information can be done as follows:
sudo ifconfig ethX del 192.168.1.100 sudo ip addr del 192.168.1.100/24 dev ethX
Add default route via gateway IP
A default route can be set using ip and ifconfig commands for destinations without static routes defined.
sudo route add default gw 192.168.1.1 sudo ip route add default via 192.168.1.1
Method 3 – Using Network Manager (nmcli)
In RHEL and CentOS 7 and 8 the networking service is managed by the NetworkManager daemon and it is used to dynamically configure and control network devices and keep connections up and active when they are available.
Howto remove NetworkManager
In case you don’t want to remove network manager you can disable and remove it as follows:
systemctl disable NetworkManager systemctl remove NetworkManager
Incase you do wish to use NetworkManager then a reference of commands can be found below:
Howto start NetworkManager
You can check Network Manager is running and started as follows:
systemctl status NetworkManager sudo systemctl start NetworkManager
Bring Up interfaces using NetworkManager
You can bring network interfaces up and down using network manager as follows:
sudo nmcli connection down ethX sudo nmcli connection up ethX
Configure a static IP using NetworkManager
1) List current connections
nmcli con show
2) Delete connections entering UUID or network name from Step 1
nmcli con delete eth0
3) Create a network with name ethX
nmcli con add type ethernet ifname ethX con-name ethx \ autoconnect yes ip4 192.168.1.100 gw4 192.168.1.1
4) Configure DNS and make network configurations always be manual for this network interface.
nmcli con mod ethX ipv4.method manual nmcli con mod ethX ipv4.dns "8.8.8.8 8.8.4.4"
5) Restart the network:
sudo nmcli con down ethX && sudo nmcli con up eth0
6) View the connection ethX
nmcli con show ethX