How to Encrypt Hard Disk (partition) using LUKS in Linux | GoLinuxCloud (2024)

Table of Contents

Advertisem*nt

Earlier I had shared an article to encrypt, decrypt and sign a file using GPG key in Linux. In this article I will show you the steps to create an encrypted block device using LUKS. By default if somebody connects your hard disk to their computer, it can be mounted automatically, even without entering any user credentials, and that is why we should always encrypt hard disk.

If your hard disk was encrypted then in order to mount an encrypted device, you need to enter a passphrase, without passphrase, nobody can mount it. So this will protect your hard disk, or your server, hard disk from being lost or stolen or whatever, after which data can be accessed easily.

To create encrypted devices in Linux we use LUKS. LUKS is the Linux encryption layer.

Below are some more articles on LUKS based Disk Encryption

  • How to Encrypt Hard Disk (partition) using LUKS in Linux
  • How to auto mount LUKS encrypted partition using fstab at boot in Linux
  • How to encrypt root partition and entire file system using LUKS in Linux
  • How to resize LUKS partition (shrink or extend encrypted luks volume) in Linux
ALSO READ: How to properly check if file exists in Bash or Shell (with examples)

dm-crypt and cryptsetup vs LUKS

dm-crypt and cryptsetup

  • Device-mapper is a part of the Linux kernel that provides a generic way to create virtual layers of block devices, most commonly LVM logical volumes. The device-mapper crypt target (dm-crypt) provides transparent encryption of block devices using the kernel crypto API.
  • In Red Hat Enterprise Linux, userspace interaction with dm-crypt is managed by a tool calledcryptsetup, which uses the device-mapper infrastructure to setup and operate on encrypted block devices.

LUKS

  • With modern versions of cryptsetup (i.e., since ~2006), encrypted block devices can be created in two main formats, plain dm-crypt format or the extended LUKS (Linux Unified Key Setup-on-disk-format) format.
  • LUKS provides a standard on-disk-format for hard disk encryption, which facilitates compatibility among Linux distributions and provides secure management of multiple user passwords.
  • In contrast to previous Linux disk-encryption solutions, LUKS stores all necessary setup information in the partition header, enabling the user to more easily transport or migrate their data.
  • The advantages of LUKS over plain dm-crypt are the obvious higher usability: automatic configuration of non-default crypto parameters, the ability to add, change, and remove multiple passphrases.
  • Additionally, LUKS offers defenses against low-entropy passphrases like salting and iterated PBKDF2 passphrase hashing

Attach new hard disk (optional)

So to start with, you need an empty device. I have added a new virtual disk to my virtual machine as /dev/sdb

[root@node1 ~]# cat /proc/partitionsmajor minor #blocks name 11 0 1048575 sr0 8 0 31457280 sda 8 1 524288 sda1 8 2 28844032 sda2 8 16 5242880 sdb 253 0 26738688 dm-0 253 1 2097152 dm-1

ALSO READ:

How to backup and restore entire partition and file system using fsarchiver in Linux

ALSO READ: Configure Thin Provision LVM using kickstart with example in CentOS/RHEL 7/8

Create new partition

We will create a new partition /dev/sdb1 on this disk

[root@node1 ~]# fdisk /dev/sdbWelcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.Be careful before using the write command.Device does not contain a recognized partition tableBuilding a new DOS disklabel with disk identifier 0xa12bdd47.Command (m for help): nPartition type: p primary (0 primary, 0 extended, 4 free) e extendedSelect (default p):Using default response pPartition number (1-4, default 1):First sector (2048-10485759, default 2048):Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):Using default value 10485759Partition 1 of type Linux and of size 5 GiB is setCommand (m for help): pDisk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0xa12bdd47 Device Boot Start End Blocks Id System/dev/sdb1 2048 10485759 5241856 83 LinuxCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.

So our partition is successfully created.

Advertisem*nt

[root@node1 ~]# partprobe

So our partition is successfully created.

[root@node1 ~]# cat /proc/partitionsmajor minor #blocks name 11 0 1048575 sr0 8 0 31457280 sda 8 1 524288 sda1 8 2 28844032 sda2 8 16 5242880 sdb 8 17 5241856 sdb1 253 0 26738688 dm-0 253 1 2097152 dm-1

Format the partition using luksFormat

So you would do luksFormat on the device, and the luksFormat command is going to create the encryption layer. This is the passphrase that needs to be entered by anyone who wants to access the device. In real life of course, you wanna have something that really is secure because devices are not mounted that often. (computer keys tapping and clicking)

[root@node1 ~]# cryptsetup luksFormat /dev/sdb1WARNING!========This will overwrite data on /dev/sdb1 irrevocably.Are you sure? (Type uppercase yes): YESEnter passphrase for /dev/sdb1:Verify passphrase:

Initialise LUKS device

Next, you need to do luksOpen, and that brings you to a different level where you are going to work with the encrypted device. So this will create a new device, and this new device is managed by the device mapper, so let's call it /dev/mapper/secret.

[root@node1 ~]# cryptsetup luksOpen /dev/sdb1 secretEnter passphrase for /dev/sdb1:

As we will see when you are using the cryptsetup, luksOpen command, a new device is created, and you will provide the name for the device. In this example, the name for the device is /dev/mapper/secret

[root@node1 ~]# cd /dev/mapper/
[root@node1 mapper]# ls -ltotal 0lrwxrwxrwx. 1 root root 7 Feb 25 21:11 centos-root -> ../dm-0lrwxrwxrwx. 1 root root 7 Feb 25 21:11 centos-swap -> ../dm-1crw-------. 1 root root 10, 236 Feb 25 21:09 controllrwxrwxrwx. 1 root root 7 Feb 25 21:14 secret -> ../dm-2
ALSO READ: How to get script execution time from within the shell script in Linux

Create file system on LUKS device

Now the important step is that you need to create a file system on the encrypted device, and that means that the file system is going to be created here.

IMPORTANT NOTE:

Here make sure you create a file system on the encrypted device and not on the physical partition. The file system must be created on the LUKS device.

[root@node1 mapper]# mkfs.ext4 /dev/mapper/secretmke2fs 1.42.9 (28-Dec-2013)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks327680 inodes, 1309952 blocks65497 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=134217728040 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736Allocating group tables: doneWriting inode tables: doneCreating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: done

Mount the LUKS partition

Once a file system has been created on the LUKS device, you can move on, you can create a mount point and mount it.

[root@node1 mapper]# mkdir /secret[root@node1 mapper]# cd[root@node1 ~]# mount /dev/mapper/secret /secret/

Once we can verify using the mount command, you can see that from the mount command perspective, there's nothing visible about the device being encrypted, we just see a device that is encrypted.

[root@node1 ~]# mount | grep secret/dev/mapper/secret on /secret type ext4 (rw,relatime,seclabel,data=ordered)

So we can create files on top of it. (computer keys tapping and clicking) And these files will be safely stored on the encrypted device.

Advertisem*nt

ALSO READ: How to auto mount LUKS device (encrypted partition) using fstab in Linux

Dis-connect the encrypted partition

let me also show you how to disconnect the encrypted device.

[root@node1 ~]# umount /secret
[root@node1 ~]# cryptsetup luksClose /dev/mapper/secret[root@node1 ~]# mount | grep secret

Lastly I hope the steps from the article to encrypt hard disk (partition) using LUKS on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

In the next article I will share the steps to automatically decrypt and mount the encrypted partition at booting stage using key file on Linux

How to Encrypt Hard Disk (partition) using LUKS in Linux | GoLinuxCloud (2024)
Top Articles
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 6570

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.