LUKS (Linux Unified Key Setup) encrypts a whole disk at block level: without the passphrase, the data is unreadable, even if the disk is stolen. The standard tool is cryptsetup. This guide shows how to encrypt a volume with LUKS2, manage several passphrases, back up the header (vital), and unlock at boot via /etc/crypttab or a network server (NBDE). For intermediate to advanced administrators, on Debian, Ubuntu or RHEL. Commands tested on AlmaLinux 10 (cryptsetup 2.8.1).
What you will learn
- Encrypt a volume with
cryptsetup luksFormat(LUKS2) - Open, format and mount the decrypted volume
- Manage passphrases and back up the header
- Unlock at boot with
crypttab, and automatically with Clevis/Tang
Prerequisites
- A Linux server with
sudoand thecryptsetuppackage - A dedicated disk, partition or container file (encryption overwrites its content)
- Know how to identify a disk and mount it
LUKS1 or LUKS2
Since cryptsetup 2.1, the default format is LUKS2, and it is the one to use. It brings a redundant header (stored twice, so more resistant to corruption) and Argon2id key derivation, designed to resist GPU cracking, where LUKS1 is limited to PBKDF2. LUKS2 also carries up to 32 key slots against 8 for LUKS1.
Encrypt a volume
The order never changes: format, open, create the file system on the mapping, mount. The file system always goes on /dev/mapper/..., never on the raw disk.
-
Format the volume as LUKS2. The operation asks for a confirmation (
YES) and the passphrase.Terminal window sudo cryptsetup luksFormat --type luks2 /dev/sdXThe defaults are safe and confirmed by
luksDumpbelow:aes-xts-plain64cipher, 512-bit key (that is AES-256, the XTS key being doubled), Argon2id derivation.The confirmation is case sensitive: the command asks
Are you sure? (Type 'yes' in capital letters):, and a lowercaseyesanswer givesOperation aborted.without formatting anything. OnlyYESgoes on. -
Inspect the header to check the format.
Terminal window sudo cryptsetup luksDump /dev/sdXVersion: 2Keyslots:0: luks2Cipher: aes-xts-plain64Cipher key: 512 bitsPBKDF: argon2id -
Open the volume: it appears under
/dev/mapper/.Terminal window sudo cryptsetup open /dev/sdX coffre -
Create the file system and mount the mapping.
Terminal window sudo mkfs.ext4 /dev/mapper/coffresudo mkdir -p /mnt/coffresudo mount /dev/mapper/coffre /mnt/coffre
To close the volume after unmounting: sudo umount /mnt/coffre && sudo cryptsetup close coffre. The data becomes unreadable again without the passphrase.
Manage passphrases
LUKS2 stores up to 32 independent passphrases in key slots: handy to give personal access to several administrators, then revoke one of them without changing the others.
sudo cryptsetup luksAddKey /dev/sdX # add a passphrase (a valid passphrase is asked)sudo cryptsetup luksChangeKey /dev/sdX # replace a passphrasesudo cryptsetup luksRemoveKey /dev/sdX # remove the passphrase typedsudo cryptsetup luksKillSlot /dev/sdX 2 # remove slot number 2 (another passphrase required)sudo cryptsetup luksDump /dev/sdX # see the used slotsFor automated unlocking, you can enrol a key file instead of a passphrase:
sudo dd if=/dev/urandom of=/root/luks.key bs=512 count=4sudo chmod 0400 /root/luks.keysudo cryptsetup luksAddKey /dev/sdX /root/luks.keyBack up the header (vital)
The LUKS header holds the encrypted keys: if it is corrupted, all data is lost, even with the right passphrase. An accidental write on the start of the disk (an unfortunate mkfs) is enough. Back it up offline:
sudo cryptsetup luksHeaderBackup /dev/sdX --header-backup-file /secours/header.imgOn LUKS2, this file is 16 MiB (16,777,216 bytes measured). Restore: cryptsetup luksHeaderRestore.
Unlock at boot: /etc/crypttab
For an encrypted volume to be available at boot, declare it in /etc/crypttab, which creates the /dev/mapper/ mapping, then mount that mapping via /etc/fstab (never the raw disk).
Get the UUID of the encrypted volume, then add the line (four fields: name, device, key, options):
sudo cryptsetup luksUUID /dev/sdXcoffre UUID=5cda3d09-88cd-4c5e-941f-347749dd8025 none luksThe none key asks for the passphrase interactively at boot; replace it with a key file path for unlocking without typing. The /etc/fstab side then mounts the mapping:
/dev/mapper/coffre /mnt/coffre ext4 defaults,nofail 0 2After a change, regenerate the initramfs so that unlocking works early at boot:
sudo update-initramfs -usudo dracut --forceAutomatic network unlocking (NBDE)
Typing a passphrase at every boot is impractical on a fleet of servers. NBDE (Network-Bound Disk Encryption) unlocks a volume automatically as long as the machine reaches a Tang server on the network, without ever sending the key. The client is Clevis, the server Tang; the state is stored in the LUKS2 header.
On the server, Tang is enabled as a service:
sudo systemctl enable tangd.socket --nowOn the machine to encrypt, bind the volume to Tang. At the next boot, it will open by itself if it reaches the server:
sudo clevis luks bind -d /dev/sdX tang '{"url":"http://tang.interne.lan"}'sudo clevis luks list -d /dev/sdXVariant without network: bind the volume to the machine's TPM2 chip (clevis luks bind -d /dev/sdX tpm2 '{"pcr_ids":"7"}'). You can even require Tang AND TPM2 at the same time via the sss pin. NBDE security rests on the fact that an attacker needs both the physical disk and access to the network of the Tang server.
Performance and TRIM
With the AES-NI acceleration present on every modern CPU, the cost of encryption is negligible compared with the speed of a disk. Measure it on your hardware:
cryptsetup benchmarkBy default, TRIM/discard is disabled on encrypted volumes. Enabling it (cryptsetup open --allow-discards or the discard option in crypttab) improves the performance and lifetime of an SSD, but reveals at the physical level which blocks are used (file system type, space used). On a sensitive system, leave it disabled; enable it knowingly.
Traps to avoid
Encryption adds a layer you no longer see once the volume is open, and that is precisely what makes its mistakes costly: most of them only show up when you need the data.
- Header lost = total loss: back it up offline, it is non-negotiable.
- LUKS protects at rest only: once the volume is unlocked and mounted, files are in clear text for any authorised process. LUKS protects against disk theft, not against access to the running system.
- File system on the wrong device: always
mkfson/dev/mapper/..., never on the raw encrypted disk (that would destroy the header). - Block versus file: LUKS encrypts the whole volume (ideal for disk, partition, swap). To encrypt a subfolder synced to a cloud, a per-file tool such as
gocryptfsis better suited. AvoideCryptfs, deprecated. - Encrypted
/bootand GRUB: an old GRUB does not handle Argon2id; for an encrypted root, validate the bootloader / version pair on your distribution.
Troubleshooting
cryptsetup messages are short but precise, and its exit code completes them: 2 for a refused passphrase, 5 for a mapping already open. Note both before touching the header.
| Symptom | Likely cause | Fix |
|---|---|---|
No key available with this passphrase. | Wrong passphrase or wrong slot | Try another passphrase, check luksDump |
| Volume not unlocked at boot | crypttab/initramfs not regenerated | Fix crypttab, update-initramfs/dracut |
Device coffre already exists. | Mapping already open | cryptsetup close coffre before reopening |
| Unreadable data after an incident | Corrupted header | cryptsetup luksHeaderRestore from the backup |
| Slow opening | Argon2id uses RAM/CPU | Normal (anti brute force); adjust --pbkdf-memory at creation |
Put it into practice
Theory proves nothing until the volume has been encrypted, closed, then reopened after a reboot. This lab puts you on a VM with a blank disk: you run the full format, open, mkfs, mount, crypttab cycle, and the validation checks the real state of the machine after reboot, not the commands you typed.
Key points
- LUKS2 is the default format (Argon2id, redundant header, 32 slots): use it.
- The order is format → open → mkfs on the mapping → mount → close.
- The defaults are safe: aes-xts-plain64, 512-bit key (AES-256), Argon2id.
- Back up the header (
luksHeaderBackup, 16 MiB) offline: losing it = total loss. - Unlocking at boot via
/etc/crypttab(by UUID) +fstab+ initramfs; automatic with Clevis/Tang (NBDE) or TPM2. - LUKS protects at rest: once mounted, the data is in clear text for the system.
Knowledge check
Check that the essentials of this guide are acquired. The questions only cover what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Control disk capacity and quotas
A new full quiz with random questions
Work again only on the questions you missed