My scenario is this: I want to be able to execute a program in a separate namespace with a network interface of its own. But I also want it to be able to access internet though my physical network interface. This is my code so far:ip netns add private_nsip link add link eth1 name eth1.100 type vlan id 100ip link set eth1.100 netns private_nsip netns exec private_ns ip addr add 10.1.1.1/24 dev eth1.100ip netns exec private_ns ip link set eth1.100 upThen I try:ip netns exec private_ns ping 8.8.8.8And I get connect: Network is unreachable
Connect network namespace to internet
linux;networking;namespace
Each network namespace is a separate logical copy of the network stack. So you need to treat a network namespace as if it was a separate machine.To connect a new machine to the internet via your current machine you wouldSelect an unused network card in each machine (or install a new one).Connect them together with a network cable.Choose between Ethernet bridging, plain IP routing or NAT.4a. For Ethernet bridging add the new network card in the existing machine to an appropriate bridge and give the network card in the secondary machine an IP in the subnet the bridge is on.4b. For conventional IP routing choose an IP block for the new link. Assign the interfaces IPs from that block, make sure IP forwarding is enabled on your existing machine, make sure the rest of your network knows about the new subnet, and make sure your iptables rules (if any) allow the traffic to flow.4c. For NAT choose an IP block for the new link. Assign the interfaces IPs from that block, make sure IP forwarding is enabled on your existing machine, make sure your IPtables rules allow the traffic flow and put in place a SNAT or MASQURADE rule for the traffic from your new subnet to external hosts.Setup an appropriate default gateway on the new machine.Similarly when working with a network namespace.Create a veth pair in the main network namespace.Move one end of the veth pair to the secondary network namespace.Choose between Ethernet bridging, plain IP routing or NAT.4a. For Ethernet bridging add the new veth in the main network namespace to an appropriate bridge and give the veth in the new network namespace an IP in the subnet the bridge is on.4b. For conventional IP routing choose an IP block for the new link. Assign the interfaces IPs from that block, make sure IP forwarding is enabled on your existing machine, make sure the rest of your network knows about the new subnet, and make sure your iptables rules (if any) allow the traffic to flow.4c. For NAT choose an IP block for the new link. Assign the interfaces IPs from that block, make sure IP forwarding is enabled on your existing machine, make sure your IPtables rules allow the traffic flow and put in place a SNAT or MASQURADE rule for the traffic from your new subnet to external hosts.Setup an appropriate default gateway in the secondary network namespace.(sorry about the ugly formatting, feel free to fix it if you have more skills with markdown than I do)