Kahibaro
Discord Login Register

4.4.4 Full disk encryption (LUKS)

Why LUKS and where it fits

Linux Unified Key Setup (LUKS) is the standard way to do block‑device encryption on Linux, usually via dm-crypt. In the context of advanced storage, LUKS gives you:

Here we focus on how LUKS works, its versions, and how to design, set up, and manage full‑disk encryption on a Linux system.

LUKS1 vs LUKS2

Modern systems should generally use LUKS2, but you will still encounter LUKS1.

On‑disk format

A LUKS container (on a partition or disk) has:

Neither the header nor keyslots contain your actual data, only the master key in wrapped (encrypted) form.

LUKS1 characteristics

You may prefer LUKS1 when:

LUKS2 characteristics

You generally want:

To check:

cryptsetup luksDump /dev/sdXn

Look for Version: 1 or Version: 2.

LUKS concepts: master key, keyslots, PBKDF

Master key and data encryption

When you create a LUKS volume, cryptsetup generates a random master key. This key directly encrypts the data using a block cipher (commonly AES) in a mode suited for disk encryption (e.g., XTS).

Keyslots and passphrases

Each keyslot stores a copy of the master key wrapped by a passphrase or key file, derived via a PBKDF (Password‑Based Key Derivation Function).

Properties:

Managing keyslots (examples on an existing LUKS device /dev/sdXn):

  cryptsetup luksDump /dev/sdXn
  cryptsetup luksAddKey /dev/sdXn

You’ll enter an existing valid passphrase first, then the new one.

  cryptsetup luksRemoveKey /dev/sdXn

You’ll type the passphrase you want to revoke.

PBKDF (key stretching)

Passphrases are run through a PBKDF to slow brute‑force attacks. LUKS uses:

Choosing PBKDF parameters is a trade‑off:

When creating a LUKS volume, you can tune this with:

cryptsetup luksFormat --type luks2 \
  --pbkdf argon2id --pbkdf-memory 1048576 --pbkdf-time 2000 \
  /dev/sdXn

(Values are examples; cryptsetup will auto‑tune if you omit them.)

Layout patterns for “full‑disk” encryption

“Full disk” is often implemented with combinations of LUKS, partitions, and LVM. Common layouts:

Encrypted root + swap + home via LVM inside LUKS

Layout (most distros’ “encrypt whole disk” option):

  1. Create a small unencrypted /boot partition.
  2. Create one large partition for everything else.
  3. Put LUKS on that large partition.
  4. Inside the unlocked LUKS device, create an LVM PV/VG/LVs for:
    • / (root)
    • swap
    • optionally /home (as a separate LV)

Pros:

Cons:

Separate LUKS for each partition

Example:

Pros:

Cons:

Full‑disk including `/boot` (with modern boot chains)

Modern bootloaders/firmware combinations (e.g., systemd‑boot, GRUB on some setups, Secure Boot + TPM integration) can support:

This is more advanced and distro‑specific, but the key idea is: the firmware and bootloader trust chain must be able to unlock the LUKS container or verify a signed image before loading the kernel.

Creating a LUKS encrypted device

Here we create a LUKS2 device on /dev/sdX2 and a filesystem on top. This is the block device side; integrating it with the boot process is covered later.

1. Prepare the partition

Using your preferred partition tool (fdisk, parted, etc.), create a partition (e.g., /dev/sdX2) and mark it as a LUKS/crypto partition type if available (for metadata clarity only).

2. Initialize LUKS

cryptsetup luksFormat --type luks2 /dev/sdX2

You will be prompted to confirm and then set an initial passphrase.

Choosing options

Common useful options:

  cryptsetup luksFormat \
    --type luks2 \
    --cipher aes-xts-plain64 --key-size 512 \
    /dev/sdX2

3. Open (map) the LUKS device

Opening creates a device mapper node under /dev/mapper/:

cryptsetup open /dev/sdX2 cryptdata

This creates /dev/mapper/cryptdata which exposes decrypted blocks.

4. Create a filesystem inside

Now create a filesystem on the mapped device:

mkfs.ext4 /dev/mapper/cryptdata

Then mount it normally:

mkdir /mnt/secure
mount /dev/mapper/cryptdata /mnt/secure

To close later:

umount /mnt/secure
cryptsetup close cryptdata

Integrating LUKS with the boot process

For full‑disk encryption (including the root filesystem), you must unlock LUKS before the real root is mounted. This is handled by:

`/etc/crypttab` basics

/etc/crypttab defines encrypted devices to open at boot:

Format:

<name>  <source>       <keyfile>  <options>

Examples:

  cryptroot  UUID=<uuid-of-luks-partition>  none  luks
  cryptswap  /dev/sdX3  /dev/urandom  swap,cipher=aes-xts-plain64,size=256
  cryptdata  /dev/sdX4  /root/keys/data.key  luks

After editing /etc/crypttab, rebuild your initramfs (distro‑specific, e.g. update-initramfs -u or dracut -f).

`/etc/fstab` and decrypted devices

Once a LUKS device is opened at boot (e.g., with name cryptroot), the resulting node is /dev/mapper/cryptroot. In /etc/fstab you then mount that:

