Skip to content
Go back

LUKS Full Disk Encryption on Linux

By SumGuy 6 min read
LUKS Full Disk Encryption on Linux

Someone breaks into your rack. They yank the drives. They go home, plug them in, and read your entire Nextcloud, your SSH keys, your database dumps—everything. You discover this from a Slack notification at 3 AM. That’s when you realize that without encryption, a stolen server is just expensive recycling that happens to contain your life.

This is where LUKS comes in. LUKS (Linux Unified Key Setup) is the Linux standard for disk encryption. It’s not fancy, it’s not new, but it works. A stolen drive encrypted with LUKS becomes hostile hardware—the attacker gets a few thousand dollars in used storage and absolutely nothing else.

Here’s the thing: most people never bother. “It’ll slow things down,” they say. “Too complicated,” they say. And then someone steals something, and suddenly encryption doesn’t feel so inconvenient. Your 2 AM self will absolutely appreciate it.

Why LUKS Matters (And It’s Not What You Think)

LUKS isn’t about privacy theater. It’s about a simple mechanical fact: if someone has your drive, they have everything on it. Full stop. Encryption makes that drive inert. They can’t read it, they can’t modify it, they can’t mount it elsewhere. It becomes a brick with your name on it.

The encryption itself is rock-solid AES-256 with PBKDF2 key derivation. Cryptographically speaking, breaking it is a fun thought experiment that doesn’t happen in the real world. Your passphrase is the weak link—and that’s fine, because passphrases are under your control.

There are three flavors of encryption you could do:

Most homelabs should use option three. It’s simple, effective, and doesn’t require you to understand Grub encryption at 2 AM.

Setting Up Encrypted Partitions: The Walk-Through

Let’s say you have a secondary drive /dev/sdb that you want to encrypt. (Never experiment on your boot drive unless you enjoy reinstalling.)

First, create the LUKS container:

Terminal window
sudo cryptsetup luksFormat /dev/sdb1

Cryptsetup will ask for a passphrase. Choose something strong. This is your master key. If you lose it, the drive becomes a brick forever—and not in the good way.

Now open it (decrypt it without mounting):

Terminal window
sudo cryptsetup luksOpen /dev/sdb1 my-encrypted-drive

This creates a decrypted block device at /dev/mapper/my-encrypted-drive. Now format it like any other drive:

Terminal window
sudo mkfs.ext4 /dev/mapper/my-encrypted-drive
sudo mkdir -p /mnt/secure
sudo mount /dev/mapper/my-encrypted-drive /mnt/secure

At this point you have a mounted encrypted filesystem. Write some files, feel smug about your data security.

When you’re done:

Terminal window
sudo umount /mnt/secure
sudo cryptsetup luksClose my-encrypted-drive

The drive is now locked. Pulling the power won’t help an attacker—they’d need the passphrase.

Auto-Mounting at Boot (Because You’re Not Typing a Passphrase Every Time)

Typing a passphrase every time you reboot is technically secure but practically annoying. For a headless server, it’s impossible. Here’s where keyfiles come in.

Create a random keyfile:

Terminal window
sudo dd if=/dev/urandom of=/root/.luks/drive-key bs=1 count=512
sudo chmod 600 /root/.luks/drive-key

Add it as an alternative unlock method:

Terminal window
sudo cryptsetup luksAddKey /dev/sdb1 /root/.luks/drive-key

Cryptsetup will ask for the original passphrase first (to verify you own the drive), then adds the keyfile as an additional way to unlock. Keep both—they’re independent keys.

Now add an entry to /etc/crypttab:

/etc/crypttab
my-encrypted-drive /dev/sdb1 /root/.luks/drive-key luks

And an entry to /etc/fstab:

/etc/fstab
/dev/mapper/my-encrypted-drive /mnt/secure ext4 defaults 0 2

Next boot, the drive unlocks automatically using the keyfile. No passphrase, no intervention. Your 2 AM self is happy.

The Dropbear Trick (Unlocking Headless Servers Remotely)

Here’s the problem: you encrypted a remote server’s root filesystem with a passphrase. It boots, hits the LUKS prompt, and sits there waiting forever because nobody’s at the console. Your server is dead until someone shows up to type the passphrase.

The solution is Dropbear SSH—a lightweight SSH server in the initramfs that lets you unlock the drive remotely.

On Debian/Ubuntu:

Terminal window
sudo apt install dropbear-initramfs
sudo update-initramfs -u

On most distributions, just installing dropbear-initramfs is enough. The package hooks into initramfs and drops an SSH server into the boot environment. You can now SSH into your server before the filesystem is mounted, unlock LUKS, and boot normally.

At 3 AM when your server fails to boot, you SSH in, type cryptsetup-askpass, enter the passphrase, and your system boots. No driving to the datacenter. No console access needed.

Key Management: Adding, Rotating, and Backup

LUKS supports up to 8 keys. If one keyfile leaks, revoke it without recreating the entire encrypted container:

Terminal window
sudo cryptsetup luksRemoveKey /dev/sdb1 /root/.luks/old-key

To add another key:

Terminal window
sudo cryptsetup luksAddKey /dev/sdb1 /root/.luks/backup-key

Backup your LUKS header (not the actual data—just the encryption metadata):

Terminal window
sudo cryptsetup luksHeaderBackup /dev/sdb1 --header-backup-file /root/luks-header-backup.img

Store this file somewhere safe and separate—a USB stick in a safe, a password manager, whatever. If your drive header gets corrupted, you can restore it:

Terminal window
sudo cryptsetup luksHeaderRestore /dev/sdb1 --header-backup-file /root/luks-header-backup.img

Without the header, the drive is permanently locked. The header is not secret—it just contains the metadata LUKS needs to decrypt the drive. Backing it up is not a security risk.

What Not To Do (The Mistakes Checklist)

Wrapping Up

LUKS encryption takes about fifteen minutes to set up and gives you the peace of mind that a stolen drive is just a brick. Your database dumps won’t leak. Your SSH keys won’t be cloned. Your Nextcloud stays yours.

The worst-case scenario is no longer “someone steals my drive and reads everything.” It’s “someone steals my drive and gets a paperweight.” Call that a win.

Set it up now, while you’re thinking about it. Your 3 AM self (and your data) will thank you.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Podman Quadlets: Systemd-Native Containers
Next Post
Rootless Docker: Run Without Root

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts