How to forward network ports to systemd-nspawn containers

Published: Sunday, Aug 23, 2015 Last modified: Tuesday, Nov 26, 2024

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 http://serverfault.com/questions/34552/is-there-a-name-based-virtual-host-ssh-reverse-proxy.