Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example Script for using network namespaces
#!/bin/bash
set -e
# Network namespace example
# Inspired by https://lwn.net/Articles/580893/
#
NNS="netns1"
DEV_OUTSIDE=veth0
DEV_INSIDE=veth1
IP_INSIDE=fec0::1/64
IP_OUTSIDE=fec0::2/64
ip netns add $NNS
ip link add $DEV_OUTSIDE type veth peer name $DEV_INSIDE
ip link set $DEV_INSIDE netns $NNS
ip netns exec $NNS ip link set dev $DEV_INSIDE up
ip netns exec $NNS ip addr add $IP_INSIDE dev $DEV_INSIDE
ip link set dev $DEV_OUTSIDE up
ip add add $IP_OUTSIDE dev $DEV_OUTSIDE
ip netns exec $NNS /usr/sbin/sshd -p 22
echo "you now can connect to fec0::1 via ssh"
@toke

This comment has been minimized.

Owner

toke commented Jul 17, 2018

Interesting use cases:

  • start a vpn inside that namespace and you have isolated your host.
  • use it for services that should only be available locally
  • use it for routing tests
  • isolate applications i.e. having a local DNS resolver and a DNS server running locally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment