Encrypting Partitions with LUKS using cryptsetup: A Guide

Jan 2, 2025    #guide  

Prerequisites

Installing cryptsetup

Choose the appropriate command for your distribution:

For Debian/Ubuntu:

sudo apt-get install cryptsetup

For distributions using pacman:

sudo pacman -Sy cryptsetup

Encryption Process

1. Initialize LUKS Encryption

⚠️ WARNING: Before we proceed, please make sure you have a BACKUP OF THE DATA somewhere.

Initialize the LUKS encryption on your partition:

cryptsetup -y -v luksFormat /dev/sdb2

2. Open the Encrypted Partition

Create a mapping for the encrypted partition:

cryptsetup luksOpen /dev/sdb2 test

Running the following command will prompt for the passphrase you just created. After successful authentication, the encrypted partition will be available at /dev/mapper/test.

3. Verify the Setup

Check the status of your encrypted mapping:

cryptsetup -v status test

To view detailed LUKS header information:

cryptsetup luksDump /dev/sdb2

4. Secure the Partition

Fill the entire encrypted volume with zeros to ensure secure initialization:

pv -tpreb /dev/zero | dd of=/dev/mapper/test bs=128M

This step will:

5. Create a File System

Format the encrypted partition with ext4 or any file system:

mkfs.ext4 /dev/mapper/test

6. Mount and Verify

Create a mount point and mount the encrypted partition:

sudo mkdir /test_device
sudo mount /dev/mapper/test /test_device
df -H

Usage Tips


Note: Whenever you see a guide in my blog, they are primarily for my personal documentation, or I am sharing them in hope they might be useful.