How to configure a static IP address on CentOS

Welcome to the tutorial on how to configure a static IP address on CentOS. Have you been trying to find an easy way to set a static IP address on CentOS? If so, then this guide is for you. When working on a server, no matter what kind of server you are managing, you must do a static IP address configuration.

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

  1. Editing network configuration files under guidance / etc / sysconfig / network-scripts / table of Contents.
  2. Using nmcli network configuration command line tool
  3. Use t Network Configuration Tool
  4. Use the following command for static IP address configuration ip with ifconfig command.

We will cover all of these methods in the next section of this guide. Please note ip with ifconfig This method does not prevent restart. Use only if you need a test setup to submit it to a file.

Method 1: Configure a 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, Its 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 start-up protocol should not be used.Bout -BOOTP protocol should be used.dhcp -The DHCP protocol should be used.

IPADDR =

-> Where

Is the 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 directives are set, modify /etc/resolv.conf. If DHCP is used, 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 rows

NM_CONTROLLED=no

You can then stop the NetworkManager service. For CentOS 7, this can be done using:

$ sudo systemctl stop NetworkManager

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

# ifdown eth0 && 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, install it using the following command:

# yum -y install net-tools

Then use to check your IP address information ifconfig command.

# ifconfig -a

Method 2: Use nmcli to configure a static IP

NetworkManager is a daemon that sits on top of libudev and other Linux kernel interfaces and provides a high-level interface for configuring network interfaces. Nmcli, on the other hand, is a command line tool for controlling NetworkManager and reporting network status. Can be used as an alternative Applets Or other graphics client. 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 basic configurations are considered here. Before attempting to use Configure Network, make sure the NetworkManager service is running. Antarctica tool.

# systemctl status NetworkManager

If the service is not running, start it:

# systemctl start NetworkManager

To use nmcli to control the state of a network interface, pass down before the NIC name. This will shut down the interface or show closed interfaces.

# nmcli connection down eth0
# nmcli connection up eth0

Create network with nmcli and configure 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 the current connection:

# nmcli con show

Before recreating, delete the connection with the same name. Network name or network UUID displayed using 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 for 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:

# nmcli con down eth0 && nmcli con up eth0

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

# nmcli con show eth0

To delete a connection, use:

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

Method 3: Use the ip and ifconfig commands for static IP address configuration.

There is a guide on how to use the ip and ifconfig commands to configure a static IP address. This command is located at-> Use ip and ifconfig to configure a Linux network.

wrap up.

We have introduced how to use the manual modification of the network configuration file and the complete steps to configure a static IP address on CentOS using the nmcli command line tool. Other tools are available, such as t Although deprecated, it is not recommended. Hope this helps, and thank you for watching.

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

Sidebar