Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

Encrypt a disk with LUKS and cryptsetup

By updated

Improve this page

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 sudo and the cryptsetup package
  • 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.

  1. Format the volume as LUKS2. The operation asks for a confirmation (YES) and the passphrase.

    Terminal window
    sudo cryptsetup luksFormat --type luks2 /dev/sdX

    The defaults are safe and confirmed by luksDump below: aes-xts-plain64 cipher, 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 lowercase yes answer gives Operation aborted. without formatting anything. Only YES goes on.

  2. Inspect the header to check the format.

    Terminal window
    sudo cryptsetup luksDump /dev/sdX
    Version: 2
    Keyslots:
    0: luks2
    Cipher: aes-xts-plain64
    Cipher key: 512 bits
    PBKDF: argon2id
  3. Open the volume: it appears under /dev/mapper/.

    Terminal window
    sudo cryptsetup open /dev/sdX coffre
  4. Create the file system and mount the mapping.

    Terminal window
    sudo mkfs.ext4 /dev/mapper/coffre
    sudo mkdir -p /mnt/coffre
    sudo 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.

Terminal window
sudo cryptsetup luksAddKey /dev/sdX # add a passphrase (a valid passphrase is asked)
sudo cryptsetup luksChangeKey /dev/sdX # replace a passphrase
sudo cryptsetup luksRemoveKey /dev/sdX # remove the passphrase typed
sudo cryptsetup luksKillSlot /dev/sdX 2 # remove slot number 2 (another passphrase required)
sudo cryptsetup luksDump /dev/sdX # see the used slots

For automated unlocking, you can enrol a key file instead of a passphrase:

Terminal window
sudo dd if=/dev/urandom of=/root/luks.key bs=512 count=4
sudo chmod 0400 /root/luks.key
sudo cryptsetup luksAddKey /dev/sdX /root/luks.key

Back 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:

Terminal window
sudo cryptsetup luksHeaderBackup /dev/sdX --header-backup-file /secours/header.img

On 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):

Terminal window
sudo cryptsetup luksUUID /dev/sdX
/etc/crypttab
coffre UUID=5cda3d09-88cd-4c5e-941f-347749dd8025 none luks

The 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:

/etc/fstab
/dev/mapper/coffre /mnt/coffre ext4 defaults,nofail 0 2

After a change, regenerate the initramfs so that unlocking works early at boot:

Terminal window
sudo update-initramfs -u

Automatic 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:

Terminal window
sudo systemctl enable tangd.socket --now

On the machine to encrypt, bind the volume to Tang. At the next boot, it will open by itself if it reaches the server:

Terminal window
sudo clevis luks bind -d /dev/sdX tang '{"url":"http://tang.interne.lan"}'
sudo clevis luks list -d /dev/sdX

Variant 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:

Terminal window
cryptsetup benchmark

By 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 mkfs on /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 gocryptfs is better suited. Avoid eCryptfs, deprecated.
  • Encrypted /boot and 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.

SymptomLikely causeFix
No key available with this passphrase.Wrong passphrase or wrong slotTry another passphrase, check luksDump
Volume not unlocked at bootcrypttab/initramfs not regeneratedFix crypttab, update-initramfs/dracut
Device coffre already exists.Mapping already opencryptsetup close coffre before reopening
Unreadable data after an incidentCorrupted headercryptsetup luksHeaderRestore from the backup
Slow openingArgon2id uses RAM/CPUNormal (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

6 questions
6 min
70% to pass

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

Is this project useful to you?

The guides, courses, quizzes and labs stay free. If you use them regularly, you can contribute to their upkeep and development.

Become a supporting member (new tab) One-time support (new tab) Learn more

Follow the project on LinkedIn