1
I am learning about network namespaces. I created two network namespaces - red and blue - on my system and attached a veth pair to connect between them - veth-red and veth-blue. I assign IP addresses to each of those interfaces - 192.168.1.1 and 192.168.1.2. I then bring the interfaces up. However they can't ping each other unless I specify a Default Gateway. I though you only need to specify a default gateway if they are on different networks. The code is here:
# Create network namespaces
ip netns add red
ip netns add blue
# Create veth pair
ip link add veth-red type veth peer name veth-blue
# Add veth to respective namespaces
ip link set veth-red netns red
ip link set veth-blue netns blue
# Set IP Addresses
ip -n red addr add 192.168.1.1 dev veth-red
ip -n blue addr add 192.168.1.2 dev veth-blue
# Bring up interfaces
ip -n red link set veth-red up
ip -n blue link set veth-blue up
# Bring up Loopback interfaces
ip -n red link set lo up
ip -n blue link set lo up
I then try to ping blue from red
# Ping test
ip netns exec red ping 192.168.1.2
But it fails with the error:
connect: Network is unreachable
I then add default gateway for both the namespaces:
# Add default gateway
ip netns exec red ip route add default via 192.168.1.1 dev veth-red
ip netns exec blue ip route add default via 192.168.1.2 dev veth-blue
The ping then works.
My question is, why do I have to add a default gateway if they are on the same network?
Thank you! This was bugging me, too. – Lee Gaines – 2019-12-11T22:39:47.410