Skip to content

Instantly share code, notes, and snippets.

@stephen-hannam
Last active May 31, 2022 00:05
Show Gist options
  • Save stephen-hannam/c532d6e5f7f0b089621bc772f341584e to your computer and use it in GitHub Desktop.
Save stephen-hannam/c532d6e5f7f0b089621bc772f341584e to your computer and use it in GitHub Desktop.
Linux Network Namespaces

Instructions

  1. Create two network namespaces
ip netns add ns0
ip netns add ns1
  1. Assign interfaces to namespaces
ip link set enp23s0 netns ns0
ip link set enp101s0 netns ns1
  1. Enter network namespace(s), assign IPs and create a default route if desired
ip netns exec ns0 bash # Drops into a subshell, in the context of network namespace ns0
ip a a 10.1.1.1/24 dev enp23s0
ip link set up enp23s0 # Bring up the interface (will be brought down when moved into the namespace)
exit # Don't forget to exit the subshell before moving on
  1. Repeat step 3 for the remaining interfaces/namespaces:
ip netns exec ns1 ip a a 10.1.1.2/24 dev enp101s0 # Can execute single commands at a time for a given namespace
ip netns exec ns1 ip link set up enp101s0
  1. In the context of either namespace, send traffic between interfaces and verify using an external device/libexanic that traffic really is being physically sent/received on each interface.
ip netns exec ns0 ping 10.1.1.2

ip netns exec ns0 exanic-capture -i exanic0:0 -w - | tcpdump -r -
reading from file -, link-type EN10MB (Ethernet)
14:58:12.358488 IP 10.1.1.2 > 10.1.1.1: ICMP echo reply, id 27967, seq 1, length 64
14:58:13.357655 IP 10.1.1.2 > 10.1.1.1: ICMP echo reply, id 27967, seq 2, length 64
14:58:14.357654 IP 10.1.1.2 > 10.1.1.1: ICMP echo reply, id 27967, seq 3, length 64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment