How to forward network ports to systemd-nspawn containers

Published: Sunday, Aug 23, 2015 Last modified: Thursday, Jul 2, 2020

Assuming you’ve setup ~/containers/ssh to start a sshd on boot:

sudo systemd-nspawn -D ~/containers/ssh -n -b -p 23:22

The magic is the --port aka -p switch that allows port forwarding from the host to the container.

So when I ssh -p 23 $host I get to my ssh container, since it’s redirected to port 22. Sadly ssh does not send the hostname as part of the SSH protocol as discussed in