Linux: Create virtual network interface

On Linux, virtual network interface configuration isn’t something most people need to do very often, but it can come in handy sometimes. The system will recognize a virtual interface as a real, physical interface. linux create virtual network interface

In this respect, it works sort of like a virtual machine – that is, it emulates the necessary hardware components to seem like it’s physically connected to the machine.

In this tutorial, you’ll learn how to create a virtual network interface on Linux. Follow along with the step by step instructions below to create one or more of these interfaces on your own system.

In this tutorial you will learn:

  • How to create virtual network interfaces on Linux
Linux virtual network interface
Linux: create virtual network interface
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Create virtual network interfaces on Linux




The methods for creating a virtual network interface have changed a bit through the years. There is more than one way to do this, but we will be using the “dummy” kernel module to set up our virtual interface in these steps.

  1. Start off by enabling the dummy kernel module with the following command.
    $ sudo modprobe dummy
    
  2. Now that the module has been loaded, we can create a new virtual interface. Feel free to name yours however you want, but we will name ours eth0 in this example.
    $ sudo ip link add eth0 type dummy
    

    You will be able to verify that the link was added by executing the following command afterwards:

    $ ip link show eth0
    
  3. We have our virtual interface, but it’t not much use to us without an IP address or MAC address. Let’s give the interface a MAC address with the following command. Feel free to substitute any address you want to use, as ours is just a randomly generated one.
    $ sudo ifconfig eth0 hw ether C8:D7:4A:4E:47:50
    

    Note that if the ifconfig command is not available, you’ll need to install package net-tools.



  4. We can now add an alias to the interface and configure an it with an IP address.
    $ sudo ip addr add 192.168.1.100/24 brd + dev eth0 label eth0:0
    
  5. Don’t forget to put the interface up, or it probably won’t be very useful.
    $ sudo ip link set dev eth0 up
    
  6. You should now be able to use your virtual network interface for whatever you want. You can see the full configuration by viewing the output of the ip a command.
    $ ip a
    [...]
    3: eth0: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
        link/ether c8:d7:4a:4e:47:50 brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.100/24 brd 192.168.1.255 scope global eth0:0
           valid_lft forever preferred_lft forever
    
  7. If the virtual network interface has finished serving its purpose, you can revert all your changes with the following commands.
    $ sudo ip addr del 192.168.1.100/24 brd + dev eth0 label eth0:0
    $ sudo ip link delete eth0 type dummy
    $ sudo rmmod dummy
    


A fully configured virtual network interface on Linux
A fully configured virtual network interface on Linux

Closing Thoughts

In this tutorial, we learned how to add a virtual network interface to a Linux system, by using the dummy kernel module. This is a useful practice in multiple scenarios. For example, if you have an application that must be tied to a network interface and don’t wish to use your physical adapter.



Comments and Discussions
Linux Forum