Skip to content

Instantly share code, notes, and snippets.

@mtds
Last active October 28, 2026 14:29
Show Gist options
  • Select an option

  • Save mtds/4c4925c2aa022130e4b7c538fdd5a89f to your computer and use it in GitHub Desktop.

Select an option

Save mtds/4c4925c2aa022130e4b7c538fdd5a89f to your computer and use it in GitHub Desktop.
Linux Virtual Networking

Virtual Networking on Linux

Last reviewed: October 2026 · Tested on: Debian 12.15, Kernel version 6.1.0-53-amd64, iproute2: 6.1.0-3

Conventions: # = command run as root (or with sudo), $ = command run as a normal user. Commands use iproute2 (ip, bridge, tc) and nftables (nft). The older ifconfig, brctl and route tools are deprecated and not used here.

Contents

  1. Introduction
  2. Types of virtual network interfaces
  3. Linux namespaces
  4. Network namespaces
  5. Connecting two namespaces with a veth pair
  6. A virtual network with namespaces, a bridge and NAT
  7. The whole lab as a single script
  8. Testing in an isolated environment
  9. Troubleshooting
  10. References

Introduction

In the Linux kernel, access to networking hardware is standardized by the socket API. A (very) simplified view of the path a packet takes:

 User space      applications
                      |  socket API
 ---------------------+------------------------------------
 Kernel space         |
               +------+---------------------------+
               | transport (TCP/UDP) + IP layer   |  <- routing, netfilter/nftables
               +------+---------------------------+
                      |
               +------+---------------------------+
               | network device layer (qdisc)     |
               +------+---------------------------+
                      |
          +-----------+-----------+
          |                       |
     +----+-----+          +------+--------+
     |  driver  |          | virtual device|   <- bridge, veth, tun/tap, ...
     +----+-----+          +---------------+
          |
     +----+-----+
     |   NIC    |
     +----------+

To support new workloads and make better use of hardware, Linux can virtualize CPU, memory, storage and networking. Virtual networking is the basis for hosting VMs and containers: a virtual device looks like a real network interface to the stack, but is implemented entirely in software.

A general overview is given in the IBM developerWorks article Virtual networking in Linux (the original page has been retired, so an archived copy is linked).

Types of virtual network interfaces

Type Layer What it is Typical use
bridge 2 Behaves like a network switch: forwards frames between the interfaces attached to it. Supports STP, VLAN filtering and multicast snooping. Connecting VMs, containers and namespaces to each other and to the host.
veth 2 A virtual Ethernet pair: what goes in one end comes out of the other (a virtual patch cable). If either end is down, the link state of the pair is down. Connecting namespaces, or a namespace to a bridge.
TUN 3 A point-to-point device that carries IP packets (IPv4 and IPv6) to a user-space program. It cannot carry non-IP protocols, cannot be added to a bridge and does not support broadcast. VPN software that needs to encrypt packets before they go on the wire.
TAP 2 Like TUN, but at the Ethernet level, so it behaves like a real NIC: it carries any layer-3 protocol, can be part of a bridge and forwards broadcast. (The name is simply "network tap", as in "wire tap".) Virtual NICs for guest VMs (QEMU/KVM), layer-2 VPNs.
macvlan / ipvlan 2 / 2–3 Multiple virtual interfaces on top of one physical NIC, each with its own MAC address (macvlan) or sharing the parent's MAC (ipvlan). Giving containers direct presence on the physical LAN without a bridge.
vlan 2 An 802.1Q sub-interface (e.g. eth0.100). Traffic separation on a trunk port.
vxlan / geneve 2 over UDP Overlay tunnels that carry Ethernet frames inside UDP. Multi-host virtual networks (Kubernetes, SDN, cloud).
wireguard 3 A kernel-native, in-kernel VPN interface (not a TUN device). Modern VPNs.
dummy – An interface that accepts and drops traffic. Stable addresses for services, testing.
bond / team 2 Aggregates several interfaces for redundancy or throughput. Server uplinks.
vrf 3 Separate routing tables on the same host (a lighter-weight cousin of network namespaces). Multi-tenant routing.
ifb – Intermediate functional block, used to shape ingress traffic with tc. Ingress shaping.
netkit 2 A BPF-programmable device pair (kernel 6.7+) designed as a faster alternative to veth for containers. Container networking (e.g. Cilium).

More types are described in this post on the Red Hat developers blog. Run ip link help to see which types your iproute2 knows about.

