How to configure a static IP address on CentOS 8/7

The
You can download this article in PDF format to support us through the following link.

Download the guide as a PDF

turn off
The

The
The

Welcome to the tutorial on how to configure a static IP address on CentOS 8/7 Linux. Have you been trying to find an easy way to set a static IP address on CentOS 8.7? If yes, then this guide is for you.

When working on the server, no matter which server you want to manage, you must do static IP address configuration.

To configure a static IP address on CentOS, you can use any of the following methods:

  1. Guide to edit network configuration files / etc / sysconfig / network-scripts / table of Contents.
  2. Use nmcli network configuration command line tool
  3. To use t Network configuration tool
  4. Use the following command to configure static IP address ip with ifconfig command.

We will introduce all these methods in the next part of this guide. caution ip with ifconfig This method cannot prevent a restart. Use only if you need to test the settings to submit them to a file.

Method 1: Configure the static IP address by editing the network configuration file

Suppose you have a server with a network interface named eth0 And want to set a static network configuration for it, a file will be created / etc / sysconfig / network-scripts / ifcfg-eth0, The basic content is similar to the following:

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

The most common configuration parameters and their descriptions are as follows:

BOOTPROTO = –> Where Is one of the following:No — The startup protocol should not be used.Bute — The BOOTP protocol should be used.dhcp — The DHCP protocol should be used.

IPADDR =

–> Where

Is an IP address.DEVICE = –> Where Is the name of the physical device.DNS {1,2} =

–> Where

Is the name server address to be placed in /etc/resolv.confGateway =

–> Where

Is the IP address of the network routerMACADDR = –> Where Is the hardware address of the Ethernet device in the format AA: BB: CC: DD: EE: FNETMASK = –> Where Is the netmask value.Boot = –> Where Is one of the following:Yes —The device should be activated at boot time.No —The device should not be activated at startup.PEERDNS = –> Where Is one of the following:Yes -If DNS commands are set, please modify /etc/resolv.conf. If using DHCP, the default is yes.No -Do not modify /etc/resolv.conf.USERCTL = –> Where Is one of the following: Yes – Allow non-root users to control this device.No – Non-root users are not allowed to control this device.

If you are running the NetworkManager service, you need to instruct the network service network administrator not to manage this interface (eth0). This is done by adding lines;

NM_CONTROLLED=no

Then, you can stop the NetworkManager service. For CentOS 7/8, you can use the following methods to complete:

sudo systemctl stop NetworkManager

After saving the changes, close the interface and restore it:

sudo ifdown eth0 && sudo ifup eth0

Use the ip command to check the current configuration:

$ ip addr show

If you want to use the ifconfig command but it is not installed, use the following command to install:

sudo yum -y install net-tools

Then use to check your IP address information ifconfig command.

$  ifconfig -a

Method 2: Use nmcli to configure static IP

NetworkManager is a daemon that sits on top of libudev and other Linux kernel interfaces and provides high-level interfaces for configuring network interfaces.

On the other hand, nmcli is a command-line tool for controlling NetworkManager and reporting network status. It can replace nm-applet or other graphics clients. nmcli can create, display, edit, delete, activate and deactivate network connections, and control and display network device status.

nmcli can complete many network configurations, only the basic configuration is considered here. Before attempting to configure the network using the nmcli tool, make sure that the NetworkManager service is running.

$ systemctl status NetworkManager

If the service is not running, start it:

$ sudo systemctl start NetworkManager

To use nmcli to control the status of the network interface, pass down | in front of the NIC name. This will close the interface or display the closed interface.

sudo nmcli connection down eth0
sudo nmcli connection up eth0

Use nmcli to create a network and configure a static IP address

This example will show you how to create a new network called eth0 And use nmcli to configure it as an IPv4 address.

Show current connection:

$ nmcli con show

Before recreating, delete the connection with the same name. Use the network name or network UUID shown in the previous command:

$ nmcli con delete eth0

Create a network with a name eth0

$ nmcli con add type ethernet ifname eth0 con-name eth0 
  autoconnect yes ip4 192.168.1.10 gw4 192.168.1.1

Connection 'eth0' (804ce9b1-c5e1-42ff-b1a9-7a92e08e44a7) successfully added.

Configure DNS and make the network configuration of this network interface always manual.

nmcli con mod eth0 ipv4.method manual 
nmcli con mod eth0 ipv4.dns "8.8.8.8 8.8.4.4"

Restart the network:

sudo nmcli con down eth0 && sudo nmcli con up eth0

Use the show command and nmcli to view the network configuration:

$ nmcli con show eth0

To delete the connection, use:

$ nmcli con del eth0
Connection 'eth0' (804ce9b1-c5e1-42ff-b1a9-7a92e08e44a7) successfully deleted.

Method 3: Use the ip and ifconfig commands to configure static IP addresses.

There are existing guidelines on how to use the ip and ifconfig commands to configure static IP addresses:

Use ip and ifconfig to configure the Linux network.

wrap up.

We have already introduced how to manually modify the network configuration file and use the nmcli command line tool to configure the static IP address on CentOS. Other tools are available, for example t Although deprecated, it is not recommended. Hope this helps you, and thank you for watching.

If you are a newly installed CentOS server, I recommend reading: The main things to do after installing a new CentOS 7

The
You can download this article in PDF format to support us through the following link.

Download the guide as a PDF

turn off
The

The
The

Sidebar