Skip to content

Instantly share code, notes, and snippets.

@beddari
Forked from nerdalert/veth_pair_example.md
Created January 18, 2016 07:57
Show Gist options
  • Save beddari/eb3ca263b9533acf5d2b to your computer and use it in GitHub Desktop.
Save beddari/eb3ca263b9533acf5d2b to your computer and use it in GitHub Desktop.

Linux Networking Veth Pair Namespace Sausage Factory

Summary

This is a simple example of namespace networking.

  • Create 4 different namespaces and assign an ip address to each side of the veth pair
sudo ip netns add ns1 
sudo ip netns add ns2 
sudo ip netns add ns3 
sudo ip netns add ns4 
sudo ip link add veth1 type veth peer name veth11 
sudo ip link add veth2 type veth peer name veth12 
sudo ip link add veth3 type veth peer name veth13 
sudo ip link add veth4 type veth peer name veth14 
sudo ip link set veth11 netns ns1 
sudo ip link set veth12 netns ns2 
sudo ip link set veth13 netns ns3 
sudo ip link set veth14 netns ns4 
sudo ip netns exec ns1 ifconfig lo up 
sudo ip netns exec ns2 ifconfig lo up 
sudo ip netns exec ns3 ifconfig lo up 
sudo ip netns exec ns4 ifconfig lo up 
sudo ifconfig veth1 10.1.11.1/24 up 
sudo ifconfig veth2 10.1.12.1/24 up 
sudo ifconfig veth3 10.1.13.1/24 up 
sudo ifconfig veth4 10.1.14.1/24 up 
sudo ip netns exec ns1 ifconfig veth11 10.1.11.2/24 up 
sudo ip netns exec ns2 ifconfig veth12 10.1.12.2/24 up 
sudo ip netns exec ns3 ifconfig veth13 10.1.13.2/24 up 
sudo ip netns exec ns4 ifconfig veth14 10.1.14.2/24 up 
sudo ip netns exec ns1 route add default gw 10.1.11.1 veth11 
sudo ip netns exec ns2 route add default gw 10.1.12.1 veth12 
sudo ip netns exec ns3 route add default gw 10.1.13.1 veth13 
sudo ip netns exec ns4 route add default gw 10.1.14.1 veth14

Now from ns1 ping ns2, ns3, and ns4 and verify reachability.

ip netns exec ns1 ping -c2 10.1.12.2
ip netns exec ns1 ping -c2 10.1.13.2
ip netns exec ns1 ping -c2 10.1.14.2

To remove the newly created namespaces.

sudo ip netns del ns1 
sudo ip netns del ns2 
sudo ip netns del ns3 
sudo ip netns del ns4 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment