:::: MENU ::::

Encrypt Hard Drive Disk under Linux – LUKS and cryptsetup

  • Jul 25 / 2014
  • 0

Encrypt Hard Drive Disk under Linux – LUKS and cryptsetup

You want to add a new hard drive disk (or a new partition) to your Linux system but this disk will contain some private data then you want it to be encrypted so you can restrict its access to whom will have the key.

Be careful, there are some parameters you have to take in consideration before performing these actions:

  • The passphrases used for encryption will never been saved, you have to be really careful about this and do not lose it. If you do, it will be impossible to retrieve data on this disk.
  • The encryption will impact your system performances (due to the CPU usage for encrypt/decrypt actions). Be sure that you use this disk for passive data (avoid any executables files for example), and prefer newest CPU with latest instructions set for AES so the performances can be improved (AES-NI).

I am going to present here the encryption of a new disk (pretty small, a 1GB disk for the example) identified as /dev/sdb on the system:

First of all, we need to install tools, like cryptsetup that we will use to encrypt/decrypt our disk (it’s a system tool allowing dm-crypt/LUKS encryption on hard drive, partition or even file):

We can now indicate that we want to crypt our /dev/sdb disk using AES and a hash alorithm SHA-256:

You will get a confirmation message to which you will have to type “YES” to validate. Then, you will be asked for the passphrase you want to use (this password won’t be saved, so don’t forget it!) to encrypt your data:

If you want to add some other keys for this encryption, you can do it using this following command:

Thanks to this command, you can add up to 8 different keys for the disk, allowing up to 8 different users to access to these data (by using each one its own passphrase):

You can also check whenever you want the state of the slots (used or not) so you can manage the keys. For that, you can just use this command:

Now that the disk is encrypted, we need to create our partition and our file system so we can use it. We will decrypt our disk in a first time and create the mapping with a custom name encrypteddata:

Right now, we can find this new mapping under /dev/mapper:

We can now work with this new mapping point. A mapping for an encrypted disk can be checked at any time by using the parameter status of cryptsetup command:

Then, we will create our partition and our file system with ext3:

We are now able to mount our new decrypted disk on any local point to read/write its content:

Our disk is now available on /mnt:

To close an encrypted disk, you will need to unmount it and then use the luksClose method:

The disk is now closed and encrypted again until someone will open it.

You can also choose to mount thi sdisk automatically on system start-up. For that, you will use /etc/crypttab file to define the encrypted volume configuration and then the /etc/fstab to define its mount (as you used to do for a standard disk). Using this mechanism, the key to decrypt the disk will be asked on system start-up:

Warning: if there is already a line existing in /etc/fstab for this disk, you will need to comment it so you don’t get any error on start-up.

Right now, you know how to encrypt your own disk 😉 !

Comments are closed.

Question ? Contact