Configure a static IP address in CentOS 7

To view the IP address on a Linux machine, we can use either of the following commands from a terminal. NOTE: The first command is deprecated and should not be used if possible.

ip a

Option 1: Edit configuration files

The first option is to edit the files under /etc/sysconfig/network-scripts. These files exist for each of the interfaces (physical or virtual) on the computer. To edit the file type the following, substitute the correct interface name for your system:

vi /etc/sysconfig/network-scripts/ifcfg-Wired_connection_1

In this file, make sure the line BOOTPROTO=none. Configure the the IPADDR, PREFIX, GATEWAY, and DNS1 lines with the appropriate address details. When finished editing, save and exit the file. Once the configuration has been saved, we need to restart the Network Manager service to reload the configurations. At the terminal, type:

sudo systemctl restart NetworkManager
sudo nmcli device connect eth0

Option 2: Network manager Command Line Interface (nmcli)

The second option is to use the Network Manager command line interface (nmcli) to configure the IP address. The first step is to list the available interfaces and identify the interface to configure

nmcli connection show

Review the list of interfaces and identify the interface to update. Then type the following, substituting the correct interface name)

nmcli connection edit Wired\ connection\ 1

Inside the nmcli, you can type ? or help to get context sensitive help. To start, you can view the interface configuration by typing print.

To set the IP address, DNS, and gateway, type the following

set ipv4.address
set ipv4.dns,
set ipv4.gateway
save persistent

When finished with nmcli, type the following commands to restart and reload the network configuration

sudo systemctl restart NetworkManager
sudo nmcli device connect eth0

Leave a Reply