Examples

Create a bridge:

# ip link add br0 type bridge

Attach (enslave) an existing interface to a bridge:

# ip link set eth0 master br0

Create a veth pair (ep1 and ep2) and attach both ends to the bridge:

# ip link add ep1 type veth peer name ep2
# ip link set ep1 master br0
# ip link set ep2 master br0

Assign IP addresses. Always include the prefix length: without it the kernel assumes /32 and the two addresses cannot reach each other.

# ip addr add 10.0.0.10/24 dev ep1
# ip addr add 10.0.0.11/24 dev ep2

Interfaces start in the DOWN state, so bring them up before testing:

# ip link set br0 up
# ip link set ep1 up
# ip link set ep2 up

Show all interfaces, in a compact and colorized form:

$ ip -br -c address show

Note: a veth end that is attached to a bridge does not need an IP address and, if it has one, it is not used for bridged traffic. This example only illustrates the syntax; the complete working setup is shown below.

Linux namespaces

Namespaces are a kernel feature used as a basis for Linux Containers (LXC), Docker, Podman, Kubernetes and software-defined networking (SDN) solutions. They let a process see its own private instance of a global resource. The available namespace types are (see namespaces(7)):

  • Cgroup
  • IPC
  • Mount
  • PID
  • Time (since Linux 5.6)
  • User
  • UTS
  • Network

Useful tools for working with them:

Tool Purpose
lsns List the namespaces on the system and the processes that own them.
unshare Run a program in new namespaces (e.g. unshare --net).
nsenter Run a program inside the namespaces of an existing process.
ip netns Create and manage named network namespaces.

Network namespaces give each container (or process group) its own network stack.

Network namespaces

A network namespace gives its processes a separate view of the network:

  • Interfaces: each namespace has its own set of interfaces (and its own loopback).
  • Routes: since processes see different addresses in different namespaces, they also need different routing tables.
  • Firewall rules: rules depend on source and target interfaces, so each namespace has its own netfilter/nftables state.
  • Sockets and ports: two namespaces can both listen on port 80.

Named network namespaces are managed with the ip command from the iproute2 package.

NOTE: the commands in the following examples must be executed as root or with sudo. If you want to experiment without root privileges, see Rootless experiments.

Create, list and delete a network namespace:

# ip netns add ns1
# ip netns list
# ip netns del ns1

ns1 is a network namespace that is completely separate from the default one (which always exists after boot).

Two ways of running a command inside a namespace are equivalent:

# ip netns exec ns1 ip link show      # run any command in the namespace
# ip -n ns1 link show                 # shorthand, only for ip(8) itself

New namespaces contain only a loopback interface, which is DOWN by default:

# ip -n ns1 link set lo up

The routing table of a new namespace is empty:

# ip -n ns1 route show

A named namespace is represented by a file under /run/netns (/var/run is a symlink to /run on current systems):

# mount | grep netns
tmpfs on /run/netns type tmpfs (rw,nosuid,noexec,relatime,size=2468812k,mode=755)
nsfs on /run/netns/ns1 type nsfs (rw)

# ls -l /run/netns
[...]
-r--r--r--  1 root root    0 Jan 15 11:53 ns1

Deleting a namespace removes the interfaces that live in it. If one end of a veth pair is deleted, the other end disappears too.

DNS inside a namespace. ip netns exec does not know about the host's resolver (e.g. the 127.0.0.53 stub of systemd-resolved is not reachable from another namespace). Files in /etc/netns/<name>/ are bind-mounted over /etc/ while the command runs:

# mkdir -p /etc/netns/ns1
# echo "nameserver 1.1.1.1" > /etc/netns/ns1/resolv.conf

References:

Connecting two namespaces with a veth pair

The simplest virtual network: two namespaces and one veth pair, with each end placed in a different namespace.

 +-------------------+                +-------------------+
 |       ns1         |                |       ns2         |
 |  veth1 10.0.0.1/24|================|10.0.0.2/24  veth2 |
 +-------------------+   veth pair    +-------------------+
# ip netns add ns1
# ip netns add ns2
# ip link add veth1 netns ns1 type veth peer name veth2 netns ns2

Assign IP addresses (the /24 is the subnet prefix length):

# ip -n ns1 addr add 10.0.0.1/24 dev veth1
# ip -n ns2 addr add 10.0.0.2/24 dev veth2

