How to configure a static CentOS 8 network address from the command line

Every CentOS admin should know how to configure a static IP address using nothing but the terminal.

How to configure a static CentOS 8 network address from the command line Every CentOS admin should know how to configure a static IP address using nothing but the terminal.

CentOS 8 is here and it's chock full of new features and improvements. One feature that has not changed all that much is the configuration of a static network address. Although Ubuntu migrated to the much more user-friendly netplan tool, CentOS is still using the old-school method. 

If you don't happen to know how to manually configure a network address, switching it from DHCP to static, I'm going to show you how.

SEE: Hiring kit: Network administrator (TechRepublic Premium)

What You'll Need

All you'll need to make this work is the following:

  • A running instance of CentOS 8
  • An available IP address
  • A user with sudo privileges

How to locate the device name

The first thing we must do is locate the device name to be configured. Do this with the command:

ip a

This should reveal the name of your network interface (Figure A).

Figure A

centosipa.jpg

My interface name is enp0s3.

How to backup your configuration file

Next, we're going to backup the original configuration file with the command:

sudo mv /etc/sysconfig/network-scripts/ifcfg-NAME /etc/sysconfig/network-scripts/ifcfg-NAME.bak

Where NAME is the name of your network interface.

How to create a new configuration

Now we create a new configuration file with the following command:

sudo nano /etc/sysconfig/network-scripts/ifcfg-NAME

Where NAME is the name of your network interface.

In that file, paste the following:

TYPE="Ethernet"
BOOTPROTO="none"
NAME="NAME"
IPADDR="IP_ADDRESS"
NETMASK="255.255.255.0"
GATEWY=GATEWAY
DEVICE="NAME"
ONBOOT="yes"
PEERDNS=no

Where:

  • NAME is the name of the network interface
  • IP_ADDRESS is the static IP address you want to apply to the interface
  • GATEWAY is the gateway address for your network

Save and close the file.

Restart the network manager with the command:

sudo systemctl restart NetworkManager

Your static IP address has been applied

How to configure DNS servers

Because we're using a static IP address, we might also want to use static addresses for DNS resolution (which is why we added PEERDNS=no to the configuration file). To configure DNS servers, issue the command:

sudo nano /etc/resolv.conf

In that file, add your static DNS addresses below the last line like so:

nameserver 8.8.4.4,8.8.8.8

Save and close the file.

And that's all there is to configuring a static IP address in CentOS 8. 

Also see

centoshero.jpg

Image: CentOS