Encrypt your Hard Drive in Linux

Do you need to encrypt a hard drive in Linux? This post will show you how to encrypt a separate partition, a whole hard drive, or just a USB stick. It will work in any Linux distribution.

There are many methods to perform encryption in Linux. In this post, I will show you the encryption method I use the most. It is probably the simplest method to encrypt your data in Linux. It is a command line tool, but there is nothing extraordinary.

Encrypt your hard drive in Linux with LUKS

I usually use LUKS encryption and dm-crypt.

Install the necessary programs

First, you need to install cryptsetup package:

1
sudo apt install cryptsetup

Identify the partition name

Next, make sure the partition you are going to encrypt doesn’t have any important data on it because it will be overwritten during the encryption process.

If your hard drive is brand new, you may need to create a new partition table. If you want to encrypt only part of your hard drive, you also need to re-partition your hard drive into two partitions: one will be encrypted and another one won’t. You can use Gparted for that.

Gparted
Gparted

You can also find out the name of your partition with the command lsblk and find the partition you need based on its size.

Running lsblk command to show all partition on your hard drive
Running lsblk command to show all partition on your hard drive

Encrypt this partition

When the partition you want to encrypt is ready and all the data is backed up into another hard drive, run this command:

1
sudo cryptsetup --verbose --verify-passphrase luksFormat /dev/sdb1

Where /dev/sdb1 is the partition you want to encrypt. Next, confirm that you agree to overwrite all the data in this partition.

Encrypting the partition
Encrypting the partition

Next, you will be prompted to type your passphrase. When you type it, you will see nothing. This is normal and due to security reasons. You will need this password to unencrypt the partition. Make sure you memorize it.

Next, open the encrypted partition. This is where you will need to type your passphrase.

1
sudo cryptsetup luksOpen /dev/sdb1 sdb1

Once the partition is opened, the encrypted partition is mapped to /dev/mapper/sdb1.

1
sudo fdisk -l
Mapped partition
Mapped partition

Then, create a new filesystem on it:

1
sudo mkfs.ext4 /dev/mapper/sdb1
Making a new filesystem to the partition
Making a new filesystem to the partition

I also suggest to get rid of reserved space. EXT4 file system reserves some space by default, but you won’t need it if you don’t run your system on this partition. This way you also get more space on the drive:

1
sudo tune2fs -m 0 /dev/mapper/sdb1
Getting more space on the partition
Getting more space on the partition

Now, your partition is encrypted and ready to use.

Test the encrypted partition

Mount it somewhere on your system. /mnt is the most common place to mount partitions.

Let’s create a folder encrypted in it.

1
sudo mkdir /mnt/encrypted

And perform the mounting:

1
sudo mount /dev/mapper/sdb1 /mnt/encrypted

Now, you can start placing your files into it. Let’s create a test file.

1
sudo touch /mnt/encrypted/test.txt

Here, I use sudo because you need root permissions to access the encrypted partition.

Then, you can change the access permission to this encrypted files with this command:

1
sudo chown -R `whoami`:users /mnt/encrypted

Next, you can create another file, but this time as a regular user without sudo.

1
touch /mnt/encrypted/test2.txt

You can also create and view encrypted files, from your file manager:

Using the file manager to show the encrypted partition
Using the file manager to show the encrypted partition

When you finish working with the encrypted partition, unmount it.

1
sudo umount /dev/mapper/sdb1

Let me remind you that my encrypted partition name is sdb1, in your case it may be different.

Then, close the mapped device.

1
sudo cryptsetup luksClose sdb1

Finally, it is safe to disconnect the hard drive from your system.

How to use the encrypted hard

Next time, when you want to use your encrypted drive. You have to connect your hard drive to the system and check its name.

1
lsblk
Checking the partitions
Checking the partitions

It is sdb1 in my case.

Next, open the encrypted partition with your decryption password:

1
sudo cryptsetup luksOpen /dev/sdb1 sdb1

Then, mount it.

1
sudo mount /dev/mapper/sdb1 /mnt

Now, it is available at /mnt/encrypted. As you can see, the test files we created earlier are there.

Mounting the encrypted partition
Mounting the encrypted partition

When you finished working with this encrypted files, unmount the partition:

1
sudo umount /dev/mapper/sdb1

And close it.

1
sudo cryptsetup luksClose sdb1

In some Linux systems, such Linux Mint in my case, you can also mount the encrypted partition by double-clicking on it in the file manager and entering your passphrase. So, you can avoid the command line hassle.

Some Linux distribution can open a encrypted partition easily
Some Linux distribution can open an encrypted partition easily

Conclusion

This way you can encrypt any hard drive, including flash drives. If you encrypt a hard drive that is permanently connected to your system, you can also make it mount automatically on the boot of your system.

For the next read, I recommend you my post about Linux Root Folders.

So, do you use an encrypted partition? What method do you use to encrypt it? Let me know below.

Average Linux User
Average Linux User I am the founder of the Average Linux User project, which is a hobby I work on at night. During the day I am a scientist who uses computers to analyze genetic data.