Steps to create a pair of namespace and connect them.
- Create the namespaces using
# sudo ip netns add <ns_name>sudo ip netns add ns1sudo ip netns add ns2- Connect these namespaces using a virtual ethernet (created in pairs) using:
#sudo ip link add <if_name> netns <ns_name> type veth peer name <peer_name> netns <peer_ns>$ sudo ip link add veth0 netns ns1 type veth peer name veth1 netns ns2This will create veth0 in ns1 and veth1 in ns2.
- Assign IP addresses to these veth pairs using :
# sudo ip -n <ns_name> addr add <ip/mask> dev <if_name>sudo ip -n ns1 addr add 10.1.1.1/24 dev veth0sudo ip -n ns2 addr add 10.1.1.2/24 dev veth1UPthe interfaces using :
# sudo ip -n <ns_name> link set <if_name> upsudo ip -n ns1 set veth0 upsudo ip -n ns2 set veth1 up- Exec into the namespaces in different terminals and ping each other :
sudo ip netns exec ns1 ping 10.1.1.2 # in one terminalsudo ip netns exec ns2 ping 10.1.1.1 # in another terminalYou can also exec bash in any namespace and execute commands interactively in that namespace :
sudo ip netns exec ns1 /bin/bash# you will get a terminal now