Unlock a LUKS-encrypted disk over SSH with NixOS initrd networking
Posted on Sun 17 May 2026 in NixOS
A headless server with an encrypted root disk needs a passphrase on every boot. Without a keyboard and screen, entering that passphrase is a problem, especially after a power loss. NixOS can bring up the network in the initrd and run an SSH server, so you can unlock the disk from another machine.
Generate a host key
The initrd SSH server needs its own host key. Do not reuse the regular SSH host key. NixOS copies it into the initrd, which lives on the unencrypted boot partition.
sudo mkdir -p /etc/secrets/initrd
sudo ssh-keygen -t ed25519 -N "" -f /etc/secrets/initrd/ssh_host_ed25519_key
sudo chmod 600 /etc/secrets/initrd/ssh_host_*_key
Enable networking and SSH
Add these options to the host configuration:
# Unlock luks via SSH, requires the host key from the previous step
boot.initrd.network.enable = true;
boot.initrd.network.ssh.enable = true;
boot.initrd.network.ssh.port = 2222;
boot.initrd.network.ssh.hostKeys = [ "/etc/secrets/initrd/ssh_host_ed25519_key" ];
boot.initrd.network.ssh.authorizedKeys = [
"ssh-ed25519 AAAA... you@example"
];
boot.initrd.luks.forceLuksSupportInInitrd = true;
boot.initrd.availableKernelModules = [ "e1000e" ];
DHCP runs because networking.useDHCP defaults to true.
e1000e is the network driver; run lspci -v | grep -iA8 ethernet to find the driver for your NIC.
Without DHCP, set a static address with the ip= kernel parameter or boot.initrd.systemd.network.networks.
Apply the configuration and reboot:
sudo nixos-rebuild switch --flake '.#<hostname>'
sudo reboot
Unlock the disk
Connect to the initrd SSH server while the machine waits for the passphrase:
ssh -p 2222 -o HostKeyAlias=initrd root@<host>
HostKeyAlias keeps the initrd host key separate from the regular host key in known_hosts.
Execute systemd-tty-ask-password-agent and answer the pending passphrase request:
systemd-tty-ask-password-agent
Enter the passphrase and the boot continues. The SSH session closes when the initrd finishes.
Firewall
The initrd SSH server starts before the NixOS firewall, so networking.firewall does not apply to it.
If the machine is reachable from the internet, restrict port 2222 to trusted networks or forward it only when needed.
The host key lives on the unencrypted boot partition, so treat it as a convenience key, not a secret.
Further information
This post is an opinionated version of the Remote disk unlocking article in the NixOS wiki. Check the article for more options.