Linux ping network namespace

linuxnetworkingping

I added namespace to my host-machine and made it available by using this:

ip netns add h1
ip link add veth01 type veth peer name veth1
ip link set dev veth1 netns h1
ifconfig veth01 10.0.0.1/24 up
ip net e h1 ifconfig veth1 10.0.0.2/24 up
route add -net 10.0.0.0/24 gw 10.0.0.1 dev veth01
ip net e h1 ip r a default via 10.0.0.1

Now I can ping 10.0.0.2 from my host and backward ( ip net e h1 ping 10.0.0.1 ).

Then I did the same with the second namespace:

ip netns add h2
ip link add veth02 type veth peer name veth2
ip link set dev veth2 netns h2
ifconfig veth02 20.0.0.1/24 up
ip net e h2 ifconfig veth2 20.0.0.2/24 up
route add -net 20.0.0.0/24 gw 20.0.0.1 dev veth02
ip net e h2 ip r a default via 20.0.0.1

Now e.g. I can ping 10.0.0.1 (and 20.0.0.1) from h1 (10.0.0.2); can ping 20.0.0.2 from host, but I can't ping 20.0.0.2 from h1. What am I doing wrong?

Thanks in advance

Best Answer

What am I doing wrong?

Adding route — it's wrong because you've assigned this network per interface pair already.

ifconfig veth01 10.0.0.1/24 up
…
route add -net 10.0.0.0/24 gw 10.0.01 dev veth01

Another possible culprit people often tend forgetting is enabling forwarding per se (sysctl net.ipv4.ip_forward=1).