/dev/mapper/cryptroot  /      ext4  defaults  0  1
/dev/mapper/cryptdata  /data  ext4  defaults  0  2

The main rule: fstab refers to the decrypted mapper devices, not the raw LUKS partitions.

Passphrase strategies and key files

Strong passphrases

Because the attacker can make unlimited offline attempts to guess your passphrase, quality matters:

Multiple passphrases for different purposes

Use separate keyslots for:

Add a new keyslot with a keyfile:

# Create keyfile with restricted permissions
dd if=/dev/urandom of=/root/keys/data.key bs=1 count=64
chmod 0400 /root/keys/data.key
# Add keyfile as an additional unlock method
cryptsetup luksAddKey /dev/sdX2 /root/keys/data.key

You will be prompted for an existing passphrase first.

Key files and automation

Key files are often used to:

For example, put a keyfile on the root filesystem (which itself is protected by a boot passphrase), and use it in /etc/crypttab for other volumes:

cryptdata  UUID=<uuid-of-luks-part>  /root/keys/data.key  luks

Security trade‑off: once the system is up and root is compromised, additional disks protected by those keyfiles can be unlocked.

Using LUKS with LVM

Full‑disk setups commonly combine LUKS with LVM:

Workflow:

  # After unlock:
  cryptsetup open /dev/sdX2 cryptroot
  # Now activate LVM:
  vgchange -ay
  mount /dev/vg0/root /mnt

For most cases, prefer one LUKS container per physical disk or partition, LVM inside.

Performance considerations

LUKS adds overhead, but on modern hardware it is usually acceptable.

CPU and AES acceleration

Most CPUs have AES acceleration (AES‑NI on x86_64). Check:

grep aes /proc/cpuinfo

If present, you should see near‑native performance. Without AES‑NI, encryption can still be fine for many workloads but heavy I/O may be slower.

Cipher and key size

Common default:

You can reduce to 256‑bit key (AES‑XTS-plain64 with --key-size 256) if you need to squeeze out some performance at the cost of generous but slightly smaller security margin.

Impact is usually small compared to disk speed and other bottlenecks.

I/O patterns

Encryption works at the block layer:

Use tools from the storage chapter (e.g. fio, iostat, blktrace) to measure.

Backup, recovery, and header safety

Backing up data vs backing up headers

Traditional file‑level backups work the same on an encrypted filesystem (after it is unlocked). These do not capture LUKS metadata or headers.

You should also consider:

Backing up the header

For LUKS1 or LUKS2:

cryptsetup luksHeaderBackup /dev/sdX2 --header-backup-file /root/luksheader-sdX2.bin

Store this backup offline, encrypted or in a physically secure place.

To restore (use only if current header is damaged):

cryptsetup luksHeaderRestore /dev/sdX2 --header-backup-file luksheader-sdX2.bin

Warning: restoring a header overwrites the current header and keyslots. Make sure the backup corresponds to the current master key (i.e., made after your final keyslot setup or you understand the history).

Handling forgotten passphrases

LUKS is designed to be secure against guessing; without:

you cannot recover the data. There is no built‑in backdoor.

Mitigations:

Reencrypting and migrating LUKS volumes

Sometimes you need to change encryption parameters (cipher, key size) or fully rotate the master key.

Changing passphrases only (no data reencryption)

When you:

…the data stays encrypted with the same master key. Only the wrapping of the master key changes.

Example: change PBKDF for a slot (LUKS2):

cryptsetup luksConvertKey --pbkdf argon2id --pbkdf-force-iterations 4 /dev/sdX2

(Consult man cryptsetup for current options; they evolve.)

Reencryption (changing the master key or cipher)

LUKS2 supports in‑place reencryption with cryptsetup-reencrypt. This is advanced and potentially risky; always back up your data first.

Example (conceptual):

cryptsetup-reencrypt --encrypt --type luks2 /dev/sdX2

This can change:

You should consult your distro’s current documentation before using it; options and behavior can differ by version.

TPM and hardware integration (overview only)

For advanced setups you can integrate LUKS with:

Conceptually:

These setups aim to balance security with unattended boot for servers, but they introduce new trust chains and failure modes (e.g., network required to unlock).

Practical checklists

Designing a new full‑disk LUKS setup

  1. Decide on layout:
    • Single LUKS + LVM inside for /, swap, /home
    • Separate data partitions if needed
  2. Choose LUKS2 unless you need LUKS1.
  3. Check CPU AES support (grep aes /proc/cpuinfo).
  4. Plan passphrase/key strategy:
    • Strong interactive passphrase
    • Optional backup passphrase
    • Optional keyfiles for secondary disks
  5. Partition disk (/boot unencrypted or advanced encrypted‑boot chain).
  6. Create LUKS container(s) with appropriate cipher/PBKDF.
  7. Create LVM or filesystems on top.
  8. Configure /etc/crypttab and /etc/fstab; rebuild initramfs.
  9. Test boot, encryption unlock, and recovery path.

Ongoing management

With careful design and routine checks, LUKS provides strong, practical full‑disk encryption integrated into advanced Linux storage setups.

Views: 147

Comments

Please login to add a comment.

Don't have an account? Register now!