How To Configure IP Address in CentOS 7 / RHEL 7 & CentOS 6 / RHEL 6

0

ADVERTISEMENT

Configure IP Address in CentOS 7
Configure IP Address in CentOS 7

Just after the installation of operating systems, you must configure the network to access your system from outside. This guide helps you to Configure IP Address in CentOS 7 / RHEL 7 & CentOS 6 / RHEL 6.

Here I will show how to configure Static IP addresses for your machine and also configure the network interface to get an IP address from the DHCP server.

Prerequisites

Let’s check the available interfaces on our system. Use the ifconfig command to list interfaces.

ifconfig

Output:

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 2406:7400:bf:7cdd:e387:a515:14c9:2272  prefixlen 64  scopeid 0x0
        inet6 2406:7400:bf:e32d:8706:45ed:f7dd:cda8  prefixlen 64  scopeid 0x0
        inet6 fe80::9cf2:e3d2:e192:8273  prefixlen 64  scopeid 0x20
        ether 08:00:27:13:2c:70  txqueuelen 1000  (Ethernet)
        RX packets 663  bytes 62963 (61.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 754  bytes 80123 (78.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 64  bytes 5632 (5.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64  bytes 5632 (5.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

From the above output, you can see my system has two interfaces namely lo and ifcfg-enp0s3. Depends on hardware, the name of the interface will change.

To set an IP address to an interface, go to /etc/sysconfig/network-scripts/ directory, there you will find a file ifcfg-<interfacename>.

cd /etc/sysconfig/network-scripts/ ; ls -al ifcfg-*

Output:

-rw-r--r--. 1 root root 376 Nov 24 03:05 ifcfg-enp0s3
-rw-r--r--. 1 root root 254 Mar 29  2019 ifcfg-lo

As per the above output, my system has a file ifcfg-enp0s3 since CentOS 7 / RHEL 7 uses consistent network interface naming.

In CentOS 6 / RHEL 6, the network interfaces are named eth0, eth1 and so on. So, the interface file name will be ifcfg-eth0.

Static IP Address

In this mode, we will manually assign an IP address to machines.

cd /etc/sysconfig/network-scripts/

Edit the ifcfg-<interfacename> file.

vi ifcfg-enp0s3

FROM

HWADDR=00:0C:29:76:96:A8
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=e5a5d8e9-b5d4-4b5e-bd1e-6ebcd0144dfa
ONBOOT=no

TO

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none" # Static IP
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="enp0s3"
DEVICE="enp0s3"
ONBOOT="yes" # Enable Network Interaface on boot
IPADDR="192.168.0.10" # IP Address
NETMASK="255.255.255.0" # NetMask
GATEWAY="192.168.0.1" # Gateway / Router
DNS1="192.168.0.1" # DNS Server 1
DNS2="8.8.8.8" # DNS Server 2
IPV6_PRIVACY="no"

Restart the network service using the below command.

service network restart

Now you can see the ip address 192.168.0.10 assigned to the interface enp0s3.

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.10  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 2406:7400:bf:7cdd:e387:a515:14c9:2272  prefixlen 64  scopeid 0x0
        inet6 fe80::9cf2:e3d2:e192:8273  prefixlen 64  scopeid 0x20
        ether 08:00:27:13:2c:70  txqueuelen 1000  (Ethernet)
        RX packets 1001  bytes 95433 (93.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1055  bytes 136959 (133.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 64  bytes 5632 (5.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64  bytes 5632 (5.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

DHCP IP Address

In this mode, the system will get an IP address from the DHCP server. This is the default mode set on CentOS to get an IP address from the DHCP server.

ADVERTISEMENT

cd /etc/sysconfig/network-scripts/

Edit the ifcfg-<interfacename> file.

vi ifcfg-enp0s3

Just modify the lines like this.

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp" # DHCP IP
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="enp0s3"
DEVICE="enp0s3"
ONBOOT="yes" # Enable Network Interface on boot
IPV6_PRIVACY="no"

Restart the network service using the below command.

service network restart

Now you can see the ip address 192.168.0.23 assigned to the interface enp0s3 using DHCP.

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.23  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 2406:7400:bf:7cdd:e387:a515:14c9:2272  prefixlen 64  scopeid 0x0
        inet6 2406:7400:bf:e32d:8706:45ed:f7dd:cda8  prefixlen 64  scopeid 0x0
        inet6 fe80::9cf2:e3d2:e192:8273  prefixlen 64  scopeid 0x20
        ether 08:00:27:13:2c:70  txqueuelen 1000  (Ethernet)
        RX packets 663  bytes 62963 (61.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 754  bytes 80123 (78.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 64  bytes 5632 (5.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64  bytes 5632 (5.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Conclusion

That’s All. Please share your feedback in the comments section.

You might also like