Bring the interfaces up. The kernel adds the connected route 10.0.0.0/24 automatically as soon as the link is up, so no manual route is needed:

# ip -n ns1 link set veth1 up
# ip -n ns2 link set veth2 up
# ip -n ns1 route show
10.0.0.0/24 dev veth1 proto kernel scope link src 10.0.0.1

Test it:

# ip netns exec ns1 ping -c 2 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.047 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.052 ms

--- 10.0.0.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.047/0.049/0.052/0.007 ms

# ip netns exec ns2 ping -c 2 10.0.0.1

Clean up (this also removes the veth pair):

# ip netns del ns1
# ip netns del ns2

A virtual network with namespaces, a bridge and NAT

With a veth pair you can only connect two points. To connect more namespaces, use a Linux bridge, the virtual equivalent of a network switch. Building blocks:

  • a network namespace can have its own interfaces and routes;
  • veth interfaces come in pairs;
  • an interface can be moved to a different network namespace.

The topology built in this section:

                 host (default network namespace)
 +------------------------------------------------------------+
 |                                                            |
 |   +--------------------------------+                       |
 |   |  br1   192.168.1.10/24         |-----> uplink (e.g. enp5s0) ---> Internet
 |   +--------+----------------+------+     (forwarding + NAT)
 |            |                |                              |
 |       br-veth1          br-veth2        (veth ends in the host, attached to br1)
 +------------|----------------|--------------------------------+
              |                |
 +------------|---+      +-----|------------+
 | veth1 192.168.1.11 |  | veth2 192.168.1.12 |
 |      ns1           |  |      ns2           |
 | default via .10    |  | default via .10    |
 +--------------------+  +--------------------+
  • veth{1,2}: the veth ends that live inside the namespaces.
  • br-veth{1,2}: the peer ends that stay in the host and are attached to the bridge. (br-veth is only a naming convention: a name does not make an interface a bridge port, the master setting does.)

1. Create the namespaces and the veth pairs:

# ip netns add ns1
# ip netns add ns2
# ip link add veth1 type veth peer name br-veth1
# ip link add veth2 type veth peer name br-veth2

2. Move one end of each pair into its namespace:

# ip link set veth1 netns ns1
# ip link set veth2 netns ns2

veth1 and veth2 now only show up inside their respective namespaces:

# ip -n ns1 link list
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
32: veth1@if31: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 4e:8c:92:72:f5:cd brd ff:ff:ff:ff:ff:ff link-netnsid 0

3. Configure the namespace side: addresses, interfaces up (including lo), and a default route through the bridge address (without it the namespaces cannot reach anything outside their own subnet):

# ip -n ns1 addr add 192.168.1.11/24 dev veth1
# ip -n ns1 link set veth1 up
# ip -n ns1 link set lo up
# ip -n ns1 route add default via 192.168.1.10

# ip -n ns2 addr add 192.168.1.12/24 dev veth2
# ip -n ns2 link set veth2 up
# ip -n ns2 link set lo up
# ip -n ns2 route add default via 192.168.1.10

Even now the two namespaces cannot talk to each other: nothing in the host connects br-veth1 and br-veth2. That is the job of the bridge.

4. Create the bridge, attach the host-side veth ends and bring everything up:

# ip link add name br1 type bridge
# ip link set br-veth1 master br1
# ip link set br-veth2 master br1
# ip link set br1 up
# ip link set br-veth1 up
# ip link set br-veth2 up

Check that the ports are attached:

# bridge link show
# ip -br link show master br1

5. Give the bridge an IP address. This is the namespaces' gateway and the host's address on this subnet:

# ip addr add 192.168.1.10/24 dev br1

(The broadcast address 192.168.1.255 is derived automatically from the prefix length; brd + is only needed in unusual cases.)

The host now has a connected route for the subnet:

# ip route
default via 10.1.1.1 dev enp5s0
10.1.1.0/24 dev enp5s0 proto kernel scope link src 10.1.1.10
192.168.1.0/24 dev br1 proto kernel scope link src 192.168.1.10

6. Verify local connectivity. Namespaces and host can all reach each other:

# ping -c 1 192.168.1.11                       # host -> ns1
# ip netns exec ns1 ping -c 1 192.168.1.12     # ns1 -> ns2
# ip netns exec ns2 ping -c 1 192.168.1.10     # ns2 -> host (bridge address)

At this point the namespaces are isolated from the outside world: they can only talk to each other and the host.

