Friday, December 23, 2011

Configuring virtual network interfaces in RedHat


In RedHat we are using the files in /etc/sysconfig/network-scripts/ to configure the network. We'll see how to configure virtual interface using these files.

The configuration of an interface would typically be using a file called /etc/sysconfig/network-scripts/ifcfg-eth1, in the case of eth1, with the following information:

DEVICE=eth1
IPADDR=10.17.128.50
NETMASK=255.255.255.0
ONBOOT=yes

In the case of a virtual interface would be very similar, you just need to add two points and a number that identifies it. For example, create the file /etc/sysconfig/network-scripts/ifcfg-eth0:0 with the following contents:

DEVICE=eth1:0
IPADDR=10.17.80.50
NETMASK=255.255.255.0
ONBOOT=yes

Then we run /etc/init.d/network restart:

# /etc/init.d/network restart

Shutting down interface eth0:                              [  OK  ]
Shutting down interface eth1:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]
Bringing up interface eth1:                                [  OK  ]

# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:73
          inet addr:10.16.80.1  Bcast:10.12.80.255  Mask:255.255.255.0
          inet6 addr: fe80::21e:c9ff:feab:ef73/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:714 errors:0 dropped:0 overruns:0 frame:0
          TX packets:801 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:131970 (128.8 KiB)  TX bytes:119930 (117.1 KiB)
          Interrupt:169 Memory:f8000000-f8012100

eth1      Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.128.50  Bcast:10.12.128.255  Mask:255.255.255.0
          inet6 addr: fe80::21e:c9ff:feab:ef75/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:2616 (2.5 KiB)
          Interrupt:169 Memory:f4000000-f4012100

eth1:0    Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.50  Bcast:10.12.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

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:137686820 errors:0 dropped:0 overruns:0 frame:0
          TX packets:137686820 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3287570989 (3.0 GiB)  TX bytes:3287570989 (3.0 GiB)

In case you want to configure a range of IPs is very annoying to have to create a file for each IP, so if we can create correlated with a single file. Create a file called /etc/sysconfig/network-scripts/ifcfg-eth0-range0 with the following:

IPADDR_START=10.17.80.50
IPADDR_END=10.17.80.60
CLONENUM_START=0

Then restarting /etc/init.d/network we can see how it created all interfaces from IP 10.17.80.50 to 10.17.80.60 (total 10) using a single file for it.

# /etc/init.d/network restart

Shutting down interface eth0:                              [  OK  ]
Shutting down interface eth1:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]
Bringing up interface eth1:                                [  OK  ]

# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:73
          inet addr:10.16.80.1  Bcast:10.12.80.255  Mask:255.255.255.0
          inet6 addr: fe80::21e:c9ff:feab:ef73/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:518 errors:0 dropped:0 overruns:0 frame:0
          TX packets:561 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:114796 (112.1 KiB)  TX bytes:75124 (73.3 KiB)
          Interrupt:169 Memory:f8000000-f8012100

eth1      Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.12.128.50  Bcast:10.12.128.255  Mask:255.255.255.0
          inet6 addr: fe80::21e:c9ff:feab:ef75/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:55 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:11100 (10.8 KiB)
          Interrupt:169 Memory:f4000000-f4012100

eth1:0    Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.50  Bcast:10.17.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

eth1:1    Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.51  Bcast:10.17.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

eth1:2    Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.52  Bcast:10.17.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

eth1:3    Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.53  Bcast:10.17.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

eth1:4    Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.54  Bcast:10.17.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

eth1:5    Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.55  Bcast:10.17.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

eth1:6    Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.56  Bcast:10.17.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

eth1:7    Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.57  Bcast:10.17.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

eth1:8    Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.58  Bcast:10.17.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

eth1:9    Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.59  Bcast:10.17.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

eth1:10   Link encap:Ethernet  HWaddr 00:1E:C9:AB:EF:75
          inet addr:10.17.80.60  Bcast:10.17.80.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Memory:f4000000-f4012100

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:137755392 errors:0 dropped:0 overruns:0 frame:0
          TX packets:137755392 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3304455536 (3.0 GiB)  TX bytes:3304455536 (3.0 GiB)

We can use the parameter definitions for CLONENUM_START to combine unique range of interface definitions, we only have to be careful not to "tread" between them.

How do I remove virtual interfaces such as eth0:1 or eth1:1?

A. Use ifconfig command. It is used to remove virtual interfaces or network aliases.

Ifconfig is used to configure the kernel-resident network interfaces. It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed.

Type the command to remove eth0:1:
# ifconfig eth0:1 down

/etc/sysconfig/network-scripts/ifcfg-ethX-range0 file

To remove interface permanently edit network configuration file stored at /etc/directory. For RedHat the file stored in /etc/sysconfig/network-scripts/ directory. For Debian or Ubuntu Linux just edit file /etc/network/interfaces and remove the entries. For example open file called:  /etc/sysconfig/network-scripts/ifcfg-eth1-range0

# vi /etc/sysconfig/network-scripts/ifcfg-eth0-range0

You will see network aliases configuration:

IPADDR_START=192.167.1.5
IPADDR_END=192.167.1.100
CLONENUM_START=0
NETMASK=255.255.255.0

Just comment out everything or just rename the file:
# mv /etc/sysconfig/network-scripts/ifcfg-eth0-range0 /etc/sysconfig/network-scripts/working.ifcfg-eth0-range0.backup

Note: always rename the file "ifcfg-eth0-range0" adding a word at the beggining of it. For example. 2011-12-23_ifcfg-eth0-range0 . If you don't rename like that there is a risk (when you restart the network service) that both ranges/virtual interfaces start up.

Just restart network service:
# service network restart