#!/usr/bin/env bash set -ex PATH=$PATH:/usr/sbin:/usr/bin # Ohne for-Schleifen: # ip netns del host0 &>/dev/null || true # ip link del veth0 &>/dev/null || true # ip netns del host1 &>/dev/null || true # ip link del veth1 &>/dev/null || true # ip link add veth0 type veth peer name veth1 # ip netns add host0 # ip link set veth0 netns host0 name eth0 up # ip -n host0 addr add 192.168.0.100/24 dev eth0 # ip -n host0 link set lo up # ip netns add host1 # ip link set veth1 netns host1 name eth0 up # ip -n host1 addr add 192.168.0.101/24 dev eth0 # ip -n host1 link set lo up # ip netns exec host0 ping -c1 192.168.0.100 &>/dev/null # ip netns exec host0 ping -c1 192.168.0.101 &>/dev/null # ip netns exec host1 ping -c1 192.168.0.100 &>/dev/null # ip netns exec host1 ping -c1 192.168.0.101 &>/dev/null # Altlasten aufräumen for i in 0 1; do ip netns del host$i &>/dev/null || true ip link del veth$i &>/dev/null || true done # Neues virtuelles Kabel erschaffen ip link add veth0 type veth peer name veth1 for i in 0 1; do # Container host$i erschaffen ip netns add host$i # Kabelende veth$i in host$i einstecken ip link set veth$i netns host$i name eth0 up ip -n host$i addr add 192.168.0.10$i/24 dev eth0 ip -n host$i link set lo up done for i in 0 1; do for j in 0 1; do ip netns exec host$i ping -c1 192.168.0.10$j &>/dev/null done done # 1. Debian-Linux installieren (2x) if ! [ -e /var/lib/machines/debian ]; then debootstrap --include=systemd,dbus stable /var/lib/machines/debian systemd-nspawn -D /var/lib/machines/debian -E PATH=$PATH -- bash -c " apt install -y wireguard " fi # Kai-Frage: -E PATH=$PATH rm -rf /var/lib/machines/host0 rm -rf /var/lib/machines/host1 cp --reflink=auto -a /var/lib/machines/debian /var/lib/machines/host0 cp --reflink=auto -a /var/lib/machines/debian /var/lib/machines/host1 # Standardmäßig heißen die Container genauso wie das Gastgebersystem. # Das ist verwirrend. Die folgenden zwei Befehle benennen die Container um. echo host0 > /var/lib/machines/host0/etc/hostname echo host1 > /var/lib/machines/host1/etc/hostname # 2. WireGuard-Schlüsselmaterial erstellen (2x) systemd-nspawn -D /var/lib/machines/host0 -E PATH=$PATH -- bash -c " cd /etc/wireguard wg genkey | tee privatekey | wg pubkey > publickey " systemd-nspawn -D /var/lib/machines/host1 -E PATH=$PATH -- bash -c " cd /etc/wireguard wg genkey | tee privatekey | wg pubkey > publickey " # 4. WireGuard einrichten (2x) cat >/var/lib/machines/host0/etc/wireguard/wg0.conf </var/lib/machines/host1/etc/wireguard/wg0.conf <