Skip to content

Commit

Permalink
feat: Generate ssh host keys on first boot (#140)
Browse files Browse the repository at this point in the history
This change removes the keys generated by dpkg/debootstrap during configure phase
and adds a systemd unit that runs dpkg-reconfigure openssh-server
only if ssh host keys are not found. This ensures that every image
burned to sd card gets it's own set of ssh host keys, improving the
security posture.

Co-authored-by: Jan Praczyk <jan@jpraczyk.xyz>
  • Loading branch information
jpraczyk and Jan Praczyk authored Aug 10, 2024
1 parent dfd6712 commit ec913e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Generate SSH host keys on first boot
ConditionPathExistsGlob=!/etc/ssh/ssh_host_*_key

[Service]
Type=oneshot
ExecStart=/usr/sbin/dpkg-reconfigure -f noninteractive openssh-server

[Install]
RequiredBy=multi-user.target
4 changes: 4 additions & 0 deletions scripts/build-debian
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ echo "kernel-url,${KERNEL_URL}\n" >> /tmp/versions.csv
/usr/bin/systemctl enable systemd-networkd.service
/usr/bin/systemctl enable systemd-resolved.service
/usr/bin/systemctl enable systemd-timesyncd.service
/usr/bin/systemctl enable generate-ssh-host-keys.service
/bin/rm -f /var/log/*.log
/bin/echo "root:${PASSWORD}" | /usr/sbin/chpasswd
/bin/sed -i "s/#*\s*PermitRootLogin .*/PermitRootLogin yes/" /etc/ssh/sshd_config
Expand All @@ -180,6 +181,9 @@ EOF
# Remove ARM emulation stuff again
rm -v debian/usr/bin/qemu-*-static || :

# Remove ssh host keys, they will be generated on first boot by generate-ssh-host-keys.service
rm -v debian/etc/ssh/ssh_host_*_key* || :

cp -rv --preserve=mode ../2nd-stage-files/post-2nd-stage-files/* debian

# Set hostname
Expand Down

0 comments on commit ec913e6

Please sign in to comment.