Giter Site home page Giter Site logo

playing-with-linux-network-namespaces's Introduction

Playing ๐ŸŽฎ with Linux Network Namespaces

Objectives

Main tasks

  • create new network namespaces
  • connect two network namespaces via virtual ethernet (veth)
  • connect to a python HTTP server on a separate namespace
  • connect two network namespaces via a virtual switch bridge
  • use IP masquerading to establish an ingress connection

Meta tasks

  • docker test environment setup
  • apply color in bash at docker startup
  • run container forever
  • test network types
Connect Two Namespaces Using VETH- TL;DR ๐Ÿ‘‡

Outline for basic command and process.

Note

Please prefix following commands with sudo if we're not logged in as a root user.

  1. Create a new network namespace
ip netns add <NAMESPACE_NAME>
  1. Create a veth cable and assign an interface to a particular namespace
ip link add <INTERFACE_NAME> type veth peer name <OTHER_INTERFACE_NAME>
ip link set <INTERFACE_NAME> netns <NAMESPACE_NAME>

Note

This step must be done after the interface assigning to a namespace is complete.

Now, enter into one of the namespaces

ip netns exec <NAMESPACE_NAME> bash
  1. Assign an IP address to an interface
ip addr add <SUBNET_WITH_CIDR> dev <INTERFACE_NAME>
  1. Bring up the interface
ip link set dev <INTERFACE_NAME> up
  1. Configure route

If we just want the namespace to send packets to the server that is making the request, we need to configure routes in both namespaces as follows -

ip route add default via <GATEWAY_IP> dev <INTERFACE_NAME>
  1. Test with ping
ping <OTHER_NAMESAPCE_IP>

We can also specify the interface

ping -I <INTERFACE> <OTHER_NAMESAPCE_IP> # ๐Ÿ‘ˆ from the other namespace
tcpdump -v -i <OTHER_INTERFACE> # ๐Ÿ‘ˆ from the other namespace

Find the step-by-step example to connect two network namespaces here.

Connect Two Namespaces Using Bridge - TL;DR ๐Ÿ‘‡

Outline for basic command and process.

Note

Please prefix following commands with sudo if we're not logged in as a root user.

  1. Create a new network namespace
ip netns add <NAMESPACE_NAME>
  1. Create a bridge
ip link add <BRIDGE_NAME> type bridge
  1. Create veth cables and assign interfaces
ip link add <INTERFACE_NAME> type veth peer name <BRIDGE_INTERFACE_NAME>
ip link set <INTERFACE_NAME> netns <NAMESPACE_NAME>
ip link set <BRIDGE_INTERFACE_NAME> master <BRIDGE_NAME>
  1. Assign an IP address to the Bridge interface
ip addr add <SUBNET_WITH_CIDR> dev <BRIDGE_NAME>
  1. Assign an IP address to a namespace interface
ip netns exec <NAMESPACE_NAME> ip addr add <SUBNET_WITH_CIDR> dev <INTERFACE_NAME>
  1. Bring up the interface
ip link set dev <BRIDGE_INTERFACE_NAME> up
ip link set dev <BRIDGE_NAME> up
ip netns exec <NAMESPACE_NAME> ip link set dev <INTERFACE_NAME> up
  1. Configure route

If we just want the namespace to send packets to the server that is making the request, we need to configure routes in both namespaces as follows -

ip route add default via <GATEWAY_IP> dev <INTERFACE_NAME>
  1. Test with ping
ping -I <INTERFACE> <OTHER_NAMESAPCE_IP> # ๐Ÿ‘ˆ from the other namespace
tcpdump -v -i <OTHER_INTERFACE> # ๐Ÿ‘ˆ from the other namespace

Find the step-by-step example to connect two network namespaces here.

playing-with-linux-network-namespaces's People

Contributors

umarfchy avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.