TL;DR
| |
After any key change, back up the header again and destroy the previous backup. Restoring an old backup reactivates every passphrase it held at the time, including the ones you removed.
Why the header is everything
LUKS does not encrypt your data with your passphrase: it encrypts it with a random master key, and that master key sits encrypted inside the header, one copy per slot, each protected by a passphrase or keyfile. The header takes the first 16 MiB of the device on LUKS2 (2 MiB on LUKS1).
If those megabytes get damaged (a wrong dd, a mkfs on the raw disk, dead sectors right there), the master key is gone and the data becomes unrecoverable noise, even if you know the passphrase. A header backup, stored somewhere else, is the only insurance. The file is the size of the header and contains no data, only the encrypted master key.
Backing up and restoring
| |
Keep it off the machine, encrypted (a gpg -c is enough), with a name that says which disk it belongs to. Restoring:
| |
It only repairs the header. If the accident went past the first 16 MiB, whatever was there stays lost.
Slots and keys
LUKS2 has 32 slots (0–31); LUKS1, 8. All of them unlock the same master key, so it does not matter which one you come in through:
| |
Keyfile to open without typing (typical for a data disk that comes up with the system):
| |
And in /etc/crypttab:
| |
Test without rebooting: systemctl daemon-reload && systemctl start systemd-cryptsetup@data.service. A keyfile under /root only makes sense if / is encrypted too or the disk belongs to a machine you trust physically.
Removing keys:
luksRemoveKeyasks for the passphrase you want gone.luksKillSlot <n>removes a specific slot and asks for another valid passphrase; useful when nobody remembers the slot’s own.luksChangeKeyswaps one passphrase for another in the same slot.
Traps
- The header backup keeps the old keys. You removed a former colleague’s passphrase and the earlier backup is still on a USB stick: that stick opens the disk. Re-back up after every change and destroy the previous ones (
shred -u). luksEraseis not secure erasure if a header backup exists. It destroys every slot on the disk, but aluksHeaderRestorebrings them back. For a final wipe, destroy the backups too.- Do not kill the last slot.
cryptsetupwarns the device will become unusable; if you accept, that is it. - A backup on the same disk is not a backup. Nor on the same server if losing the server is what you fear.
- Detached header (
--header /path/header.imgwhen formatting and opening) moves the header off the disk entirely; then the “backup” is the header itself and losing it equals losing the data. - LUKS1 vs LUKS2: the commands are the same;
cryptsetup convert --type luks2migrates a LUKS1 (with a backup first, of course).
Reference: man 8 cryptsetup, sections luksHeaderBackup, luksHeaderRestore, and luksKillSlot.