Encrypting Disk on Linux

2 minutes read

Make sure the encryption modules are loaded

sudo modprobe dm-crypt
sudo modprobe dm-mod

Prepare the disk

Setup LUKS on the disk

sudo cryptsetup luksFormat -v -s 512 -h sha512 /dev/nvme1n1

Open the encrypted partition

# Syntax
sudo cryptsetup open /dev/nvme1n1 --type luks <cryptdiskname>

# Example
sudo cryptsetup open /dev/nvme1n1 --type luks cryptdisk

Partition the disk

In this example we’re gonna use btrfs.

# Syntax
mkfs.fstype -L mylabel /dev/mapper/name

# Example
mkfs.btrfs -L Disk /dev/mapper/cryptdisk

Manual mounting and unmounting

sudo cryptsetup open --type luks cryptdisk

# Syntax
mount -t fstype /dev/mapper/cryptdisk /mnt/disk

# Example
mount -t btrfs /dev/mapper/cryptdisk /mnt/disk
umount /mnt/disk

cryptsetup close cryptdisk