Adding an IP Address to an Interface With Red Hat and CentOS

There is some apparent voodoo on what, exactly, is needed to add an IP address to an interface using Red Hat style scripts. We have tested this on Red Hat Enterprise 3 and CentOS 4, and it appears that the convention is quite forgiving. This makes sense, really, since if you hose up these interfaces, well, you can’t administer the machine remotely. Let’s look at our current interfaces on eth0:

 
[root@srv-1 usr-1]# /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:60:97:94:52:69  
inet addr:10.50.100.1  Bcast:10.50.100.255  Mask:255.255.255.0
inet6 addr: fe80::260:97ff:fe94:5269/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:381439 errors:0 dropped:0 overruns:0 frame:0
TX packets:454464 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000 
RX bytes:233910853 (223.0 MiB)  TX bytes:303099507 (289.0 MiB)
Interrupt:5 Base address:0xd400 
lo        Link encap:Local Loopback  
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:16436  Metric:1
RX packets:1977 errors:0 dropped:0 overruns:0 frame:0
TX packets:1977 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0 
RX bytes:107244 (104.7 KiB)  TX bytes:107244 (104.7 KiB)

We have one IP address on eth0. This is configured in /etc/sysconfig/network-scripts:

[root@srv-1 usr-1]# cd /etc/sysconfig/network-scripts
[root@srv-1 network-scripts]# ls ifcfg-eth0*
ifcfg-eth0
[root@srv-1 network-scripts]# cat ifcfg-eth0*
DEVICE=eth0
BOOTPROTO=static
BROADCAST=10.50.100.255
HWADDR=00:60:97:94:52:69
IPADDR=10.50.100.1
NETMASK=255.255.255.0
NETWORK=10.50.100.0
ONBOOT=yes
TYPE=Ethernet

Now, the hardware address will vary, certainly, and other entries may or may not be on your system. There is a lot of variation here. The safest way is to copy the config file for the interface you want to add the IP address to, and then edit that file. It really doesn’t matter what you call the file. We will standardize on ifcfg-eth0:1 later in the article, and believe this is a good naming scheme; however, we can also call it ifcfg-eth0-blahsecond:

[root@srv-1 network-scripts]# cp ifcfg-eth0* ifcfg-eth0-blahsecond
[root@srv-1 network-scripts]# vi ifcfg-eth0-blahsecond
[root@srv-1 network-scripts]# cat ifcfg-eth0-blahsecond
DEVICE=eth0:blahsecond
BOOTPROTO=static
BROADCAST=10.50.100.255
HWADDR=00:60:97:94:52:69
IPADDR=10.50.100.2
NETMASK=255.255.255.0
NETWORK=10.50.100.0
ONBOOT=yes
TYPE=Ethernet
[root@srv-1 network-scripts]# /etc/init.d/network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Setting network parameters:                                [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]
Bringing up interface eth0-blahsecond:                     [  OK  ]
[root@srv-1 network-scripts]# /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:60:97:94:52:69  
inet addr:10.50.100.1  Bcast:10.50.100.255  Mask:255.255.255.0
inet6 addr: fe80::260:97ff:fe94:5269/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:383046 errors:0 dropped:0 overruns:0 frame:0
TX packets:456302 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000 
RX bytes:234100139 (223.2 MiB)  TX bytes:304511869 (290.4 MiB)
Interrupt:5 Base address:0xd400 
eth0:blah Link encap:Ethernet  HWaddr 00:60:97:94:52:69  
inet addr:10.50.100.2  Bcast:10.50.100.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
Interrupt:5 Base address:0xd400 
lo        Link encap:Local Loopback  
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:16436  Metric:1
RX packets:1977 errors:0 dropped:0 overruns:0 frame:0
TX packets:1977 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0 
RX bytes:107244 (104.7 KiB)  TX bytes:107244 (104.7 KiB)
[root@srv-1 network-scripts]#

The colon after eth0 on the interface name does seem to be required with this CentOS 4 box. Without it, the startup script seems to hang. Also, notice how the stuff after the : is truncated to 4 characters. Let’s change the device name to something more standard:

 
[root@srv-1 network-scripts]# vi ifcfg-eth0-blahsecond
[root@srv-1 network-scripts]# cat ifcfg-eth0-blahsecond
DEVICE=eth0:1
BOOTPROTO=static
BROADCAST=10.50.100.255
HWADDR=00:60:97:94:52:69
IPADDR=10.50.100.2
NETMASK=255.255.255.0
NETWORK=10.50.100.0
ONBOOT=yes
TYPE=Ethernet
[root@srv-1 network-scripts]# /etc/init.d/network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Setting network parameters:                                [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]
Bringing up interface eth0-blahsecond:                     [  OK  ]
[root@srv-1 network-scripts]# /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:60:97:94:52:69  
inet addr:10.50.100.1  Bcast:10.50.100.255  Mask:255.255.255.0
inet6 addr: fe80::260:97ff:fe94:5269/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:384060 errors:0 dropped:0 overruns:0 frame:0
TX packets:457491 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000 
RX bytes:234243015 (223.3 MiB)  TX bytes:305215295 (291.0 MiB)
Interrupt:5 Base address:0xd400 
eth0:1    Link encap:Ethernet  HWaddr 00:60:97:94:52:69  
inet addr:10.50.100.2  Bcast:10.50.100.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
Interrupt:5 Base address:0xd400 
lo        Link encap:Local Loopback  
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:16436  Metric:1
RX packets:1977 errors:0 dropped:0 overruns:0 frame:0
TX packets:1977 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0 
RX bytes:107244 (104.7 KiB)  TX bytes:107244 (104.7 KiB)
[root@srv-1 network-scripts]# 

That looks more standard, and will avoid possible problems in the future because this is how everybody does it for the most part. Notice how it is not true that the file name has to match the interface name? Let’s make it match, though:

[root@srv-1 network-scripts]# mv  ifcfg-eth0-blahsecond ifcfg-eth0:1
[root@srv-1 network-scripts]# /etc/init.d/network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Setting network parameters:                                [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]
[root@srv-1 network-scripts]# /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:60:97:94:52:69  
inet addr:10.50.100.1  Bcast:10.50.100.255  Mask:255.255.255.0
inet6 addr: fe80::260:97ff:fe94:5269/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:384676 errors:0 dropped:0 overruns:0 frame:0
TX packets:458326 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000 
RX bytes:234326065 (223.4 MiB)  TX bytes:305924137 (291.7 MiB)
Interrupt:5 Base address:0xd400 
eth0:1    Link encap:Ethernet  HWaddr 00:60:97:94:52:69  
inet addr:10.50.100.2  Bcast:10.50.100.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
Interrupt:5 Base address:0xd400 
lo        Link encap:Local Loopback  
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:16436  Metric:1
RX packets:1977 errors:0 dropped:0 overruns:0 frame:0
TX packets:1977 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0 
RX bytes:107244 (104.7 KiB)  TX bytes:107244 (104.7 KiB)
[root@srv-1 network-scripts]#

Now we have the typical config, it works fine, and most anybody will know what you are doing and how. In summary, to add an IP, find the highest device number on the interface using ifconfig, and add another IP using that device name in a file named in the same way. Fairly easy. Do make a copy of anything you change, of course, and do be very careful you don’t make the machine unavailable over the network. If at all possible, do this from a console or serial terminal.