Encrypt disk in linux with LUKS and auto unlock while boot

May 29, 2025 min read

Encrypt disk

 cryptsetup luksFormat /dev/sdX

It will ask you for password.

Get UUID of the disk

blkid /dev/sdX

Add key to the LUKS vault

dd   if=/dev/urandom  of=/root/crypttab.key  bs=1024   count=4
chmod  400  /root/crypttab.key
cryptsetup   -v luksAddkey   UUID=###   /root/crypttab.key

Open the vault

cryptsetup open /dev/sdX vaultdrive

Create filesystem type ext4

mkfs.ext4 /dev/mapper/vaultdrive 

Auto unlock by crypttab while booting

nano /etc/crypttab

put the following end of the file vaultdrive UUID=### /root/crypttab.key luks

Mount by fstab

mkdir -p /mnt/vaultdrive
nano /etc/fstab

put the following at the end of the file /dev/mapper/vaultdrive /mnt/vaultdrive ext4

Now you have your secondary disk encrypted and will be unlocked automatically without password.