Lab 14: Disk Encryption with LUKS

Time: 40 minutes | Level: Advanced | Docker: docker run -it --rm --privileged ubuntu:22.04 bash

LUKS (Linux Unified Key Setup) is the standard for block-level disk encryption on Linux. It sits between the raw block device and the filesystem, transparently encrypting all data at rest using AES.


Prerequisites

docker run -it --rm --privileged ubuntu:22.04 bash
apt-get update -qq && apt-get install -y cryptsetup

Step 1: Create a Loopback Device for Encryption

# Create a 100 MiB "virtual disk"
dd if=/dev/zero of=/tmp/encrypted.img bs=1M count=100

# Attach to a loop device
mknod /dev/loop50 b 7 50 2>/dev/null || true
losetup /dev/loop50 /tmp/encrypted.img

echo "Device ready:"
losetup -a | grep loop50

# Verify the raw device (no filesystem yet)
file -s /dev/loop50

📸 Verified Output:

💡 LUKS works with any block device: physical disks, partitions, LVM volumes, or loopback devices.


Step 2: Format the Device with LUKS

📸 Verified Output:

💡 --batch-mode suppresses the interactive confirmation prompt. Never use this without being sure about the device! In production, type YES manually.


Step 3: Inspect LUKS Header with luksDump

📸 Verified Output:

💡 LUKS2 supports up to 32 key slots — you can have multiple passphrases or keyfiles that all unlock the same volume. The actual encryption key is stored encrypted in each slot.


Step 4: Open the Encrypted Device

📸 Verified Output:

📸 Verified Output:


Step 5: Create a Filesystem and Mount

📸 Verified Output:


Step 6: Close the Encrypted Device

📸 Verified Output:

💡 After luksClose, the decrypted device disappears from /dev/mapper/. The data on disk is fully encrypted — without the passphrase, it's unreadable.


Step 7: Add a Second Key (luksAddKey)

📸 Verified Output:

💡 Common key slot uses: slot 0 = admin passphrase, slot 1 = recovery key, slot 2 = automation keyfile. Revoke a slot with cryptsetup luksKillSlot /dev/sda1 1.


Step 8: Capstone — Auto-mount with /etc/crypttab

In production, encrypted devices are configured to unlock at boot via /etc/crypttab.

📸 Verified Output:


Summary

Command
Purpose

cryptsetup luksFormat --batch-mode /dev/sda1

Format device with LUKS

cryptsetup luksOpen /dev/sda1 myname

Unlock → create /dev/mapper/myname

cryptsetup luksClose myname

Lock the device

cryptsetup status myname

Show active mapping info

cryptsetup luksDump /dev/sda1

Show LUKS header details

cryptsetup luksAddKey /dev/sda1 keyfile2

Add a second key/passphrase

cryptsetup luksKillSlot /dev/sda1 1

Remove key slot 1

/etc/crypttab

Auto-unlock at boot

mkfs.ext4 /dev/mapper/myname

Format decrypted device

LUKS2 Concept
Detail

Cipher

aes-xts-plain64 (default, AES 256-bit)

Key slots

Up to 32 independent passphrases/keyfiles

PBKDF

argon2id — slow hash to resist brute force

Header size

16 MiB — can be backed up with cryptsetup luksHeaderBackup

Last updated