Categories: Linux

How to encrypt block devices using LUKS on Linux

Sometimes you may want to encrypt your hard disk so that when someone connects your hard drive to their computer they need to provide user credentials to mount the drive. In Linux, it is possible to encrypt individual block devices. In this article, we will learn how to encrypt block devices in Linux using LUKS. LUKS is the Linux encryption layer that can be used to encrypt the entire root partition, a logical volume, or a specific partition.

This tutorial covers the following Linux Distributions

  • Debian
  • Ubuntu
  • RHEL
  • CentOS
  • Rocky Linux
  • Almalinux

Install cryptsetup-luks package

Cryptsetup utility tool comes with the cryptsetup-luks package which is used for setting up block device encryption in Linux systems. Installation can be done by using the following command.

Ubuntu/Debian

$ apt-get install cryptsetup

RHEL/CentOS/Rocky Linux/Almalinux

$ dnf install cryptsetup-luks

Prepare a LUKS partition

Once the utility tool is installed, prepare a partition for encryption. To list all the available partitions and block devices, run the following command.

$ fdisk -l
$ blkid

Now use cryptsetup luksFormat command to set up encryption in the partition. In this example, the partition, sdb, is used for encryption. You can make your own assumption based on your environment.

$ cryptsetup -y -v luksFormat /dev/sdb

The command executed above will remove all the data on the partition

Now we need to create a logical device-mapper device mounted to the LUKS-encrypted partition in the above step. In this example, encrypted is the name provided for the mapping name of the opened LUKS partition.

The following command will create a volume and set passphrase or initial keys. Remember that the passphrase can not be recovered.

$ cryptsetup luksOpen /dev/sdb encrypted

The mapping details of the partition can be found by using the following command.

$ ls -l /dev/mapper/encrypted

Use the following command to view the status of mapping. Replace your mapping name with encrypted.

$ cryptsetup -v status encrypted

cryptsetup along with luksDump command can be used to check that the device has been formatted successfully for encryption. In this example, sdb partition is being used for the confirmation.

$ cryptsetup luksDump /dev/sdb

Format LUKS partition

Writing zeros to the LUKS-encrypted partition will allocate the block size with zeros. Use the following command to set zeros to the encrypted block device.

$ dd if=/dev/zero of=/dev/mapper/encrypted

dd command may take some time to be executed. Use the pv command to check the progress.

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

Note: Replace encrypted with your device mapping name.

Now format the new partition with your desired file system. In this example, the ext4 file system is used.

$ mkfs.ext4 /dev/mapper/encrypted

Replace encrypted with your device-mapper name.

Mount the new file system. In this example, the new file system is mounted at /encrypted

$ mkdir /encrypted
$ mount /dev/mapper/encrypted /encrypted

Replace the device-mapper name encrypted with your own mapper name.

$ df -h
$ cd /encrypted
$ ls -l

So we successfully created an encrypted partition on Linux using LUKS.

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

Recent Posts

How to Install Magento 2 on AlmaLinux

Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…

1 year ago

How to Install ISPConfig Hosting Control Panel with Apache Web Server on Ubuntu 24.04

ISPConfig is an open-source control panel that allows users to manage multiple servers from a…

1 year ago

How to Test your Email Server (SMTP) Using the Telnet Command

As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…

1 year ago

Managing Network Interfaces and Settings on Ubuntu 24.04 with nmcli

Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…

2 years ago

Using Restic Backup on Ubuntu 24.04

Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…

2 years ago

Installing phpMyAdmin on Rocky Linux 9 and Securing it with Let’s Encrypt SSL

phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…

2 years ago