January 14, 2020

Assign a secondary IP in Centos/Redhat

Assign a secondary IP in Centos/Redhat

Navigate to your network-scripts location:

cd /etc/sysconfig/network-scripts/

cat the interface you want to add a secondary IP to, you can check your interfaces by using

$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1460 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 42:01:ac:10:08:05 brd ff:ff:ff:ff:ff:ff
    inet 172.16.8.5/32 brd 172.16.8.5 scope global dynamic eth0
       valid_lft 83548sec preferred_lft 83548sec
    inet6 fe80::4001:acff:fe10:805/64 scope link 
       valid_lft forever preferred_lft forever

Or

$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1460
        inet 172.16.8.5  netmask 255.255.255.255  broadcast 172.16.8.5
        inet6 fe80::4001:acff:fe10:805  prefixlen 64  scopeid 0x20<link>
        ether 42:01:ac:10:08:05  txqueuelen 1000  (Ethernet)
        RX packets 29454  bytes 84679379 (80.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21859  bytes 6134384 (5.8 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 10  bytes 5958 (5.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 5958 (5.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Use ls to list the directory contents:

network-scripts]$ ls 
ifcfg-eth0    ifdown       ifdown-ippp  ifdown-post    ifdown-sit       ifdown-tunnel  ifup-bnep  ifup-ipv6  ifup-plusb  ifup-routes  ifup-TeamPort  init.ipv6-global
ifdown-bnep  ifdown-ipv6  ifdown-ppp     ifdown-Team      ifup           ifup-eth   ifup-isdn  ifup-post   ifup-sit     ifup-tunnel    network-functions
ifcfg-lo      ifdown-eth   ifdown-isdn  ifdown-routes  ifdown-TeamPort  ifup-aliases   ifup-ippp  ifup-plip  ifup-ppp    ifup-Team    ifup-wireless  network-functions-ipv6

cat the interface you will be adding a secondary IP to

$ cat ifcfg-eth0

# Generated by parse-kickstart
NM_CONTROLLED=no
IPV6INIT="no"
DHCP_HOSTNAME="localhost"
BOOTPROTO="dhcp"
DEVICE="eth0"
ONBOOT="yes"
UUID="18fa3a36-80a2-442f-bc21-276a1bb368be"
MTU=1460
PERSISTENT_DHCLIENT="y"
IPV6INIT=yes

create a new sub-interface file in the format of interface:1 for this we will create ifcfg-eth0:1

sudo vi ifcfg-eth0:1

Copy the previous config replacing the BOOTPROTO with static and add the IPADDR for this interface

NM_CONTROLLED=no
IPV6INIT="no"
DHCP_HOSTNAME="localhost"
BOOTPROTO="static"
DEVICE="eth0"
ONBOOT="yes"
UUID="18fa3a36-80a2-442f-bc21-276a1bb368be"
MTU=1460
PERSISTENT_DHCLIENT="y"
IPV6INIT=yes
IPADDR=172.16.8.2
PREFIX=32

bring up the interface:

sudo ifup ifcfg-eth0:1

You will see a message as follows:


Determining if ip address 172.16.8.2 is already in use for device eth0...

Verify the new ipaddress

 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
   inet 127.0.0.1/8 scope host lo
      valid_lft forever preferred_lft forever
   inet6 ::1/128 scope host 
      valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1460 qdisc pfifo_fast state UP group default qlen 1000
   link/ether 42:01:ac:10:08:05 brd ff:ff:ff:ff:ff:ff
   inet 172.16.8.5/32 brd 172.16.8.5 scope global dynamic eth0
      valid_lft 82096sec preferred_lft 82096sec
   inet 172.16.8.2/32 brd 172.16.8.2 scope global eth0
      valid_lft forever preferred_lft forever
   inet6 fe80::4001:acff:fe10:805/64 scope link 
      valid_lft forever preferred_lft forever