Skip to content

Instantly share code, notes, and snippets.

@shpark
Created October 26, 2021 13:30
Show Gist options
  • Save shpark/950fdfa179073de2c6572da54ea80a66 to your computer and use it in GitHub Desktop.
Save shpark/950fdfa179073de2c6572da54ea80a66 to your computer and use it in GitHub Desktop.

Linux netns cheat sheet

TL;DR. You first create a network namesmace (netns), and a veth device (you also create a peer). Then you assign one end to another netns. You can talk to another netns via the veth.

TODO: How to use a different device instead of veth for communication across netns?

Create a new netns

ip netns add ns1

Run command on behalf of a netns

ip netns exec ns1 ...

Create a veth in one netns

ip netns exec ns1 ip link add veth1 type veth peer name veth2

Assign a veth end to another netns

ip link set veth2 netns ns2

You can also create veth in the default (or global) netns.

Assign IP addresses

ip addr add 192.168.1.1/24 dev veth1
ip netns exec ns2 ip addr add 192.168.1.100/24 dev veth2

Ping

ping 192.168.1.100 # or
ip netns exec ns2 ping add 192.168.1.1

Send ping to the IP address of the peer (that is assigned within another netns).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment