This documentation provides a step-by-step guide to creating and configuring virtual network elements using namespaces, virtual Ethernet cables, and a virtual bridge in order to establish communication between two namespaces.
Before proceeding with the setup, ensure that the following prerequisites are met:
- Basic knowledge of Linux networking and terminal commands.
- Vagrant, Git and VirtualBox are installed on your machine.
-
Download and Install VirtualBox from the official website (https://www.virtualbox.org)
-
Download and Install Vagrant from the official website (https://www.vagrantup.com/)
-
Open your git bash terminal
-
Create a new directory for your virtual environment mkdir ubuntu-vm
-
Change to the newly created directory cd ubuntu-vm
-
Initialize a new Vagrant Box with Ubuntu: vagrant init ubuntu/bionic64
-
Start the Vagrant box:
vagrant up
This process may take a few minutes.
-
SSH into the running Vagrant box:
vagrant ssh
This will give you access to the Ubuntu environment within the virtual machine.
-
Open a terminal or shell prompt on your Linux system.
-
Create the first namespace using the following command:
sudo ip netns add ns1
-
Create the second namespace using the same command:
sudo ip netns add ns2
-
Create the first virtual Ethernet cable connecting ns1 and the virtual bridge:
sudo ip link add ns1-veth-in type veth peer name ns1-veth-out
-
Create the second virtual Ethernet cable connecting namespace2 and the virtual bridge:
sudo ip link add ns2-veth-in type veth peer name ns2-veth-out
-
Move the ns1-veth-in end of the cable into ns1:
sudo ip link set ns1-veth-in netns ns1
-
Move the ns2-veth-in end of the cable into ns2:
sudo ip link set ns2-veth-in netns ns2
-
Assign an IP address to the ns1-veth-in interface within ns1:
sudo ip netns exec ns1 ip addr add 10.10.0.10/16 dev ns1-veth-in
-
Assign an IP address to the ns2-veth-in interface within ns2:
sudo ip netns exec ns2 ip addr add 10.10.0.20/16 dev ns2-veth-in
-
Create the virtual bridge:
sudo ip link add br0 type bridge
-
Activate the virtual devices:
sudo ip link set br0 up sudo ip link set ns1-veth-out up sudo ip link set ns2-veth-out up sudo ip netns exec ns1 ip link set dev lo up sudo ip netns exec ns1 ip link set dev ns1-veth-in up sudo ip netns exec ns2 ip link set dev lo up sudo ip netns exec ns2 ip link set dev ns2-veth-in up
-
Connect the ns1-veth-out interface to the virtual bridge:
sudo ip link set ns1-veth-out master br0
-
Connect the ns2-veth-out interface to the virtual bridge:
sudo ip link set ns2-veth-out master br0
-
Open two terminal windows, one for each namespace.
-
In the first terminal, enter the ns1:
sudo ip netns exec ns1 bash
-
In the second terminal, enter the ns2:
sudo ip netns exec ns2 bash
-
Ping the IP address of the opposite namespace from each terminal to test communication:
In the ns1 terminal:
ping 10.10.0.20
In the ns2 terminal:
ping 10.10.0.10
Note that, we didn't add any default route here but still its working cause bridge is involved here. By checking route inside the namespaces, we can see route already added.
sudo ip netns exec ns1 ip route
We have successfully created two namespaces, established virtual Ethernet cables, created a virtual bridge, and enabled communication between the namespaces. You can now use this setup for various networking experiments and configurations.