๐ Quick Pill: LUKS Encrypted Container
Info
Use case: Create encrypted file containers for secure backup storage, sensitive documents, or portable encrypted volumes. Works like a virtual encrypted disk.
๐ Quick Setup (5 minutes)
1. Install Required Tools
sudo apt install cryptsetup
2. Create Encrypted Container
# Create a 10GB empty file (fast allocation)
fallocate -l 10G BackupCrypt.img
# Initialize LUKS encryption (you'll set a password)
sudo cryptsetup luksFormat BackupCrypt.img
# Open the encrypted container
sudo cryptsetup open BackupCrypt.img backup_crypt
# Create filesystem inside
sudo mkfs.ext4 /dev/mapper/backup_crypt
# Mount it
sudo mkdir -p /mnt/encrypted
sudo mount /dev/mapper/backup_crypt /mnt/encrypted
# Set permissions (optional - for your user)
sudo chown $USER:$USER /mnt/encrypted
3. Use Your Encrypted Storage
# Copy files
cp -r ~/Documents/sensitive/ /mnt/encrypted/
# Work with files normally
echo "Secret data" > /mnt/encrypted/secret.txt
4. Close Encrypted Container
# Unmount
sudo umount /mnt/encrypted
# Close LUKS container
sudo cryptsetup close backup_crypt
Success
Done! Your data is now encrypted. Without the password, the file looks like random data.
๐ Daily Usage
Open and Mount
# Open with password prompt
sudo cryptsetup open BackupCrypt.img backup_crypt
# Mount
sudo mount /dev/mapper/backup_crypt /mnt/encrypted
Close and Lock
# Always unmount first
sudo umount /mnt/encrypted
# Then close
sudo cryptsetup close backup_crypt
๐ Command Reference
Create Container
| Command | Purpose | Notes |
|---|---|---|
fallocate -l SIZE file.img | Create empty file (fast) | Sizes: 1G, 10G, 100G, etc. |
dd if=/dev/zero of=file.img bs=1M count=10240 | Create file (slower, more secure) | Overwrites with zeros |
cryptsetup luksFormat file.img | Initialize LUKS encryption | Sets password |
Open/Close
| Command | Purpose |
|---|---|
cryptsetup open file.img name | Open encrypted container |
cryptsetup close name | Close encrypted container |
cryptsetup status name | Check if container is open |
Filesystem
| Command | Purpose |
|---|---|
mkfs.ext4 /dev/mapper/name | Create ext4 filesystem |
mkfs.xfs /dev/mapper/name | Create XFS filesystem |
mkfs.btrfs /dev/mapper/name | Create Btrfs filesystem |
๐ก Pro Tips
Create container with dd (more secure, slower)
Use dd instead of fallocate for better security - overwrites the file with zeros:
# Create 10GB file (10240 MB)
dd if=/dev/zero of=BackupCrypt.img bs=1M count=10240 status=progress
# Or use random data (MUCH slower but most secure)
dd if=/dev/urandom of=BackupCrypt.img bs=1M count=10240 status=progress
When to use:
fallocate: Fast, good for most casesddwith/dev/zero: Secure, prevents data recoveryddwith/dev/urandom: Maximum security, very slow
Add multiple passwords (key slots)
LUKS supports up to 8 passwords:
# Add a second password
sudo cryptsetup luksAddKey BackupCrypt.img
# Remove a password (need existing password)
sudo cryptsetup luksRemoveKey BackupCrypt.img
# List key slots
sudo cryptsetup luksDump BackupCrypt.img
Auto-mount with /etc/fstab
For permanent mounting (not recommended for portable containers):
# Add to /etc/fstab
/dev/mapper/backup_crypt /mnt/encrypted ext4 defaults,noauto 0 0
# Then mount with
sudo mount /mnt/encrypted
Backup LUKS header (CRITICAL!)
If the header is corrupted, your data is permanently lost:
# Backup header
sudo cryptsetup luksHeaderBackup BackupCrypt.img --header-backup-file BackupCrypt_header.img
# Store this file SEPARATELY from your encrypted container!
# Restore header if needed
sudo cryptsetup luksHeaderRestore BackupCrypt.img --header-backup-file BackupCrypt_header.img
Create script for easy mounting
Save as mount_encrypted.sh:
#!/bin/bash
CONTAINER="$HOME/BackupCrypt.img"
MAPPER_NAME="backup_crypt"
MOUNT_POINT="/mnt/encrypted"
if [ "$1" = "open" ]; then
sudo cryptsetup open "$CONTAINER" "$MAPPER_NAME"
sudo mkdir -p "$MOUNT_POINT"
sudo mount "/dev/mapper/$MAPPER_NAME" "$MOUNT_POINT"
sudo chown $USER:$USER "$MOUNT_POINT"
echo "โ
Encrypted container mounted at $MOUNT_POINT"
elif [ "$1" = "close" ]; then
sudo umount "$MOUNT_POINT"
sudo cryptsetup close "$MAPPER_NAME"
echo "๐ Encrypted container closed"
else
echo "Usage: $0 {open|close}"
fi
Make executable and use:
chmod +x mount_encrypted.sh
./mount_encrypted.sh open # Mount
./mount_encrypted.sh close # Unmount
Use key file instead of password
For automation (less secure, convenient):
# Generate random key file
dd if=/dev/urandom of=~/backup.key bs=512 count=1
chmod 600 ~/backup.key
# Add key file to LUKS
sudo cryptsetup luksAddKey BackupCrypt.img ~/backup.key
# Open with key file (no password prompt)
sudo cryptsetup open BackupCrypt.img backup_crypt --key-file ~/backup.key
# โ ๏ธ Protect the key file! Anyone with it can decrypt your data
Check container status and info
# Is container open?
sudo cryptsetup status backup_crypt
# Show LUKS header info
sudo cryptsetup luksDump BackupCrypt.img
# Check filesystem
sudo fsck -f /dev/mapper/backup_crypt
# Show disk usage
df -h /mnt/encrypted
๐ฏ Common Use Cases
Encrypted Backup Storage
# Create large container for backups
fallocate -l 100G encrypted_backup.img
sudo cryptsetup luksFormat encrypted_backup.img
sudo cryptsetup open encrypted_backup.img backup
sudo mkfs.ext4 /dev/mapper/backup
sudo mount /dev/mapper/backup /mnt/backup
# Backup with rsync
rsync -av --progress ~/Documents/ /mnt/backup/
# Close when done
sudo umount /mnt/backup
sudo cryptsetup close backup
Portable Encrypted USB
# Create small container for USB drive
fallocate -l 2G portable.img
sudo cryptsetup luksFormat portable.img
# ... setup filesystem ...
# Copy to USB
cp portable.img /media/usb/
# Use anywhere with cryptsetup installed!
Cloud Storage Encryption
# Create container for cloud sync
fallocate -l 5G cloud_vault.img
# ... setup ...
# Sync to Dropbox/Drive (encrypted!)
cp cloud_vault.img ~/Dropbox/
๐ Understanding File Sizes
# Check file size
ls -lh BackupCrypt.img
# Check actual disk usage
du -h BackupCrypt.img
# Check available space inside
df -h /mnt/encrypted
With fallocate:
- File size: 10G (sparse file)
- Disk usage: Usually smaller until filled
- Fast creation
With dd:
- File size: 10G
- Disk usage: Exactly 10G immediately
- Slower creation, more secure
โ ๏ธ Important Warnings
Danger
Critical Points:
- Backup your LUKS header: Without it, your data is unrecoverable
- Remember your password: No password recovery possible
- Always unmount before closing: Prevents data corruption
- Close before shutdown: Don’t leave containers open
- Secure your key files: They’re as sensitive as passwords
๐ง Troubleshooting
Device or resource busy when closing
Problem: Cannot close container, “device is busy”
Solution:
# Find what's using it
sudo lsof /mnt/encrypted
# Force unmount if needed
sudo umount -l /mnt/encrypted
# Then close
sudo cryptsetup close backup_crypt
Password not accepted
Problem: “No key available with this passphrase”
Solution:
# Verify LUKS header is intact
sudo cryptsetup luksDump BackupCrypt.img
# Try all key slots (0-7)
sudo cryptsetup open BackupCrypt.img backup_crypt --key-slot 0
sudo cryptsetup open BackupCrypt.img backup_crypt --key-slot 1
# ... etc
# If truly forgotten: data is LOST (by design)
# Restore from header backup if available
Container file corrupted
Problem: Cannot open container, errors about header
Solution:
# Restore header backup (if you made one!)
sudo cryptsetup luksHeaderRestore BackupCrypt.img \
--header-backup-file BackupCrypt_header.img
# Check filesystem after opening
sudo cryptsetup open BackupCrypt.img backup_crypt
sudo fsck -f /dev/mapper/backup_crypt
Permission denied when accessing files
Problem: Cannot write to mounted encrypted volume
Solution:
# Change ownership to your user
sudo chown -R $USER:$USER /mnt/encrypted
# Or set permissions
sudo chmod -R 755 /mnt/encrypted
๐ Size Recommendations
| Use Case | Recommended Size | Creation Method |
|---|---|---|
| Small secrets | 100M - 1G | fallocate |
| Personal docs | 5G - 10G | fallocate |
| Photo backup | 50G - 100G | fallocate or dd |
| Full system backup | 100G+ | dd (security) |
| Cloud sync | 1G - 5G | dd (security) |
๐ Security Levels
| Method | Security | Speed | Best For |
|---|---|---|---|
| fallocate | Good | Very fast | General use |
| dd + /dev/zero | Better | Medium | Important data |
| dd + /dev/urandom | Best | Very slow | Maximum security |
๐ Quick Commands Cheat Sheet
# CREATE
fallocate -l 10G backup.img
sudo cryptsetup luksFormat backup.img
sudo cryptsetup open backup.img vault
sudo mkfs.ext4 /dev/mapper/vault
sudo mount /dev/mapper/vault /mnt/vault
# USE
cd /mnt/vault
# ... work with files ...
# CLOSE
sudo umount /mnt/vault
sudo cryptsetup close vault
# BACKUP HEADER (DO THIS!)
sudo cryptsetup luksHeaderBackup backup.img --header-backup-file backup_header.img
# CHECK STATUS
sudo cryptsetup status vault
df -h /mnt/vault
Keep your sensitive data secure with LUKS encryption!