cryptsetup
Overview
cryptsetup manages LUKS (and other) disk encryption: format, open, close, and resize encrypted block devices. Standard tool for full-disk and data-partition encryption on Linux.
sudo apt install cryptsetupSyntax
sudo cryptsetup [options] command device [args]Common Commands
| Command | Description |
|---|---|
luksFormat |
Initialize LUKS header (destroys data) |
open / luksOpen |
Map decrypted device under /dev/mapper/ |
close / luksClose |
Remove mapping |
status |
Show mapper status |
luksAddKey / luksRemoveKey |
Manage passphrases/keys |
luksDump |
Show header metadata |
resize |
Resize LUKS mapping after LV growth |
Safety
luksFormatis irreversible without backups of the header and key material.
- Store recovery passphrases offline; header backups (
luksHeaderBackup) matter.
- Suspend-to-disk with unlocked volumes has threat-model implications.
- Always confirm the target device with
lsblkfirst.
Examples with Explanations
Format and open (data disk)
sudo cryptsetup luksFormat /dev/sdb1
sudo cryptsetup open /dev/sdb1 crypt_data
sudo mkfs.ext4 /dev/mapper/crypt_data
sudo mount /dev/mapper/crypt_data /mnt/dataStatus and close
sudo cryptsetup status crypt_data
sudo umount /mnt/data
sudo cryptsetup close crypt_dataAdd a second passphrase
sudo cryptsetup luksAddKey /dev/sdb1
sudo cryptsetup luksDump /dev/sdb1 | lessHeader backup
sudo cryptsetup luksHeaderBackup /dev/sdb1 --header-backup-file ~/luks-sdb1.header
chmod 600 ~/luks-sdb1.headerfstab / crypttab sketch
# /etc/crypttab
# crypt_data UUID=… none luks,discard
# then fstab mounts /dev/mapper/crypt_dataUse UUIDs from blkid on the LUKS device for crypttab.
Notes & Pitfalls
- LUKS2 is the modern default on current distros.
- SSD discard/TRIM:
discardoption trades some security for wear-leveling — know your threat model.
- Nested LVM-on-LUKS or LUKS-on-LVM both exist; order affects unlock at boot.
Additional Resources
man cryptsetup
crypttab(5)