Skip to content

$ cat ./posts/security/luks-on-removable-media.sh

-rw-r--r-- 1.6K #security #storage

LUKS on removable media

by chris · 2 min read · /security /storage


An encrypted volume that only opens through a desktop applet is a volume you cannot open from a rescue shell. The command-line path is four steps and works everywhere cryptsetup is installed.

1. Format

sudo cryptsetup luksFormat --type luks2 
  --cipher aes-xts-plain64 --key-size 512 
  --pbkdf argon2id /dev/sdb1

LUKS2 with argon2id is the current default for good reason: the key derivation is memory-hard, so a stolen stick is expensive to attack offline rather than merely slow.

2. Open, make a filesystem, close

sudo cryptsetup open /dev/sdb1 usbvault
sudo mkfs.ext4 -L vault /dev/mapper/usbvault
sudo mount /dev/mapper/usbvault /mnt

# when finished
sudo umount /mnt
sudo cryptsetup close usbvault

Closing matters. An unmounted but still-open mapping leaves the key in kernel memory and the volume one command away from readable.

3. Keep a second key slot

sudo cryptsetup luksAddKey /dev/sdb1
sudo cryptsetup luksDump /dev/sdb1 | grep -A2 Keyslot

Eight slots exist. Using exactly one is how a typo during a passphrase change destroys the data permanently.

4. Back up the header

sudo cryptsetup luksHeaderBackup /dev/sdb1 
  --header-backup-file luks-header-sdb1.img

The header holds the encrypted master key. Corrupt those few kilobytes and the volume is unrecoverable with any passphrase. Store the backup somewhere that is itself encrypted — the header plus a known passphrase is equivalent to the data.

← cd .. fris@linux:~/blog$ man security