7. Give the namespaces Internet access. Enable IPv4 forwarding on the host and masquerade (source NAT) the traffic of the subnet when it leaves through another interface:

# sysctl -w net.ipv4.ip_forward=1

With nftables (recommended). A dedicated table keeps the lab separate from your real firewall and makes cleanup a single command:

# nft add table ip lab_nat
# nft add chain ip lab_nat postrouting '{ type nat hook postrouting priority srcnat; policy accept; }'
# nft add rule ip lab_nat postrouting ip saddr 192.168.1.0/24 oifname != "br1" masquerade

Or with iptables (on many systems this is iptables-nft, a front end for the same kernel subsystem):

# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 ! -o br1 -j MASQUERADE

The rule means: in the nat table, append (-A) to the POSTROUTING chain a rule for packets from the source subnet (-s) that do not leave through the bridge (! -o br1), with action MASQUERADE (replace the source address with that of the outgoing interface).

If you also run Docker (or anything else that sets the FORWARD policy to DROP), forwarded packets are discarded and the namespaces still have no connectivity. Allow the lab traffic in the DOCKER-USER chain:

# iptables -I DOCKER-USER -i br1 -j ACCEPT
# iptables -I DOCKER-USER -o br1 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

8. Test the Internet connection by sending packets to 8.8.8.8:

# ip netns exec ns1 ping -c 2 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=61 time=43 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=61 time=35 ms
[...]

Name resolution additionally needs a per-namespace resolv.conf (see DNS inside a namespace).

9. Cleanup:

# ip netns del ns1
# ip netns del ns2
# ip link del br1
# nft delete table ip lab_nat

Deleting the namespaces also removes veth1/veth2 and, since veth ends are paired, br-veth1/br-veth2. Remember that sysctl -w changes are not persistent: to keep forwarding enabled across reboots put net.ipv4.ip_forward = 1 in a file under /etc/sysctl.d/. Namespaces, bridges and veths created with ip are not persistent either; use systemd-networkd, NetworkManager or your distribution's network configuration for permanent setups.

Notes on IPv6

The same topology works for IPv6: assign IPv6 addresses, enable net.ipv6.conf.all.forwarding=1, and use either routed prefixes or NAT66 (nft add table ip6 ...). Link-local addresses (fe80::/64) appear on every interface automatically.

The whole lab as a single script

The steps above, as one idempotent script with teardown. Save it as netns-bridge-lab.sh, make it executable and run sudo ./netns-bridge-lab.sh up, test and down.

#!/usr/bin/env bash
# netns-bridge-lab.sh - two network namespaces connected through a Linux bridge,
# with optional NAT towards the host's uplink.
#
# Usage: sudo ./netns-bridge-lab.sh {up|nat|test|down}
set -euo pipefail

BR=br-lab
BR_IP=192.168.100.1
PREFIX=24
SUBNET=192.168.100.0/${PREFIX}
NS=(lab1 lab2)
IPS=(192.168.100.11 192.168.100.12)
NFT_TABLE=lab_nat

need_root() {
  [[ $EUID -eq 0 ]] || { echo "error: run as root (sudo)" >&2; exit 1; }
}

up() {
  down                                  # start from a clean state
  ip link add "$BR" type bridge
  ip addr add "${BR_IP}/${PREFIX}" dev "$BR"
  ip link set "$BR" up

  for i in "${!NS[@]}"; do
    local ns=${NS[$i]}
    ip netns add "$ns"
    # host end "vb-<ns>" stays in the host and joins the bridge;
    # the other end is created directly inside the namespace as "eth0"
    ip link add "vb-${ns}" type veth peer name eth0 netns "$ns"
    ip link set "vb-${ns}" master "$BR" up
    ip -n "$ns" link set lo up
    ip -n "$ns" addr add "${IPS[$i]}/${PREFIX}" dev eth0
    ip -n "$ns" link set eth0 up
    ip -n "$ns" route add default via "$BR_IP"
  done
  echo "lab is up: ${NS[*]} on bridge $BR ($SUBNET)"
}

nat() {
  sysctl -qw net.ipv4.ip_forward=1
  nft -f - <<EOF
table ip ${NFT_TABLE} {
  chain postrouting {
    type nat hook postrouting priority srcnat; policy accept;
    ip saddr ${SUBNET} oifname != "${BR}" masquerade
  }
}
EOF
  echo "NAT enabled (nft table '${NFT_TABLE}')."
  echo "Note: net.ipv4.ip_forward is left enabled; reset it yourself if needed."
}

