How does a network namespace get connectivity?
AdvancedConceptKernel Parameters, Namespaces & cgroups
Answer
A new netns has only a down loopback. You connect it with a veth pair: one end inside, one on the host, usually attached to a bridge, plus routing/NAT.
sudo ip netns add ns1
sudo ip link add veth-host type veth peer name veth-ns
sudo ip link set veth-ns netns ns1
sudo ip addr add 10.200.0.1/24 dev veth-host && sudo ip link set veth-host up
sudo ip netns exec ns1 ip addr add 10.200.0.2/24 dev veth-ns
sudo ip netns exec ns1 ip link set veth-ns up
sudo ip netns exec ns1 ping -c1 10.200.0.1
This is exactly what Docker's bridge network and many CNI plugins do.
More on Kernel Parameters, Namespaces & cgroups
What is sysctl and how do you make a change persistent?BeginnerWhy does Kubernetes require `net.ipv4.ip_forward=1` and `br_netfilter`?AdvancedAre sysctl settings global, or per namespace?AdvancedWhat are Linux namespaces? List all types.BeginnerHow do you create and enter namespaces by hand?AdvancedWhat does the user namespace enable?Advanced