Fedora encrypted disk unlocking via SSH
Introduction#
This guide shows how to unlock a LUKS2-encrypted Fedora system over SSH during boot. It is useful for headless servers, and for desktops whose keyboard isn’t available that early.
My keyboard and mouse are paired with my computer via Bluetooth, and I always encrypt my drives with LUKS2. That is a chicken-and-egg problem: at this early boot stage the Bluetooth stack isn’t running yet, so I can’t enter my passphrase. A wired keyboard would solve it, but then I’d have two keyboards on the desk. Fortunately, gsauthof/dracut-sshd provides SSH access to the initramfs early user space. Once you have logged in with your SSH key, you can unlock the system. After the passphrase is accepted, the SSH session closes and the boot continues from the decrypted drive.
The module’s documentation is well structured and easy to follow, so have a look at the project repository. I have gone through the setup on Fedora a few times, so I still thought a short write-up would be useful.
This guide was written on Fedora 37.
Installation#
First, enable the module’s Copr repository and install the package.
It may pull in dracut-network as a dependency, which provides networking in the early boot phase.
dnf copr enable gsauthof/dracut-sshd
dnf install dracut-sshd
Next, add two parameters to the kernel options in /etc/default/grub:
rd.neednet=1to make the network stack availableip=dhcpso that the first network interface gets an address via DHCP (warning: footgun)
If the system has several active network ports, the first one to get an address wins. In the worst case, the machine is then not reachable on the network you wanted and you can’t unlock it. To prevent this, name the interface, or even the whole IP configuration, in the parameter.
For example, a static IP configuration:
ip=192.168.180.120::192.168.180.1:255.255.255.0::enp1s0:off
Or DHCP on a specific interface:
ip=enp1s0:dhcp
More about IP networking in early user space:
The modification in /etc/default/grub should look something like this:
GRUB_CMDLINE_LINUX="resume=/dev/mapper/server-swap rd.luks.uuid=luks-**** rd.lvm.lv=server/root rd.md.uuid=**** rd.lvm.lv=server/swap rhgb quiet rd.neednet=1 ip=dhcp"
/etc/default/grub only applies to kernels installed later.
To add the parameters to the installed kernels as well, use grubby:
grubby --update-kernel=ALL --args="rd.neednet=1 ip=dhcp"
For the SSH login to work, root needs an authorised key:
mkdir -p /root/.ssh
chmod 700 /root/.ssh
cat id_ed25519.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
On recent Fedora installations, the root account is locked with ! instead of * in /etc/shadow by default.
According to the module’s documentation, replacing the exclamation mark with an asterisk is enough.
In my case this didn’t work, which is another footgun!
Instead, set a password for root. Don’t worry: the default OpenSSH server configuration doesn’t allow password logins for root, so this doesn’t make the system less secure.
# passwd
Changing password for user root.
New password: *******
Finally, rebuild the initramfs with dracut:
dracut -f -v
The output should now include these modules:
dracut: *** Including module: network ***
dracut: *** Including module: sshd ***
After a reboot, the passphrase prompt appears as usual. In addition, you can now connect via SSH and enter the passphrase there:
~ $ ssh [email protected]
Welcome to the early boot SSH environment. You may type
systemd-tty-ask-password-agent
(or press "arrow up") to unlock your disks.
This shell will terminate automatically a few seconds after the
unlocking process has succeeded and when the boot proceeds.
initramfs-ssh:/root# systemd-tty-ask-password-agent
🔐 Please enter passphrase for disk Samsung SSD 980 500GB (luks-****): **************
initramfs-ssh:/root# Connection to fedora.zaage.it closed by remote host.
Connection to fedora.zaage.it closed.
Conclusion#
With dracut-sshd, an encrypted machine can be unlocked from anywhere on the network, without a BMC or remote KVM. That makes full disk encryption practical for dedicated servers too: on my Hetzner server, I set up the module without a KVM, from the rescue image with chroot.
If you prefer unlocking without a passphrase, bound to your network, see Network Bound Disk Encryption with clevis and tang on OpenWrt and Red Hat’s guide to policy-based decryption.
Sources#
- dracut-sshd: https://github.com/gsauthof/dracut-sshd
- Linux kernel documentation,
ip=parameter (nfsroot): https://docs.kernel.org/admin-guide/nfs/nfsroot.html - Red Hat Enterprise Linux 7 Networking Guide, IP networking from the kernel command line: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/sec-configuring_ip_networking_from_the_kernel_command_line