Linux Filesystem Encryption Explained: LUKS2, fscrypt, Setup, and Recovery
Linux filesystem encryption protects data stored on a machine when an attacker can read its storage without first passing the normal login or authorization checks. It is useful for laptops, removable media, servers, and application data, but the right design depends on whether you need to encrypt an entire block device or only selected directories. This guide explains the distinction, shows how LUKS2 and fscrypt fit into the Linux storage stack, and uses a disposable loopback image for the practical example so a real disk is not accidentally erased.
What Is Linux Filesystem Encryption?
Filesystem encryption converts file content, and in some designs filenames, into ciphertext that can only be read after the required key or passphrase is available. The term is used broadly for two different layers:
- Block-device encryption places an encrypted mapping below the filesystem. LUKS2 with
dm-cryptis the normal Linux choice for a complete partition, volume, or removable drive. - File-based encryption places encryption inside the filesystem.
fscryptapplies policies to directories on supported filesystems such as ext4 and F2FS, allowing different directories to have different keys.
Both approaches are transparent to applications after unlock: a process reads and writes ordinary paths, while the kernel performs the cryptographic operations. Neither approach is a replacement for permissions, backups, secure boot, patching, or encryption in transit.
The Problem: What Does Encryption Need to Protect?
Start with the threat model rather than the command. Encryption at rest is primarily designed for an offline attacker who removes a drive, boots another operating system, or obtains an unmounted backup. It is especially valuable for:
- stolen or lost laptops and removable drives;
- decommissioned disks that still contain recoverable data;
- server volumes containing customer records, credentials, or private keys;
- backups and snapshots that leave the original host.
Encryption does not automatically protect data while the volume is unlocked. A compromised account, malicious process, administrator with access to the mounted path, memory scraping, or an application that leaks plaintext can still expose it. It also does not prevent deletion or corruption. A sound design therefore combines encryption with least-privilege permissions, tested backups, endpoint controls, and monitoring.
The scope matters. Full-volume encryption also covers filesystems, swap, and free space inside the encrypted device, while directory encryption can leave filenames, file sizes, timestamps, directory structure, or other filesystem metadata visible depending on the implementation and filesystem. If a threat can observe a running system, choose controls for that threat separately.
How Linux Filesystem Encryption Works
LUKS2 and dm-crypt architecture
LUKS2 is a standard on-disk format for storing encryption metadata. cryptsetup manages the LUKS header and asks the kernel’s device-mapper (dm-crypt) layer to provide the encrypted block mapping. The data key is stored in the LUKS metadata encrypted by one or more keyslots; a passphrase unlocks a keyslot rather than directly encrypting every sector.
The I/O path looks like this:
Application
-> mounted filesystem (ext4, XFS, Btrfs, ...)
-> /dev/mapper/securedata
-> dm-crypt encrypts/decrypts blocks
-> LUKS2 container on a partition, logical volume, or image
-> physical storage
When cryptsetup open succeeds, the mapper device becomes available and the filesystem can be mounted. Closing the mapper removes the normal path to the plaintext. LUKS2 supports multiple keyslots, so an administrator can add a recovery passphrase or rotate access without re-encrypting every data block. A header backup is valuable for recovery, but it is sensitive: anyone who has the header and a valid passphrase may be able to unlock the data.
See the cryptsetup-luksFormat manual for the format operation and options. Do not treat luksFormat as a harmless initialization command; it overwrites the target’s existing data.
fscrypt architecture
fscrypt uses the Linux filesystem encryption API. The filesystem stores encrypted file content and can encrypt filenames, while the kernel applies a policy to a directory and its descendants. A user or service unlocks the policy, after which authorized processes can use the normal path.
The flow is different from LUKS:
Application
-> encrypted directory policy
-> filesystem encryption support (ext4, F2FS, or UBIFS)
-> block device and its existing storage layer
This makes fscrypt useful when only selected directories need protection, when separate users or services need separate policies, or when the rest of the filesystem must remain available during boot. It also means that filesystem metadata and the underlying device are not necessarily hidden to the same extent as with a complete LUKS volume. Read the Linux kernel fscrypt documentation before relying on a policy for a particular filesystem or threat model.
Components and Variants
| Approach | Encryption boundary | Best fit | Important limitation |
|---|---|---|---|
LUKS2 with dm-crypt |
Partition, logical volume, disk, or image | Laptops, removable media, databases, and complete data volumes | The volume must be unlocked before its files can be used |
fscrypt |
Directory tree on a supported filesystem | Selective user or application data encryption | Some filesystem metadata and the rest of the host remain outside the policy |
| Application encryption | Individual records, objects, or files | Data that must remain protected from storage administrators or backups | Applications must handle keys, rotation, search, and recovery correctly |
| eCryptfs | Stacked per-directory filesystem | Legacy systems that already depend on it | It is a legacy choice; use LUKS2 or fscrypt for new Linux deployments |
| Encrypted backups | Backup archive or repository | Copies stored off-host or in the cloud | It protects the copy, not an already-compromised live system |
LUKS is a format and key-management layer, not a filesystem. You still create ext4, XFS, Btrfs, or another filesystem on the unlocked mapping. Similarly, fscrypt is not a backup system and does not remove the need to protect the host’s key material.
Real-World Use Cases
Laptops and removable drives
Use LUKS2 for a complete data partition or system installation so swap, temporary files, and deleted blocks inside the encrypted boundary are covered. Keep the recovery key separate from the laptop. Hardware-backed unlock can improve usability, but it should not eliminate a recovery path.
Server data volumes
LUKS2 is a good fit for a database, backup, or object-storage volume when the host must unlock one boundary during boot or service startup. Put the key retrieval workflow under deliberate access control; automatic unlock trades convenience for protection against a stolen but still-powered or unattended machine.
Multi-user application data
Use fscrypt when a host needs to keep the operating system and most services running while protecting selected user or service directories. Combine it with Unix ownership, ACLs, service isolation, and careful key provisioning. Directory encryption should not be used as a way to grant access to a process that already runs with broad root privileges.
Backups and snapshots
Encrypt backups independently of the source volume. A LUKS header backup, cloud snapshot, database export, or filesystem snapshot can expose sensitive material if it is copied without the same key-management discipline as the primary data. Practice a restore on a disposable host, not only an unlock operation.
Practical Considerations and Setup Guide
Choose the boundary first
Use this decision rule:
- Choose LUKS2 when the entire partition, volume, swap area, or removable device should be protected while offline.
- Choose fscrypt when selected directories need independent policies and the host must keep other paths available.
- Add application-level encryption when storage operators or backup administrators must not be able to read plaintext after the application is running.
- Encrypt backups separately and document who can retrieve each key.
Before changing storage, confirm the device path, make and test a backup, reserve a recovery location, and verify that the distribution supports the required cryptsetup or fscrypt features. Test on a VM or disposable image first.
Safely test a LUKS2 volume with a loopback image
The following example creates a small file-backed device. It is a lab exercise and is not a substitute for a production partitioning or boot plan. It intentionally uses variables instead of a real block-device path.
# Install cryptsetup using your distribution's package manager.
# Debian/Ubuntu example:
sudo apt update
sudo apt install cryptsetup
# Create a disposable 2 GiB image and attach it to a loop device.
IMAGE="$HOME/secure-demo.img"
truncate -s 2G "$IMAGE"
LOOP_DEVICE=$(sudo losetup --find --show "$IMAGE")
# DESTRUCTIVE: initializes the loop device as a LUKS2 container.
sudo cryptsetup luksFormat --type luks2 "$LOOP_DEVICE"
# Unlock the container as /dev/mapper/secure-demo.
sudo cryptsetup open "$LOOP_DEVICE" secure-demo
# Create a filesystem inside the unlocked mapping and mount it.
sudo mkfs.ext4 /dev/mapper/secure-demo
sudo mkdir -p /mnt/secure-demo
sudo mount /dev/mapper/secure-demo /mnt/secure-demo
# Use the mounted path, then inspect the active mapping.
findmnt /mnt/secure-demo
sudo cryptsetup status secure-demo
When finished, unmount before closing the mapping. Detach the loop device only after the mapping is closed:
sudo umount /mnt/secure-demo
sudo cryptsetup close secure-demo
sudo losetup --detach "$LOOP_DEVICE"
For a real partition, replace the loop device only after checking it with lsblk -f and cryptsetup luksDump. Never run luksFormat on a path you have not positively identified.
Inspect keyslots and create a header backup
Keyslots are the access paths to the volume key. Add a second passphrase before removing the first one, and confirm that the new key works in a separate unlock test:
# Replace the loop device with the correct LUKS device in a real deployment.
sudo cryptsetup luksDump "$LOOP_DEVICE"
sudo cryptsetup luksAddKey "$LOOP_DEVICE"
# Store this backup on protected, separate media. It is not the data backup.
sudo cryptsetup luksHeaderBackup "$LOOP_DEVICE" \
--header-backup-file "$HOME/secure-demo-luks-header.img"
Protect the header backup like a secret and do not store it beside the only copy of the encrypted data. A header backup cannot restore files by itself; it restores metadata so a known key can be tried against the container. Test both the header recovery procedure and the data backup restore procedure as part of an incident runbook.
Configure a persistent mapping deliberately
For a production volume, use stable identifiers rather than device names. A typical design uses the LUKS UUID in the crypttab file and the filesystem UUID in the fstab file:
# /etc/crypttab
securedata UUID=<luks-uuid> none luks
# /etc/fstab
UUID=<filesystem-uuid> /srv/securedata ext4 defaults,nofail 0 2
Generate and verify the values with cryptsetup luksUUID, blkid, and findmnt. nofail is only appropriate when an unavailable data volume should not block boot; choose boot behavior based on the service’s availability and recovery requirements. Do not put a passphrase directly in a world-readable configuration file.
Apply fscrypt to a selected directory
On a supported filesystem, install the distribution’s fscrypt package and initialize the filesystem once. The exact integration with login keyrings differs by distribution, so test the unlock workflow for the account or service that will use the directory:
# Example workflow; read the fscrypt package documentation for your distribution.
sudo fscrypt setup /srv/data
mkdir -p /srv/data/private
sudo fscrypt encrypt /srv/data/private --user="$USER"
# Confirm the policy and test access as the intended user.
sudo fscrypt status /srv/data/private
ls -la /srv/data/private
The fscrypt project documentation explains policy provisioning, recovery options, and supported filesystems. Do not assume that creating an encrypted directory also encrypts an existing unencrypted copy, a different mount, or a backup made before the policy was applied.
Manage keys, backups, and recovery
- Keep recovery keys in a separate access-controlled system, with enough redundancy to survive a lost administrator account.
- Back up plaintext through an encrypted backup tool or back up ciphertext only when the restore process and keys are understood.
- Record the LUKS UUID, filesystem UUID, key owner, unlock dependencies, and emergency contacts without recording the passphrase itself.
- Include swap, hibernation, temporary exports, snapshots, and logs in the data-flow review.
- Test restores on an isolated machine and verify that applications can read the recovered data.
- Monitor failed unlocks and unexpected mount events, but avoid logging passphrases or key material.
Common Misconceptions
“Encryption protects files after the system is unlocked.”
Usually it does not. Once a LUKS mapping is open or an fscrypt policy is unlocked, authorized processes receive plaintext through normal filesystem APIs. Use permissions, MAC policies, service isolation, and application encryption for threats against a running host.
“LUKS encrypts the whole computer automatically.”
LUKS encrypts the device or partition to which it is applied. A separate unencrypted boot partition, another data disk, a swap area, a backup, or a cloud snapshot can still disclose information. Map the complete data lifecycle.
“A header backup is a data backup.”
It is not. The header contains the metadata required to interpret a LUKS container, while the encrypted sectors contain the files. Keep both recovery metadata and independent file backups, and protect each according to its sensitivity.
“RAID or a snapshot replaces encryption or a backup.”
RAID improves availability after some hardware failures, and snapshots provide point-in-time views. Neither one prevents an attacker with storage access from reading plaintext nor guarantees recovery from deletion, ransomware, corruption, or a lost key.
“The strongest cipher setting fixes poor key management.”
A strong cipher cannot compensate for a weak passphrase, an exposed recovery key, an unencrypted backup, or an automatically unlocked server. The operational path for issuing, rotating, revoking, and recovering keys is part of the security design.
Related Articles
- Linux storage management: disks, filesystems, LVM, RAID, and best practices covers the storage layers that sit around an encrypted mapping.
- Storage encryption technologies compared compares full-disk, file-level, drive, cloud, and application encryption.
- Linux security hardening with AppArmor explains how mandatory access controls complement encryption on a running host.
- BitLocker administration and monitoring provides the Windows enterprise comparison for endpoint encryption management.

