Linux Namespaces isolieren Prozesse voneinander. Sie sind zusammen mit cgroups die Grundlage für Container-Technologien wie Docker und LXC.

Namespace-Typen

| Namespace | Flag | Isoliert | |-----------|------|----------| | Mount | CLONE_NEWNS | Dateisysteme | | UTS | CLONE_NEWUTS | Hostname | | IPC | CLONE_NEWIPC | Inter-Process Communication | | Network | CLONE_NEWNET | Netzwerk-Stack | | PID | CLONE_NEWPID | Prozess-IDs | | User | CLONE_NEWUSER | User/Group IDs | | Cgroup | CLONE_NEWCGROUP | cgroup-Hierarchie | | Time | CLONE_NEWTIME | System-Zeit (ab Kernel 5.6) |

Namespaces anzeigen

Aktuelle Namespaces

# Eigene Namespaces
ls -la /proc/self/ns/

# Ausgabe:
# lrwxrwxrwx 1 root root 0 Jan 26 10:00 cgroup -> 'cgroup:[4026531835]'
# lrwxrwxrwx 1 root root 0 Jan 26 10:00 ipc -> 'ipc:[4026531839]'
# lrwxrwxrwx 1 root root 0 Jan 26 10:00 mnt -> 'mnt:[4026531840]'
# lrwxrwxrwx 1 root root 0 Jan 26 10:00 net -> 'net:[4026531992]'
# lrwxrwxrwx 1 root root 0 Jan 26 10:00 pid -> 'pid:[4026531836]'
# lrwxrwxrwx 1 root root 0 Jan 26 10:00 user -> 'user:[4026531837]'
# lrwxrwxrwx 1 root root 0 Jan 26 10:00 uts -> 'uts:[4026531838]'

# Für bestimmten Prozess
ls -la /proc/1234/ns/

lsns - Namespaces auflisten

# Alle Namespaces
lsns

# Bestimmter Typ
lsns -t net
lsns -t pid

# Mit Prozess-Info
lsns -p 1234

unshare - Namespaces erstellen

UTS-Namespace (Hostname)

# Neuer Hostname-Namespace
unshare --uts bash

# Hostname ändern (nur in diesem Namespace)
hostname isolated-host
hostname  # zeigt: isolated-host

# Im Host-System bleibt Hostname unverändert

PID-Namespace

# Neuer PID-Namespace
unshare --pid --fork --mount-proc bash

# Prozesse anzeigen
ps aux  # Zeigt nur Prozesse im Namespace

# PID 1 ist jetzt die bash
echo $$  # 1

Network-Namespace

# Neuer Netzwerk-Namespace
unshare --net bash

# Interfaces anzeigen (nur lo)
ip link

# lo aktivieren
ip link set lo up

Mount-Namespace

# Neuer Mount-Namespace
unshare --mount bash

# Mounts sind isoliert
mount --bind /tmp /mnt
# Nicht im Host sichtbar

User-Namespace (ohne Root)

# User-Namespace erstellen (unprivilegiert)
unshare --user --map-root-user bash

# Jetzt "root" im Namespace
id  # uid=0(root) gid=0(root)

# Aber nur innerhalb des Namespace

Kombiniert

# Volle Container-ähnliche Isolation
unshare --user --map-root-user --pid --fork --mount-proc --net --uts bash

nsenter - In Namespace wechseln

In Container wechseln

# In alle Namespaces eines Prozesses
nsenter -t 1234 -a bash

# Bestimmte Namespaces
nsenter -t 1234 --net --pid bash

# Nur Network-Namespace
nsenter -t 1234 -n ip addr

Docker-Container

# Container-PID finden
docker inspect --format '{{.State.Pid}}' container_name

# In Container wechseln
nsenter -t $(docker inspect --format '{{.State.Pid}}' container_name) -a bash

ip netns - Netzwerk-Namespaces

Namespace verwalten

# Namespace erstellen
ip netns add myns

# Auflisten
ip netns list

