For individual containers —
For a container named c with a virtual interface ve-c,
- Create a systemD network configuration file in your host machine ...
; /etc/systemd/network/N-ve-c.network [Match] Name=ve-c Driver=veth [Network] ; Assign an address and subnet to this interface; the IP used will be the gateway for containers Address=169.254.0.1/24 ; Turn this on to make IP packets originating in the container be "masked" with the IP of the host; this allows the container to reach the public internet through the host's other interfaces ; IPMasquerade=yes
... assigning it an IP and a subnet, and instructing it to operate a DHCPServer on that interface
- Create a systemD network configuration file on the container ...
; /etc/systemd/network/1-host0.network [Match] Virtualization=container Name=host0 [Network] ; Address this container will have, must be in the subnet assigned to this container's interface on the host Address=169.254.0.2/24 ; Address assigned to this container's interface on the host Gateway=169.254.0.1 ; The DNS server to use; only needed if IPMasquerade is on ; DNS=1.1.1.1
... assigning it an IP and the subnet assigned on the host-side, and informing it of what the IP of its gateway is.
Ensure that systemd-networkd is the only network manager running on the host and the container: systemctl start --enable systemd-networkd.service
Restart systemd-networkd on your host machine: systemctl restart systemd-networkd
Start the container: machinectl start c
The container should have had a single static IP address assignd to it: machinectl
MACHINE CLASS SERVICE OS VERSION ADDRESSES c container systemd-nspawn arch - 169.254.0.2… 1 machine listed.
To network between containers —
If you want your containers to be able to talk to each other as well, bridge their interfaces
For containers named c1, c2, c3, ... with virtual interfaces ve-c1, ve-c2, ve-c3, ...
- Create a bridge device on your host machine:
; /etc/systemd/network/N-br-container-group.netdev [NetDev] Name=br-container-group Kind=bridge
- Instruct
systemd-nspawn to bridge the virtual interfaces created for each machine to the specified network bridge:
; /etc/systemd/nspawn/c{1,2,3,...}.nspawn [Network] Bridge=br-container-group
- Continue as before, assigning an IP and subnet to the interface:
; /etc/systemd/network/N-br-container-group.network [Match] Name=br-container-group ; We are no longer using a veth driver, so ask systemd-networkd to match a bridge device instead ; Driver=veth Kind=bridge ...
Note: you do not need to create a systemD network configuration file for the virtual interfaces of the containers like before; the required configuration will have been performed by systemd-nspawn on encountering the Bridge=br-container-group instruction.
- And instructing each container on what IP and gateway to use, as before; the containers are not aware that they are bridged, so the container-side configuration is identical to the previous case.
Note: Each container shares the same gateway
Note: Each container has a different static IP address, each of which must nevertheless come from the same subnet assigned to the bridge interface
Ensure systemd-networkd is running, and start the container as before
The containers can now communicate with each other, you can confirm this by running a lightweight HTTP server on each container and sending requests between them