CentOS 7 – How to setup your encrypted filesystem in less than 15 minutes
Nowadays setting up an encrypted file system is something that can be achieved in a matter of minutes, there’s a small drop in FS performance but it’s barely noticeable and the benefits are countless.
All the major distributions allow you to conveniently setup the encrypted volume during the installation and that is very convenient your for you laptop/desktop, nevertheless on the server-side these options are often neglected.
With this how to you’ll be able to set up your encrypted LVM volume in your CentOS 7 in 8 easy steps and less than 15 minutes.
I’m assuming that you’re running LVM already, and that you have some free space available on your volume group (in this case 249G):
The steps:
lvcreate -L249G -n EncryptedStorage storage
skip the shred command if you just have 15 minutes, look at the explanation bellow to see if you’re willing to do so.
shred -v –iterations=1 /dev/storage/EncryptedStorage
cryptsetup –verify-passphrase –cipher aes-cbc-essiv:sha256 –key-size 256 luksFormat /dev/storage/EncryptedStorage
cryptsetup luksOpen /dev/storage/EncryptedStorage enc_encrypted_storage
mkfs.ext4 /dev/mapper/enc_encrypted_storage
Edit /etc/cryptotab and add the following entry:
enc_encrypted_storage /dev/storage/EncryptedStorage none noauto
Edit /etc/fstab and add the following entry:
/dev/mapper/enc_encrypted_storage /encrypted_storage ext4 noauto,defaults 1 2
Finally mount your encrypted volume
mount /encrypted_storage
After reboot you’ll need to run these two commands to have your encrypted filesystem available on your CentOS 7 system:
cryptsetup luksOpen /dev/storage/EncryptedStorage enc_encrypted_storage
mount /encrypted_storage
Now the steps explained.
Step 1:
lvcreate -L249G -n EncryptedStorage storage
I’ve created a volume with 249GB named EncryptedStorage on my volume group storage (each distribution has a naming convention for the volume group name, so you better check yours, just type:
vgdisplay
The output:
— Volume group —
VG Name storage
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 499.97 GiB
PE Size 32.00 MiB
Total PE 15999
Alloc PE / Size 15968 / 499.00 GiB
Free PE / Size 31 / 992.00 MiB
VG UUID tpiJO0-OR9M-fdbx-vTil-2dty-c7PF-xxxxxx— Volume group —
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 23.51 GiB
PE Size 4.00 MiB
Total PE 6018
Alloc PE / Size 6018 / 23.51 GiB
Free PE / Size 0 / 0
VG UUID sncB8Z-0Upw-VrwH-DOPJ-hELz-377f-yyyyy
As you can see I have 2 volume groups, one installed by default on all VMs and it’s called centos, and another one installed by me called storage, in the how to I’m using the storage volume group.
Step 2:
shred -v –iterations=1 /dev/storage/EncryptedStorage
This command proceeds at the sequential write speed of your device and may take some time to complete. It is an important step to make sure no unencrypted data is left on a used device, and to obfuscate the parts of the device that contain encrypted data as opposed to just random data.
You may omit this step although not recommended.
Step 3:
cryptsetup –verify-passphrase –cipher aes-cbc-essiv:sha256 –key-size 256 luksFormat /dev/storage/EncryptedStorage
On this step we format the volume with our selected block cypher, in this case I’m using AES encryption with CBC mode, essiv IV and 256 bits key.
A block cipher is a deterministic algorithm that operates on data blocks and allows encryption and decryption of bulk data. The block cipher mode describes a way the block cipher is repeatedly applied on bulk data to encrypt or decrypt the data securely. An initial vector is a block of data used for ciphertext randomization. IV ensures that repeated encryption of the same plain text provides different ciphertext output. IV must not be reused with the same encryption key. For ciphers in CBC mode, IV must be unpredictable, otherwise the system could become vulnerable to certain watermark attacks (and this is the reason for the sha256).
Step 4:
cryptsetup luksOpen /dev/storage/EncryptedStorage enc_encrypted_storage
Here we assign and open the encrypted volume to a device that will mapped using device mapper, after this step you will be able to do regular block device operations like on any other lvm volume.
Step 5:
mkfs.ext4 /dev/mapper/enc_encrypted_storage
Format the volume with the default ext4 settings, you may use whatever flags you wish though.
Step 6:
Edit /etc/crypttab and the following line:
enc_encrypted_storage /dev/storage/EncryptedStorage none noauto
With this line we will permanently enable /dev/storage/EncryptedStorage volume assignment to the enc_encrypted_storage mapped device.
The noauto setting is important to the server boot correctly if the blockdevice password is not entered during the boot process, this will enable you to use your custom script or manually insert the password in a later stage using ssh.
Step 7:
Edit /etc/fstab and add the following entry:
/dev/mapper/enc_encrypted_storage /encrypted_storage ext4 noauto,defaults 1 2
This is where we map the previously mapped device to a mount point, in this case /encrypted_storage, the noauto value is set due to the same reasons as in step 5.
Step 8
mount /encrypted_storage
Simple mount command, you’ll be able to store and access your files in /encrypted_storage, it will be a good place for the files you want to keep private on your CentOS system.
You may find more information about supported cyphers and options on Redhat documentation:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/
Cheers,
Pedro Oliveira