Skip to content

Instantly share code, notes, and snippets.

@toke
Last active July 17, 2018 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toke/997c76e184bd814ac4b7a29c9fb9c41e to your computer and use it in GitHub Desktop.
Save toke/997c76e184bd814ac4b7a29c9fb9c41e to your computer and use it in GitHub Desktop.
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
Copy link
Author

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