Preserve SSH Host Keys in a mounted volume
masterBy default, host keys are generated inside the container and change every time the container is recreated, which may cause Jenkins to lose trust in the host. To prevent this, you can persist host keys in a mounted volume (e.g., /mnt/agent).
Implementation Pattern:
- Create a directory in the mounted volume for host keys (e.g.,
/mnt/agent/host_keys/). - In your entrypoint script, copy existing keys from the volume to
/etc/ssh/. - Run
ssh-keygen -Ato ensure all keys are generated. - Copy the keys back from
/etc/ssh/to the volume to persist them for the next run.
#!/bin/bash
# /mnt/agent is the mounted volume
mkdir -p /mnt/agent/host_keys/
cp -u /mnt/agent/host_keys/ssh_host*_key* /etc/ssh/
ssh-keygen -A
cp -u /etc/ssh/ssh_host*_key* /mnt/agent/host_keys/
setup-ssd "$@"