Creating a Virtual Network Interface in Debian

There are times when you might want to assign more than one IP to a system, even if it only has a single physical NIC. This documentation details how to create a virtual network interface (known as aliasing) under Debian (see here for how to alias in Centos 6).

We'll assume that your NIC is eth0, if not then simply use the name of your network interface.

To check, run

cat /etc/network/interfaces

You should see an entry similar to one of the following

auto eth0
iface eth0 inet dhcp

# OR
iface eth0 inet manual
address 192.168.1.252
netmask 255.255.255.0
gateway 192.168.1.254

Taking the settings from above, we can create the virtual interface by doing the following

nano /etc/network/interfaces

# Add the following
auto eth0:1
allow-hotplug eth0:1
iface eth0:1 inet static
address 192.168.1.248
netmask 255.255.255.0
gateway 192.168.1.254

# Ctrl-X, Y to Save and exit

The configuration above creates a new interface - eth0:1 and sets a static IP of 192.168.1.248. To apply the changes, we simply need to restart networking

service networking restart

It's that simple, now you should be able to see the interface

ifconfig
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:65536 Metric:1
RX packets:129 errors:0 dropped:0 overruns:0 frame:0
TX packets:129 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:16247 (15.8 KiB) TX bytes:16247 (15.8 KiB)

eth0 Link encap:Ethernet HWaddr b8:27:eb:a2:0e:62
inet addr:192.168.1.252 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:902097 errors:0 dropped:0 overruns:0 frame:0
TX packets:432782 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:203759013 (194.3 MiB) TX bytes:71615701 (68.2 MiB)

eth0:1 Link encap:Ethernet HWaddr b8:27:eb:a2:0e:62
inet addr:192.168.1.248 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

We can see the interface is up and running, and should be able to ping the IP that we've added

Using a Different Subnet

If we wanted to add an IP on a different subnet (say 10.10.10.5/30), the initial steps are exactly the same

nano /etc/network/interfaces

# Add the following
auto eth0:1
allow-hotplug eth0:1
iface eth0:1 inet static
address 10.10.10.7
netmask 255.255.255.252
gateway 10.10.10.6

# Ctrl-X, Y to Save and exit

The only additional step would be making sure that clients on the LAN know how to route to it. For this, we need to add a static route (either to the other LAN clients, or on the router itself) using the IP of the physical NIC as a gateway

route add -net 10.10.10.7 netmask 255.255.255.252 gw 192.168.1.252
# Or
route add -net 10.10.10.7/32 gw 192.168.1.252

Obviously, depending on your network setup there may be a gateway that needs to know instead (in the example above we used a /30, so there's a second IP free for the gateway there)

 
 Share