# Löschen
ip netns delete myns

Befehle ausführen

# Befehl im Namespace
ip netns exec myns ip link

# Shell im Namespace
ip netns exec myns bash

Veth-Pair erstellen

# Namespace erstellen
ip netns add ns1

# Veth-Pair erstellen
ip link add veth0 type veth peer name veth1

# veth1 in Namespace verschieben
ip link set veth1 netns ns1

# IP-Adressen zuweisen
ip addr add 10.0.0.1/24 dev veth0
ip link set veth0 up

ip netns exec ns1 ip addr add 10.0.0.2/24 dev veth1
ip netns exec ns1 ip link set veth1 up
ip netns exec ns1 ip link set lo up

# Ping testen
ping 10.0.0.2
ip netns exec ns1 ping 10.0.0.1

Bridge für mehrere Namespaces

# Bridge erstellen
ip link add br0 type bridge
ip link set br0 up
ip addr add 10.0.0.1/24 dev br0

# Namespace 1
ip netns add ns1
ip link add veth1 type veth peer name veth1-br
ip link set veth1 netns ns1
ip link set veth1-br master br0
ip link set veth1-br up
ip netns exec ns1 ip addr add 10.0.0.11/24 dev veth1
ip netns exec ns1 ip link set veth1 up
ip netns exec ns1 ip link set lo up
ip netns exec ns1 ip route add default via 10.0.0.1

# Namespace 2
ip netns add ns2
ip link add veth2 type veth peer name veth2-br
ip link set veth2 netns ns2
ip link set veth2-br master br0
ip link set veth2-br up
ip netns exec ns2 ip addr add 10.0.0.12/24 dev veth2
ip netns exec ns2 ip link set veth2 up
ip netns exec ns2 ip link set lo up
ip netns exec ns2 ip route add default via 10.0.0.1

Praktische Anwendungen

Einfacher Container

#!/bin/bash
# simple-container.sh

# Namespaces erstellen
unshare --user --map-root-user --pid --fork --mount-proc --net --uts \
    /bin/bash -c '
        hostname container
        ip link set lo up
        echo "Im Container: $(hostname), PID $$"
        exec /bin/bash
    '

Isolierter Service

# Service in eigenem Network-Namespace
ip netns add myservice
ip netns exec myservice /usr/bin/myservice

Netzwerk-Test

# Isoliertes Netzwerk für Tests
ip netns add test
ip netns exec test iptables -L  # Eigene Firewall-Regeln

Sicherheit

User-Namespaces

# Unprivilegierte Container erlauben
echo 1 > /proc/sys/kernel/unprivileged_userns_clone

# Für bessere Sicherheit:
# User-Namespace + andere Namespaces kombinieren

Namespace-Grenzen

# Maximale User-Namespaces
cat /proc/sys/user/max_user_namespaces

# Limits anpassen
echo 15000 > /proc/sys/user/max_user_namespaces

Zusammenfassung

| Namespace | Befehl | Isolation | |-----------|--------|-----------| | UTS | unshare --uts | Hostname | | PID | unshare --pid --fork | Prozesse | | Network | unshare --net | Netzwerk | | Mount | unshare --mount | Dateisysteme | | User | unshare --user | UIDs/GIDs | | IPC | unshare --ipc | Shared Memory |

| Befehl | Funktion | |--------|----------| | unshare | Neuen Namespace erstellen | | nsenter | In Namespace wechseln | | lsns | Namespaces auflisten | | ip netns | Netzwerk-Namespaces verwalten |

| Datei | Beschreibung | |-------|--------------| | /proc/PID/ns/ | Namespace-Links | | /var/run/netns/ | Named Network-Namespaces |

Fazit

Namespaces sind fundamental für Container-Isolation. Sie ermöglichen leichtgewichtige Virtualisierung. Network-Namespaces sind ideal für Netzwerk-Tests. User-Namespaces erhöhen die Sicherheit. Das Verständnis von Namespaces hilft beim Container-Debugging.