Introduction
Stolen laptops get the headlines, but lost drives, decommissioned servers, and RMA'd disks leak far more data. Disk encryption turns a physical-access incident into a non-event: without the key, the drive is noise. On Linux, LUKS2 (via cryptsetup) is the standard, and modern systemd can bind the unlock key to the machine's TPM2 chip so servers reboot unattended without storing a passphrase on disk.
In this guide you will:
- Encrypt a data disk with LUKS2 using sane defaults
- Create a filesystem and mount it persistently via
/etc/crypttab - Add a recovery key and enroll a TPM2 token for automatic unlock
- Back up the LUKS header and test recovery
- Rotate and revoke keys
This covers a data disk. Encrypting the root filesystem follows the same primitives but needs initramfs changes best done at install time.
Warning: The commands below destroy existing data on the target device. Double-check the device name with
lsblkbefore every destructive step.
Prerequisites
Install the tooling and confirm the TPM is visible:
# Debian / Ubuntu
sudo apt update && sudo apt install -y cryptsetup tpm2-tools
# RHEL / Rocky / Alma
sudo dnf install -y cryptsetup tpm2-tools
# Confirm versions and TPM presence
cryptsetup --version
systemctl --version | head -1
ls /dev/tpmrm0
sudo systemd-cryptenroll --tpm2-device=listIdentify your target disk. In this guide it is /dev/sdb (a whole disk); substitute your own.
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPEStep 1: Format the Disk with LUKS2
sudo cryptsetup luksFormat --type luks2 \
--cipher aes-xts-plain64 --key-size 512 \
--hash sha256 --pbkdf argon2id \
/dev/sdbYou will be asked to type YES (uppercase) and then to set a passphrase. Use a long one; this passphrase becomes your fallback key slot. Argon2id makes offline brute-forcing memory-hard.
Inspect the result:
sudo cryptsetup luksDump /dev/sdbYou should see Version: 2, one keyslot (slot 0), and the argon2id PBKDF.
Step 2: Open, Format, and Mount
sudo cryptsetup open /dev/sdb securedata
sudo mkfs.ext4 -L securedata /dev/mapper/securedata
sudo mkdir -p /srv/secure
sudo mount /dev/mapper/securedata /srv/secure
df -h /srv/secureThe mapper name (securedata) is how the decrypted device appears under /dev/mapper/.
Step 3: Back Up the LUKS Header (Do This Now)
The LUKS header holds the encrypted master key. If it is damaged, the data is unrecoverable even with the right passphrase. Back it up immediately and store it off the machine.
sudo cryptsetup luksHeaderBackup /dev/sdb \
--header-backup-file /root/sdb-luks-header-$(date +%F).img
sudo chmod 600 /root/sdb-luks-header-*.imgTreat the backup like the key itself: anyone with the header and an old passphrase can still unlock the disk, so if you later revoke a passphrase, re-take the backup and destroy the old copy.
Step 4: Add a Recovery Key
A recovery key is a random high-entropy secret you store offline in case the TPM state changes (motherboard swap, firmware update, Secure Boot changes).
sudo systemd-cryptenroll --recovery-key /dev/sdbEnter your existing passphrase when asked. The command prints a recovery key such as cccc-dddd-eeee-... once. Copy it into your password manager now; it cannot be displayed again.
Step 5: Enroll the TPM2 Token
Bind an unlock key to the TPM, sealed to PCR 7 (the Secure Boot state). PCR 7 survives kernel updates, whereas sealing to PCRs 0-4 breaks on every firmware or bootloader change.
sudo systemd-cryptenroll --tpm2-device=auto --tpm2-pcrs=7 /dev/sdbFor stronger protection, add a PIN so a stolen, powered machine still needs a secret:
sudo systemd-cryptenroll --tpm2-device=auto --tpm2-pcrs=7 --tpm2-with-pin=yes /dev/sdbPIN-protected unlock needs someone to type the PIN at boot, so skip it for headless servers. For unattended hosts, rely on PCR 7 plus Secure Boot being enabled and locked with a firmware password.
Confirm the keyslots:
sudo cryptsetup luksDump /dev/sdb | grep -E "Keyslots|Tokens|systemd-tpm2|systemd-recovery"Step 6: Configure Persistent Unlock at Boot
Find the UUID of the LUKS container (not the inner filesystem):
sudo blkid /dev/sdbAdd to /etc/crypttab (replace the UUID):
securedata UUID=11111111-2222-3333-4444-555555555555 none luks,tpm2-device=auto,nofailAdd to /etc/fstab:
/dev/mapper/securedata /srv/secure ext4 defaults,nofail 0 2The nofail options keep a failed unlock from dropping the host into emergency mode. Regenerate the initramfs if your distro requires it, then unmount and test:
# Debian / Ubuntu
sudo update-initramfs -u
# RHEL family
sudo dracut -f
sudo umount /srv/secure
sudo cryptsetup close securedata
sudo systemctl daemon-reload
sudo systemctl start systemd-cryptsetup@securedata.service
sudo mount -aVerification and Testing
- Reboot and confirm the volume unlocks without a prompt:
sudo reboot
# after boot
lsblk -f /dev/sdb
findmnt /srv/secure- Simulate TPM failure by unlocking with the recovery key instead:
sudo systemctl stop srv-secure.mount 2>/dev/null; sudo umount /srv/secure
sudo cryptsetup close securedata
sudo cryptsetup open /dev/sdb securedata # type the recovery key at the prompt- Test header restore on a throwaway loop device, never on the real disk:
truncate -s 64M /tmp/test.img
sudo cryptsetup luksFormat --type luks2 --batch-mode /tmp/test.img <<< "testpass"
sudo cryptsetup luksHeaderBackup /tmp/test.img --header-backup-file /tmp/test-header.img
sudo cryptsetup luksHeaderRestore /tmp/test.img --header-backup-file /tmp/test-header.imgIf these all work, you have proven both the convenient path (TPM) and both recovery paths (recovery key, header backup).
Key Rotation and Revocation
Wipe and re-enroll the TPM slot after a firmware update or motherboard swap:
sudo systemd-cryptenroll --wipe-slot=tpm2 --tpm2-device=auto --tpm2-pcrs=7 /dev/sdbRemove a passphrase you no longer trust (find slot numbers with luksDump):
sudo cryptsetup luksKillSlot /dev/sdb 0Never kill your last working slot. After any slot change, take a fresh header backup.
Troubleshooting
TPM unlock stops working after an update. PCR values changed (often after toggling Secure Boot or a firmware update). Unlock with the recovery key and re-run the --wipe-slot=tpm2 command above.
No key available with this passphrase. Check keyboard layout (the initramfs may use US layout), and confirm you are using the correct slot's secret. Try sudo cryptsetup open --test-passphrase /dev/sdb.
Boot hangs waiting for the device. Add nofail and x-systemd.device-timeout=10s to the crypttab/fstab options so a missing disk does not block boot.
systemd-cryptenroll: command not found or TPM option errors. You need systemd 252+ and a build with TPM2 support; check with systemd-cryptenroll --tpm2-device=list. On older distros use Clevis (clevis luks bind -d /dev/sdb tpm2 '{"pcr_ids":"7"}') instead.
No /dev/tpmrm0 in a VM. Add a virtual TPM 2.0 (swtpm) to the VM definition; in libvirt, add a <tpm model='tpm-crb'> device with the emulated backend.
Performance concerns. AES-XTS is hardware accelerated on any CPU with AES-NI; check with cryptsetup benchmark. Expect low single-digit percent overhead.
Summary
You now have a LUKS2-encrypted data disk that unlocks automatically on the trusted machine via TPM2, with layered recovery: an offline recovery key, a passphrase slot, and a backed-up header. Key takeaways:
- Back up the LUKS header the moment you create the volume, and store it off-host
- Seal to PCR 7 for upgrade-tolerant unlocking; add a PIN where a human is present at boot
- Always keep a recovery key, because TPM bindings break when hardware or firmware changes
- Test every recovery path before you rely on it
- Encryption at rest protects against physical theft only; combine it with backups, access controls, and monitoring