Data Encryption in Practice: Disk, Database, and Transport
Data encryption works at three layers: disk encryption for data at rest, database encryption at rest, and TLS for data in transit. Each layer has its own job: disk encryption protects against physical media theft, database encryption protects against leaked backups and unauthorized internal reads, and TLS protects against link eavesdropping. Most sites only implement the third layer (HTTPS) and often overlook the first two.
Disk encryption: LUKS and cryptsetup
LUKS is the mainstream disk encryption standard on Linux, backed by the kernel's dm-crypt. It can encrypt whole disks, partitions, RAID volumes, logical volumes, and even single file containers, transparently to the file system above.
cryptsetup luksFormat /dev/sdX
cryptsetup open /dev/sdX cryptroot
A few more commands worth knowing, covering backup and recovery:
# Back up the LUKS header (do this before writing any data)
cryptsetup luksHeaderBackup /dev/sdX --header-backup-file luks-header.bak
# Restore the header
cryptsetup luksHeaderRestore /dev/sdX --header-backup-file luks-header.bak
# Inspect encryption parameters and key slots
cryptsetup luksDump /dev/sdX
An automated /etc/crypttab for boot-time unlock looks like this — keyfiles should be chmod 400 and stored separately:
cryptroot /dev/sdX2 /etc/luks/root.key luks
cryptswap /dev/sdX3 /etc/luks/swap.key luks
Reference: the full ArchWiki dm-crypt page https://wiki.archlinux.org/title/Dm-crypt
Implementation notes:
- The swap partition must also be encrypted, otherwise memory contents get swapped out to plaintext disk.
- Automated unlock at boot can use
/etc/crypttabwith a keyfile; store the keyfile separately and restrict its permissions. - Always back up the LUKS header (
cryptsetup luksHeaderBackup); a damaged header makes the data permanently unreadable. - In the cloud, encrypt the root partition and keep keys separate from data, combined with a Backup Strategy.
Database encryption: at rest and at field level
There are two common approaches at the database layer:
- Instance-level encryption at rest (TDE): MySQL/MariaDB and PostgreSQL encrypt data files automatically; it is transparent but the keys are usually managed by the database.
- Field-level encryption: the application encrypts sensitive fields (IDs, phone numbers, payment data) individually, with keys managed by the application or a Secrets Management system, offering stronger protection.
Field-level encryption forces trade-offs in queries, indexing, and sorting, so enable it only for genuinely sensitive fields, and combine it with SQL Injection Defense to prevent ciphertext from being bypassed.
The trade-offs between the two approaches:
| Dimension | Instance-level TDE | Field-level encryption |
|---|---|---|
| What is encrypted | The whole data files | Specified sensitive fields |
| Key management | Built into the database | Application/secrets management |
| Impact on queries | None | Indexing, sorting, and search need rework |
| Protection ceiling | Backups and file leaks | Can also stop application-layer over-read |
TDE protects at the file level; field-level encryption protects at the field level, and they do not conflict. Using TDE for the great majority of fields while layering field-level encryption on top for truly sensitive ones — IDs, payment tokens — is the most cost-effective combination.
Transport encryption: TLS deployment
Transport encryption is the HTTPS we all know. For certificate issuance and automatic renewal see SSL Certbot Automation, and for full migration see the HTTPS Migration Guide. Beyond the web, internal links such as database replication and Redis should also use TLS or at least stay inside encrypted private networks or dedicated lines.
Key management and backup
The security of any encryption system ultimately rests on the keys:
- Store keys separately from ciphertext, never on the same machine or disk.
- Establish a key rotation plan; disk keys, database keys, and TLS private keys rotate on different cycles.
- Losing a key means the data is permanently unreadable, so key escrow and recovery drills are essential.
How the three layers work together
The three layers do not replace each other — they stack:
- Disk encryption keeps media confidential even if the drive is physically removed.
- Database encryption at rest keeps data files and backups confidential, so "having the backup" no longer equals "having the data".
- TLS keeps the transport confidential, blocking packet capture and man-in-the-middle tampering.
Here is what each layer defends against, who holds the keys, and the usual tools:
| Layer | Defends against | Who holds the keys | Typical tools |
|---|---|---|---|
| Disk encryption | Physical media theft | The system at boot | LUKS/dm-crypt, BitLocker |
| Database encryption at rest | Leaked backups, unauthorized file reads | Database/application | TDE, field-level encryption |
| TLS | Link eavesdropping, MITM tampering | Cert/private key escrow | Let's Encrypt, mTLS |
For compliance scenarios, all three layers are usually required at once, and the audit must show key lifecycle management is sound. System- and database-managed keys cover the disk and database layers; field-level keys and TLS private keys are best held by a Secrets Management system for easier rotation and audit.
A lesson from a real incident
A startup had HTTPS and database backups, but no disk encryption. During a data-center move, ops accidentally sent two old drives to e-waste — one of them was the primary database disk. The backups were intact, but the compliance audit treated "readable media leaving the building" as a reportable event requiring a data-exposure assessment, because there was no way to prove the plaintext database had never been touched. The audit and legal process dragged on for over two months.
Had they done full-disk LUKS encryption with the header properly backed up, that drive would have been nothing but ciphertext and the problem would have been far smaller. The lesson is blunt: disk encryption protects not only against theft, but against the inability to prove you were not stolen from.
Common mistakes
- Encrypting only the data disk and forgetting swap and temp directories.
- Leaving backups unencrypted, which is equivalent to copying out plaintext.
- Storing keys on the same machine and disk as the ciphertext, which defeats the point.
- Forgetting to back up the LUKS header — once it is damaged, the whole disk is unrecoverable.
16IDC Observation
For compliance-sensitive e-commerce, SaaS, and payment sites, three-layer encryption is a baseline, not a bonus. Smaller sites should at minimum achieve: full disk encryption + HTTPS + encrypted database backups, then round out the picture with the baseline approaches in the Security Hardening category.
Configuration & Integration Examples
Example 1: LUKS format, mount and header backup
#!/usr/bin/env bash
set -euo pipefail
DEV="${LUKS_DEVICE:?e.g. /dev/nvme0n1p3}"
NAME="${LUKS_NAME:-data_crypt}"
# 1) Format: LUKS2 + argon2id; the passphrase is entered interactively
sudo cryptsetup luksFormat --type luks2 --cipher aes-xts-plain64 \
--key-size 512 --hash sha512 "$DEV"
# 2) Open, create a filesystem and mount it
sudo cryptsetup luksOpen "$DEV" "$NAME"
sudo mkfs.ext4 -L "$NAME" "/dev/mapper/$NAME"
sudo mount "/dev/mapper/$NAME" /srv/data
# 3) Back up the header immediately: a lost header means lost data
sudo cryptsetup status "$NAME"
sudo cryptsetup luksHeaderBackup "$DEV" \
--header-backup-file "/root/${NAME}-header-$(date +%F).img"
A damaged LUKS header means permanent data loss, so back it up right after formatting and store it offline.
Example 2: Database TLS verification and certificate inspection
#!/usr/bin/env bash
set -euo pipefail
HOST="${DB_HOST_NAME:?}" ; PORT="${DB_PORT:-5432}"
# verify-full checks the chain and the hostname; require only encrypts
psql "host=$HOST port=$PORT dbname=${DB_NAME} user=${DB_USER} \
sslmode=verify-full sslrootcert=${CA_FILE}" \
-c "select ssl, version from pg_stat_ssl where pid = pg_backend_pid();"
# Independently inspect validity dates and SAN coverage
echo | openssl s_client -connect "$HOST:$PORT" -servername "$HOST" -CAfile "$CA_FILE" 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates -ext subjectAltName
sslmode=verify-full validates both chain and hostname; require alone encrypts without verifying identity.
Failure Symptoms and Recovery Prerequisites for Each Layer
The three layers fail differently: disk encryption fails as "cannot open", the database layer fails as "opened, but no idea who the peer is", and field-level encryption fails as "the key leaves together with the ciphertext". Different symptoms allow different responses.
The table lists each layer's typical failure, the symptom on site and what recovery depends on. Commands and options come from the body text and example scripts above.
| Layer | Typical failure | Symptom on site | Recovery depends on |
|---|---|---|---|
| Disk (LUKS header) | header corrupted or reformatted | container will not unlock, data unreadable | the luksHeaderBackup image taken before writing data |
| Disk (swap and temp dirs) | memory pages swapped to a plaintext disk | no error at all; no way to prove nothing leaked during an audit | re-planning partitions so swap is encrypted too |
| Database TLS | sslmode=require encrypts without identifying the peer |
an expired certificate raises no error and the connection still opens | switching to verify-full with a CA file |
| Database field-level | key stored on the same host and disk as the ciphertext | an unauthorised read can still decrypt | moving keys into a secrets system on a separate host |
Only the disk layer has a stated recovery command: cryptsetup luksHeaderRestore. It requires a header image backed up offline before data was written; without that image there is no second path — which is why "back up the header right after formatting" is step one.
TLS troubleshooting follows a fixed order: print validity and SAN with openssl s_client -connect and check the hostname is covered; confirm the connection string says verify-full; then use pg_stat_ssl to prove this connection actually uses TLS. An expired certificate raises the same class of error, so identify the failing ring before swapping CAs.
Field-level encryption has no recovery step, only a separation step taken in advance. With key and ciphertext on the same machine and disk, encryption only rewrites the plaintext — anyone with the unauthorised read also has the means to decrypt.
Limitations
- Figures reflect public information at publication; terms, prices and quotas follow the provider's current published terms.
- This article reflects public information as of 2026-08-06; terms, prices and quotas follow the provider's current published terms.
- Third-party capability and limit statements follow their official documentation; no further commitment is implied here.
FAQ
How should LUKS keys be stored?
Separately from the ciphertext, offline, and with tight permissions. The key file used by /etc/crypttab should be mode 400 and kept apart from the disk; back up the header with cryptsetup luksHeaderBackup before any rebuild, because a damaged header makes the volume unreadable and a lost key has no recovery path.
Why does the database reject the TLS certificate?
Check sslmode and the CA file first. require encrypts without verifying the peer, while verify-full validates both the chain and the hostname; print the validity dates and SAN with openssl s_client -connect and confirm the hostname is covered before swapping the CA; check pg_stat_ssl afterwards to prove the live connection is encrypted.
What prerequisites does disk encryption need?
An exclusive device and data you can rebuild. cryptsetup luksFormat wipes the container, so it has to happen before any data lands; encrypt swap and temp directories too, otherwise memory contents spill to plaintext disk, and keep key files on a different host. Format as LUKS2 only after confirming the device path.
Related Providers
- Let's Encrypt: when public hostnames need automated issuance and 90-day renewal tied to certbot rotation.
- Vault: when disk keys, database field keys and TLS private keys must be centralised and rotated on separate cycles.
- Sectigo: when certificate volume outgrows manual handling and internal plus public certificates need one issuance and expiry queue.
Community & Resources
Official docs and communities
- cryptsetup manual — LUKS commands and options.
- PostgreSQL: SSL support — Server certificates and sslmode semantics.
- RFC 8446: TLS 1.3 — The transport security protocol standard.
- RFC 5280: X.509 certificates — Certificate and CRL format definitions.
Learning resources and tools
- cryptsetup repository — Release notes and issue tracking.
- Arch Wiki: dm-crypt full disk encryption — Step-by-step procedure and pitfalls.
- PostgreSQL libpq SSL — Client connection parameters and certificates.
- Stack Overflow: luks tag — Recovery and mount questions.
Original source: ArchWiki dm-crypt / LUKS ↗