Linux processes usually share the machine's network stack. A named network namespace created with iproute2 gives selected processes their own interfaces, routes, and listening ports for testing on the same machine.

Loopback lets processes within the namespace communicate on localhost. Reaching another network requires a link to it, such as a virtual Ethernet connection.

The name remains available for later use until the namespace is deleted or Linux reboots. Creating and entering it with iproute2 requires root privileges, which an administrator can obtain with sudo.

Steps to create a network namespace with iproute2:

  1. Create a network namespace, using your own unused name in place of sandbox throughout.
    $ sudo ip netns add sandbox

    If the command reports File exists, rerun step 1 with a different unused name and continue only after it succeeds.

  2. Bring up the loopback interface lo inside sandbox.
    $ sudo ip netns exec sandbox ip link set dev lo up

    The loopback starts down in a new namespace. ip netns exec runs the command after the namespace name inside that namespace.

  3. Check the interface list inside sandbox for the UP flag on lo.
    $ sudo ip netns exec sandbox ip link show
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    ##### snipped #####

    The UP flag shows that loopback is enabled. An operational state of UNKNOWN is normal for loopback. The list comes from sandbox and can include tunnel interfaces created there by the kernel.
    Related: How to show interfaces with ip link

Share this guide