check() {                                # check "<description>" <command...>
  local desc=$1; shift
  if "$@" >/dev/null 2>&1; then echo "PASS  $desc"; else echo "FAIL  $desc"; FAILED=1; fi
}

test_lab() {
  FAILED=0
  check "host -> ${NS[0]}"        ping -c1 -W1 "${IPS[0]}"
  check "${NS[0]} -> ${NS[1]}"    ip netns exec "${NS[0]}" ping -c1 -W1 "${IPS[1]}"
  check "${NS[1]} -> bridge/host" ip netns exec "${NS[1]}" ping -c1 -W1 "$BR_IP"
  if nft list table ip "$NFT_TABLE" >/dev/null 2>&1; then
    check "${NS[0]} -> Internet (1.1.1.1)" ip netns exec "${NS[0]}" ping -c1 -W2 1.1.1.1
  fi
  return "$FAILED"
}

down() {
  nft delete table ip "$NFT_TABLE" 2>/dev/null || true
  for ns in "${NS[@]}"; do ip netns del "$ns" 2>/dev/null || true; done
  ip link del "$BR" 2>/dev/null || true
}

need_root
case "${1:-}" in
  up)   up ;;
  nat)  nat ;;
  test) test_lab ;;
  down) down ;;
  *)    echo "usage: $0 {up|nat|test|down}" >&2; exit 2 ;;
esac

Testing in an isolated environment

Experiments with routing, bridges and firewall rules can easily cut you off from your own machine. The following approaches, roughly from lightest to heaviest, keep the experiments away from the host's real network configuration.

1. Rootless experiments with unshare

unshare can create a user namespace and a network namespace at the same time. Inside it you are "root" with full control over a private network stack, but without any privileges on the host: nothing you do can affect the host's interfaces, routes or firewall.

$ unshare --user --map-root-user --net bash
# ip link set lo up
# ip link add br0 type bridge
# ip link add vA type veth peer name vB
# ip link show

A second "machine" can be a second, nested network namespace held open by a background process:

# unshare --net sleep infinity &
# PEER=$!
# sleep 0.2                                   # give unshare time to create the namespace
# ip link set vB netns $PEER                  # move one veth end into the peer
# nsenter --target $PEER --net ip link set lo up
# nsenter --target $PEER --net ip addr add 10.9.0.2/24 dev vB
# nsenter --target $PEER --net ip link set vB up
# ip addr add 10.9.0.1/24 dev vA
# ip link set vA up
# ping -c 1 10.9.0.2

Everything disappears when you leave the shell and the background process ends (kill $PEER).

Limitations and notes:

  • This namespace has no connection to the outside world. For Internet access from a rootless namespace use pasta (from the passt project, also used by rootless Podman) or slirp4netns.
  • Some distributions restrict unprivileged user namespaces (for example Ubuntu through an AppArmor setting). If unshare fails with "Operation not permitted", check your distribution's documentation for the relevant sysctl or profile.
  • You cannot use the host's physical interfaces from inside a rootless namespace.

2. Containers as disposable nodes

Containers give you a blank network namespace per node. With --network none there is nothing but loopback, and you wire the nodes yourself:

$ podman run -d --rm --network none --name n1 alpine sleep infinity
$ podman unshare nsenter --target "$(podman inspect -f '{{.State.Pid}}' n1)" --net ip a

(With rootful Docker, nsenter --target "$(docker inspect -f '{{.State.Pid}}' n1)" --net ... works the same way.)

3. Declarative multi-node topologies

For anything with more than a few nodes, describe the topology instead of scripting it:

  • containerlab: YAML-defined topologies of containers (and some VM-based router images), with automatic link creation.
  • Mininet: emulated hosts and switches built on network namespaces; very popular for SDN and protocol experiments.

4. Virtual machines

When the thing you are testing is the host's own network configuration (netplan, systemd-networkd, firewalls, bonding, reboots), use a throwaway VM rather than namespaces: QEMU/KVM with libvirt, Vagrant, Multipass, or systemd-nspawn for a lighter container-like system. Snapshots let you roll back after each experiment.

