How to set up a Static IP in CentOS virtual machine

  • Post category:Network
  • Reading time:4 mins read

In this article we will understand how to set up a static IP in a CentOS virtual machine. To set up a Static IP, please follow the steps below in the VM. Ensure you have the static IP, gateway, and netmask to properly configure the VM

First step is to create a backup of ifcfg-eth0 file present at /etc/sysconfig/network-scripts/ifcfg-eth0

cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.backup

Now after creating the backup, change following properties in ifcfg-eth0 file

  • BOOTPROTO
  • IPADDR
  • NETMASK
  • GATEWAY
[proedu@localhost network-scripts]$ vi ifcfg-eth0 
PROXY_METHOD=none
BROWSER_ONLY=no
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=0fbc48bb-c23b-4225-9450-06a182bdba7b
DEVICE=eth0
ONBOOT=yes
TYPE=Ethernet
BOOTPROTO=static
DNS1=8.8.8.8
DNS2=8.8.4.4
PREFIX=24
IPADDR=192.168.1.103
NETMASK=255.255.255.0
GATEWAY=192.168.1.1

Now edit the file /etc/sysconfig/network. Add below entries

[proedu@localhost network-scripts]$ vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=proedu.co
GATEWAY=192.168.1.1

After configuring the IP, we now need to configure DNS. This can be done in /etc/resolv.conf

[proedu@localhost network-scripts]$ vi /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

Next step is to add the IP and hostname in /etc/hosts

[proedu@localhost network-scripts]$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.1.103   proedu.co

To apply the network changes, restart using following command

services network restart

Finally confirm the static IP has been applied to the VM

[proedu@localhost network-scripts]$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.103  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::215:5dff:fe38:104  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:38:01:04  txqueuelen 1000  (Ethernet)
        RX packets 265699  bytes 372765010 (355.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 151113  bytes 87012607 (82.9 MiB)
        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<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 39891  bytes 1341027230 (1.2 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 39891  bytes 1341027230 (1.2 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:cb:c7:ab  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

We have assigned a static IP to our virtual machine. Lets ping the VM from command prompt.

C:\Users\Admin>ping 192.168.1.103

Pinging 192.168.1.103 with 32 bytes of data:
Reply from 192.168.1.103: bytes=32 time<1ms TTL=64
Reply from 192.168.1.103: bytes=32 time<1ms TTL=64
Reply from 192.168.1.103: bytes=32 time<1ms TTL=64
Reply from 192.168.1.103: bytes=32 time<1ms TTL=64

Ping statistics for 192.168.1.103:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

Happy Learning 🙂

This Post Has One Comment

  1. Prakash Chauhan

    Excellent Article !!!

Comments are closed.