Linux Ubuntu Add Sub-Interface
Ubuntu Sub-interfaces
I have found myself searching the internet for the correct configuration for
Linux subinterfaces.
A subinterface is a division of one physical interface into multiple logical interfaces.
So why would we do that?
!
I use subinterfaces for hosting multiple SSL sites, DSR returns for localhost for my
load balancers, and anything else you would need multiple ips on the same physical interface.
In Ubuntu it is easy to add subinterfaces I have never had to add a temporary subinterface
in Ubuntu but I guess I figure it is easier to just add it to the system and restart networking.
Here is how to add a Ubuntu subinterface with ifconfig.
Adding a Ubuntu subinterface without restarting networking.
1. Add the interface and ip with one step
$ sudo ifconfig eth0:0 192.168.1.253 netmask 255.255.255.0
2. Turn the ip address up
$ sudo ifconfig eth0:0 up
3. Check and make sure it is in ifconfig
$ ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:30:48:28:65:2b
inet addr:192.168.1.5 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::230:48ff:fe28:652b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:101278725 errors:10 dropped:0 overruns:0 frame:10
TX packets:96594294 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3734515707 (3.7 GB) TX bytes:1773845088 (1.7 GB)
eth0:0 Link encap:Ethernet HWaddr 00:30:48:28:65:2b
inet addr:192.168.1.253 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
4. Remember the subinterface is gone unless you add it to /etc/network/interfaces.
Adding a Ubuntu subinterface permanently.
Now that you have added the subinterface without restarting networking, or
rebooting, we need to add the ip address to the configuration file on Ubuntu so the
new ip address will be on the system when you do some upgrades and need to reboot.
1. Open the /etc/network/interfaces file with your favorite editor
$ sudo vi /etc/network/interfaces
2. Add the following lines below your physical interface to create the subinterface on reboot.
auto eth0:0
iface eth0:0 inet static
address 192.168.1.253
netmask 255.255.255.0
3. You can always add more Ubuntu subinterfaces by changing
eth0:0 to eth0:1 and eth0:2 and so on.
Here is and example of my complete /etc/network/interfaces file with Ubuntu subinterfaces.
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.5
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
auto eth0:0
iface eth0:0 inet static
address 66.37.141.237
netmask 255.255.255.0