๐Ÿ’Š Quick Pill: LUKS Encrypted Container

๐Ÿ” 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

๐Ÿ”„ 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

CommandPurposeNotes
fallocate -l SIZE file.imgCreate empty file (fast)Sizes: 1G, 10G, 100G, etc.
dd if=/dev/zero of=file.img bs=1M count=10240Create file (slower, more secure)Overwrites with zeros
cryptsetup luksFormat file.imgInitialize LUKS encryptionSets password

Open/Close

CommandPurpose
cryptsetup open file.img nameOpen encrypted container
cryptsetup close nameClose encrypted container
cryptsetup status nameCheck if container is open

Filesystem

CommandPurpose
mkfs.ext4 /dev/mapper/nameCreate ext4 filesystem
mkfs.xfs /dev/mapper/nameCreate XFS filesystem
mkfs.btrfs /dev/mapper/nameCreate 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 cases
  • dd with /dev/zero: Secure, prevents data recovery
  • dd with /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

๐Ÿ”ง 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 CaseRecommended SizeCreation Method
Small secrets100M - 1Gfallocate
Personal docs5G - 10Gfallocate
Photo backup50G - 100Gfallocate or dd
Full system backup100G+dd (security)
Cloud sync1G - 5Gdd (security)

๐Ÿ” Security Levels

MethodSecuritySpeedBest For
fallocateGoodVery fastGeneral use
dd + /dev/zeroBetterMediumImportant data
dd + /dev/urandomBestVery slowMaximum 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!