How to create a virtual network interface in Ubuntu 20.04

Share this article

Introduction

A virtual interface is a networking interface, that mimics a physical interface. With the help of virtual interfaces creating virtual machines or containers are possible.

Adding virtual interface.

Adding a virtual interface is a very simple and straight task. This can be done with ip command and with some arguments.

In the below-mentioned command, I have added an interface with the name vr-br.

Adding a non persistent interface

Use the following command to add a nonpersistent interface. In the following command dummy is the kernel module.

sudo ip link add name <virtual_interface_name> type dummy

Example of the above command and its verification is shown in the following code section

ubuntu@ubuntu-exp:~$ sudo ip link add name vr-br type dummy
ubuntu@ubuntu-exp:~$ 
ubuntu@ubuntu-exp:~$ 
ubuntu@ubuntu-exp:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc fq_codel state UP group default qlen 1000
    link/ether fa:16:3e:6e:46:61 brd ff:ff:ff:ff:ff:ff
    inet 10.0.1.62/8 brd 10.255.255.255 scope global dynamic ens3
       valid_lft 77157sec preferred_lft 77157sec
    inet6 fe80::f816:3eff:fe6e:4661/64 scope link 
       valid_lft forever preferred_lft forever
3: vr-br: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 3a:4f:ec:a3:ad:68 brd ff:ff:ff:ff:ff:ff
ubuntu@ubuntu-exp:~$ 

You can then play with the interface and you can also assign IP address to this interface. This type of assignment is not persistent, which means after a reboot of your machine you won’t find a network interface.

Adding a persistent interface and IP address

Adding vr-br.netdev and vr-br.network file in /etc/systemd/network/ directory

sudo touch /etc/systemd/network/vr-br.netdev
sudo touch /etc/systemd/network/vr-br.network

The above command when we run in our system.

ubuntu@ubuntu-exp:~$ sudo touch /etc/systemd/network/vr-br.netdev
ubuntu@ubuntu-exp:~$ sudo touch /etc/systemd/network/vr-br.network
ubuntu@ubuntu-exp:~$ ls /etc/systemd/network/
vr-br.netdev vr-br.network
ubuntu@ubuntu-exp:~$

Edit both the files, I have added the following content in these files.

ubuntu@ubuntu-exp:~$ cat /etc/systemd/network/vr-br.network 
[Match]
Name=vr-br

[Network]
Address=192.168.0.100
Mask=255.255.255.0
ubuntu@ubuntu-exp:~$ 
ubuntu@ubuntu-exp:~$ 
ubuntu@ubuntu-exp:~$ cat /etc/systemd/network/vr-br.netdev 
[NetDev]
Name=vr-br
Kind=dummy
ubuntu@ubuntu-exp:~$ 

For initializing the configuration you can reboot the system or simply run the below-mentioned commands.

sudo systemctl restart systemd-networkd

For testing check the interface with ip a command

ubuntu@ubuntu-exp:~$ sudo systemctl restart systemd-networkd
ubuntu@ubuntu-exp:~$ 
ubuntu@ubuntu-exp:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc fq_codel state UP group default qlen 1000
    link/ether fa:16:3e:6e:46:61 brd ff:ff:ff:ff:ff:ff
    inet 10.0.1.62/8 brd 10.255.255.255 scope global dynamic ens3
       valid_lft 86394sec preferred_lft 86394sec
    inet6 fe80::f816:3eff:fe6e:4661/64 scope link 
       valid_lft forever preferred_lft forever
3: vr-br: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
    link/ether 3a:4f:ec:a3:ad:68 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.100/24 brd 192.168.0.255 scope global vr-br
       valid_lft forever preferred_lft forever
    inet6 fe80::384f:ecff:fea3:ad68/64 scope link 
       valid_lft forever preferred_lft forever
ubuntu@ubuntu-exp:~$ 

Other tools

We can use ifconfig commands to add a new interface and to manage or play with other network configurations. However, for ifconfig command, we need to install the net-tool package in ubuntu. You can use the following command to install the package.

ubuntu@ubuntu-exp:~$ sudo apt install net-tools

5 thoughts on “How to create a virtual network interface in Ubuntu 20.04”

  1. I do believe all of the concepts you have offered on your post.
    They’re very convincing and can certainly work.
    Still, the posts are very brief for beginners. May you please lengthen them a bit from next time?
    Thanks for the post.

  2. I tried, It worked for me, but when I restart machine, the virtual network disappeared. until I run below only:
    sudo systemctl restart systemd-networkd

    It worked again.
    How to persistant it for me?

    1. If you are using Ubuntu (desktop), it is possible that you are using NetworkManager to manage your network. When you restart your system, systemd-networkd service is not enabled (you can check the service status after the restart).
      The quick fix here is Automatically run the command on Linux startup via rc.local or crontab.

  3. Thank you very much you saved my time. I have been struggling for 2 days to bring my VPN up. Now it’s working perfectly
    Regards

Leave a Comment

Your email address will not be published. Required fields are marked *