LUKS Disk Encryption on Linux with cryptsetup (Complete Guide)

luks disk encryption linux - custom-luks-featured.png

LUKS (Linux Unified Key Setup) is the standard for encrypting block devices on Linux. It protects
data at rest: if a disk is stolen or an old drive is discarded, the contents are unreadable without a passphrase or keyfile.
It does not protect a running, unlocked system, so treat it as one layer alongside
SSH hardening and sensible
file permissions.

This guide encrypts a secondary data disk (not the root filesystem), mounts it, sets up unattended unlock for servers,
and covers the header backup that most tutorials forget.

Understand the Layers Before You Format

LUKS sits between the raw partition and the filesystem. cryptsetup unlocks the container and exposes a decrypted device under /dev/mapper; you create and mount the filesystem on that mapped device, never on the raw partition.

architectureluks-layers.txt
  /dev/sdb1            raw partition (ciphertext)
     |  cryptsetup luksOpen
  /dev/mapper/secure   decrypted block device
     |  mkfs + mount
  /secure              your files
LUKS layering
Partition, LUKS container, then filesystem

Back Up Existing Data First

luksFormat destroys whatever is on the partition. Confirm the device name with lsblk, copy off anything you need, and verify the copy before continuing.

bashidentify the disk
lsblk -f
sudo blkid /dev/sdb1
WarningTyping the wrong device into luksFormat erases it. Re-read the device path before pressing Enter.
Backing up before formatting
Formatting is irreversible

Create the LUKS Container

Modern cryptsetup defaults to LUKS2 with AES-XTS and the argon2id key derivation function. Choose a long, unique passphrase — it is the only thing standing between an attacker and the data.

bashformat
sudo cryptsetup luksFormat --type luks2 /dev/sdb1
bashinspect
sudo cryptsetup luksDump /dev/sdb1
Creating a LUKS2 container
luksFormat writes the header and first key slot

Open the Container, Create a Filesystem, Mount It

Opening maps the decrypted device. Make the filesystem once; afterwards you only open and mount.

bashopen and mount
sudo cryptsetup luksOpen /dev/sdb1 secure
sudo mkfs.ext4 /dev/mapper/secure
sudo mkdir -p /secure
sudo mount /dev/mapper/secure /secure
bashclose when done
sudo umount /secure
sudo cryptsetup luksClose secure
Open, mkfs and mount
Mapper device carries the filesystem

Add a Keyfile Slot for Unattended Unlock

A server that reboots at 3 a.m. cannot wait for someone to type a passphrase. LUKS supports multiple key slots, so add a random keyfile readable only by root while keeping the passphrase as your recovery slot.

bashcreate keyfile
sudo dd if=/dev/urandom of=/root/secure.key bs=512 count=8
sudo chmod 400 /root/secure.key
sudo cryptsetup luksAddKey /dev/sdb1 /root/secure.key
TipThe keyfile lives on the root filesystem, so this protects against a stolen data disk, not a stolen whole server.
Passphrase and keyfile slots
Two slots: human recovery and machine unlock

Wire It into crypttab and fstab

Reference the partition by UUID so a changed device name cannot break boot. nofail keeps a missing disk from dropping the machine into emergency mode.

bashfind the UUID
sudo blkid -s UUID -o value /dev/sdb1
bash/etc/crypttab
secure  UUID=<uuid-from-above>  /root/secure.key  luks
bash/etc/fstab
/dev/mapper/secure  /secure  ext4  defaults,nofail  0  2
bashtest without rebooting
sudo cryptdisks_start secure && sudo mount -a && df -h /secure
crypttab and fstab entries
Unlock in crypttab, mount in fstab

Back Up the LUKS Header

All key slots live in a small header at the start of the partition. If it is corrupted, the data is gone even with the right passphrase. Save a copy offline and treat it as sensitive, since it can be used to attack the passphrase.

bashbackup header
sudo cryptsetup luksHeaderBackup /dev/sdb1 --header-backup-file /safe/place/sdb1-header.img
bashrestore header (only if damaged)
sudo cryptsetup luksHeaderRestore /dev/sdb1 --header-backup-file /safe/place/sdb1-header.img
LUKS header backup
The header is a single point of failure

Rotate and Remove Passphrases

People leave and passphrases leak. Add the new one first, confirm it works, then remove the old one so you never lock yourself out. luksDump shows which slots are in use.

bashrotate
sudo cryptsetup luksAddKey /dev/sdb1
sudo cryptsetup luksRemoveKey /dev/sdb1
sudo cryptsetup luksDump /dev/sdb1 | grep -i keyslot
Rotating passphrases
Add, test, then remove

Cheat Sheet

  • Format: luksFormat → Open: luksOpenmkfsmount
  • Always keep one passphrase slot and an offline header backup

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.