Introduction
Let’s say you wanna deploy a Gitea instance within an unprivileged LXC container, and use the SSH functionality (pretty handy with Git, isn’t it ?).
With a classic configuration, you’ll face an “issue” : The presence of two SSH servers (one for your host, one for the container, mainly used by Gitea).
So let’s see if we could set up an architecture allowing us to forward specific SSH queries to the Gitea container from the SSH server running on the host.
This would let us achieve these objectives :
-
Keep only one instance of SSHd open on Internet (sysadmin laziness) ;
-
Keep only the
22
port open for both use cases (KISS) ; -
Configure only one instance of Fail2Ban (sysadmin laziness++) ;
-
Keep only one SSHd to harden (Internet is still an untrusted entity for wandering connections).
The procedure
First, in your Gitea container
Just follow the regular Gitea install guide, and add an SSH key to your account once it’s done (Settings > SSH / GPG Keys > Manage SSH Keys
).
Now you have two options here :
-
Enable the built-in SSH server of Gitea on a port > 1024 if your instance is running with a regular user (something I really invite you to consider, for security purposes)
-
Use the SSH server shipped within your container (give a try to the
ssh.service
systemd unit)
Next, on the host
As root
:
Before going back in the Gitea container to polish up the setup, we have to make the host keep the Gitea public identities synchronized within the fake git
session (to accept incoming connections).
For this, we cron a specific task (Proxmox 5 example) :
Pro tip : You can even deny password authentication for the
git
user on your host, see below :
Finally, back in your Gitea container
Whatever the SSH server you chose before, you only have to add the public key of the host git
session (the one you copied previously) to /home/git/.ssh/authorized_keys
, as done below :
The idea is to allow SSH connections from your host git
session to the container git
one.
Conclusion
This post has been mainly inspired from this guide, where the author detailed an interesting setup, with Docker and mounted volumes.
Unfortunately, I couldn’t manage to get something similar working with LXC, mainly due to the difficult aspect of {u,g}ids mapping for mounted endpoints within unprivileged containers…
As always, tell me what you think about this setup, and how it could be improved if you experience(d) a similar situation on your side.