5. Make the lab reproducible

  • Put setup and teardown in one script (see above) with set -euo pipefail, and make teardown idempotent (|| true).

  • Use a trap so that cleanup also happens when a script fails or is interrupted:

    trap down EXIT
  • Use a dedicated nftables table for firewall experiments: nft delete table ... removes it without touching the rest of the ruleset.

  • To wipe all named namespaces at once: ip -all netns delete.

6. Automated tests

The lab script can be driven from a test framework, for instance pytest with a module-scoped fixture:

import subprocess
import pytest

SCRIPT = "./netns-bridge-lab.sh"

def run(*cmd):
    return subprocess.run(["sudo", *cmd], capture_output=True, text=True)

@pytest.fixture(scope="module")
def lab():
    run(SCRIPT, "up")
    yield
    run(SCRIPT, "down")

def ns_ping(ns, dst):
    return run("ip", "netns", "exec", ns, "ping", "-c1", "-W1", dst).returncode == 0

def test_ns_to_ns(lab):
    assert ns_ping("lab1", "192.168.100.12")

def test_ns_to_host(lab):
    assert ns_ping("lab2", "192.168.100.1")

For finer-grained control, libraries such as pyroute2 (Python) and vishvananda/netlink + netns (Go) create namespaces, veths and routes programmatically. CI services such as GitHub Actions offer runners with sudo, so these tests can run on every commit. bats is a good fit if you prefer shell-based tests.

7. Simulating real-world network conditions

tc with the netem discipline adds delay, jitter, loss and reordering to any interface, which makes namespaces ideal for testing how software behaves on a bad network:

# ip netns exec lab1 tc qdisc add dev eth0 root netem delay 50ms 10ms loss 1%
# ip netns exec lab1 tc qdisc show dev eth0
# ip netns exec lab1 tc qdisc del dev eth0 root

8. Inspection and debugging toolkit

Goal Command
Capture packets inside a namespace ip netns exec lab1 tcpdump -ni eth0
Which route would be used? ip -n lab1 route get 8.8.8.8
Bridge ports and learned MACs bridge link show, bridge fdb show br br-lab
Interface statistics ip -s link show, ethtool -S <if>
Listening sockets in a namespace ip netns exec lab1 ss -tulpn
Trace a packet through nftables nft monitor trace (after adding a rule with meta nftrace set 1)
Which processes are in which namespace? lsns -t net
Open a shell in a namespace ip netns exec lab1 bash

Troubleshooting

Symptom Likely cause Check / fix
Command "192.168.1.0/24" is unknown ip route without a subcommand. Use ip route add ...; for the connected route none is needed.
Ping between namespaces fails An interface is still DOWN (including lo), or the veth end is not attached to the bridge. ip -br link, bridge link show.
Namespaces cannot reach each other although both are on the bridge Bridge traffic is being filtered by iptables (br_netfilter loaded, as Docker does). sysctl net.bridge.bridge-nf-call-iptables; add an accept rule for br1.
Namespaces reach the host but not the Internet Missing default route in the namespace, forwarding disabled, no NAT rule. ip -n ns1 route, sysctl net.ipv4.ip_forward, nft list ruleset.
Forwarding enabled and NAT in place, still no connectivity The FORWARD chain has policy DROP (typically Docker). Add rules in DOCKER-USER (see above).
Internet works by IP but names do not resolve The namespace uses the host's 127.0.0.53 stub resolver. Create /etc/netns/<ns>/resolv.conf.
Replies are dropped on a multi-homed host Reverse-path filtering. sysctl net.ipv4.conf.all.rp_filter (try 2 for loose mode while debugging).
RTNETLINK answers: File exists Object from a previous run still present. Run teardown first (./netns-bridge-lab.sh down).
unshare: unshare failed: Operation not permitted Unprivileged user namespaces are disabled or restricted. Check your distribution's policy for unprivileged user namespaces.

References

@sothy

sothy commented Apr 16, 2020

Copy link
Copy Markdown

Hi,
I follow your steps and I got an error in ubuntu 18.04 LTS.
$sudo ip netns exec ns1 ip route 192.168.2.0/24 dev veth1 proto kernel scope link src 192.168.2.11
Command "192.168.2.0/24" is unknown, try "ip route help".

I changed only IP network address 192.168.1.0 ==> 192.168.2.0

@MohammadAlavi1986

Copy link
Copy Markdown

I could not follow along the example described here, I think some of the commands required to set up this network are missing but I found this article with the exact same example but with step by step instruction that you can follow along https://ops.tips/blog/using-network-namespaces-and-bridge-to-isolate-servers/

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