Skip to content

Commit

Permalink
sshd: generate a custom sshd_config
Browse files Browse the repository at this point in the history
Instead of relying on the host's sshd configuration, generate a custom
sshd_config to make sure that all the options required by virtme-ng are
enabled.

Signed-off-by: Andrea Righi <[email protected]>
  • Loading branch information
arighi committed Dec 31, 2024
1 parent f53c692 commit 8772f07
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions virtme/guest/virtme-sshd-script
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,37 @@ SSH_AUTH_KEYS="${SSH_HOME}/.ssh/authorized_keys"
if [ "$(stat -f -c "%t" "${SSH_AUTH_KEYS}")" = "${OVERLAYFS}" ]; then
cat "${SSH_HOME}"/.ssh/id_*.pub >> "${SSH_AUTH_KEYS}" 2>/dev/null
chown "${virtme_ssh_user}" "${SSH_AUTH_KEYS}" 2>/dev/null
chmod 600 "${SSH_HOME}/.ssh/authorized_keys" 2>/dev/null
fi

# Generate ssh host keys (if they don't exist already).
CACHE_DIR=${SSH_HOME}/.cache/virtme-ng/.ssh
mkdir -p "${CACHE_DIR}/etc/ssh"
ssh-keygen -A -f "${CACHE_DIR}"

# Generate a minimal sshd config.
SSH_CONFIG=/etc/ssh/sshd_config
if [ "$(stat -f -c "%t" "${SSH_CONFIG}")" = "${OVERLAYFS}" ]; then
ssh_dir=$(dirname "${SSH_CONFIG}")
mkdir -p "${ssh_dir}"
cat << EOF > "${SSH_CONFIG}"
# This file is automatically generated by virtme-ng.
Port 22
PermitRootLogin yes
AuthorizedKeysFile .ssh/authorized_keys
PubkeyAuthentication yes
UsePAM yes
PrintMotd no
EOF
fi

# Start sshd.
ARGS=()
for key in "${CACHE_DIR}"/etc/ssh/ssh_host_*_key; do
ARGS+=(-h "${key}")
done

# Start sshd.
mkdir -p /run/sshd
rm -f /var/run/nologin
/usr/sbin/sshd "${ARGS[@]}"

/usr/sbin/sshd -f "${SSH_CONFIG}" "${ARGS[@]}"

0 comments on commit 8772f07

Please sign in to comment.