On most modern Linux distributions, you have the chance during the install to tell the installer to
do a full disk encryption on your partition, which effectively encrypts your data at rest (when the
laptop is powered off) and can only be unlocked with your password. I will show you how to do this on
drives, and even files.
Install cryptsetup
There is most likely a cryptsetup
package for your distribution, here is how to install it on Debian/Ubuntu:
|
|
Setting up
We need a volume to work on. In this article I will use a flat file. But note that whatever I do to this file
will work on a hard drive or a thumb drive. In fact, I encourage you to do the steps I am doing on an actual
thumb drive, because this is likely how you will want to use this article for.
I will start by allocating a 1G volume that will simulate the behaviour of a thumb drive
|
|
Formatting and encrypting the volume
You have two ways of encrypting the volume, you can do it either with a passphrase, or with a keyfile. The
passphrase should be something long that you can remember, the key file can be anything, you can just generate
one like this (for a 4kb key):
|
|
We will start by encrypting the volume with a passphrase then add the key file as an other way of opening the volume.
|
|
Make a mental note of the password you used, you will need it later. Now check you can actually open the volume:
|
|
It works. Now let’s unmount the volume and add another key to it.
|
|
Let’s add the 4kb key we generated earlier
|
|
Try to open the volume using the key file:
|
|
Amazing ! You can now open the volume either using the passphrase or the key file. Now let’s slap a filesystem on it.
Format and mount
What you did with luksOpen
creates a block device in the /dev/mapper
directory. The crypt_volume
file is a block
device much like /dev/sda1
or what not, and as such it can be formated. So let’s format it using ext4.
|
|
Now let’s mount it.
|
|
Check the size
|
|
As expected it is about a gig. You can treat this filesystem like you would any other one. When you are done with the volume
you can unmount and close it:
|
|
Done ! You have successfully created an encrypted volume!
Bonus track: 2 factor unlock with a yubikey
You will need to install the following package to make it work:
|
|
Plug in the yubikey to your computer and pop up the following command:
|
|
You can now mount the volume by unlocking it with the yubikey challenge you just created
|
|
All done :)