This instruction is about container encryption which simply means a single mountable file like an image file that we have with docker container.
This guide is for Debian based distribution, for others may not be the same steps.
Install / Setup cryptsetup
Simply we can install it
sudoaptinstallcryptsetup-bin
and the command will be installed, then use it cryptsetup
cryptsetup--help
Create the volume you need
Then using dd or fallocate we can create a single file with desired size e.g. 512M
and with fallocate
or it can be other places e.g. / which we should be root to do so
LUKS the volume
With this single 512M file we have, we will next create a LUKS partition
then cryptsetup asks you for confirmation and you should type in YES in uppercase and then it asks you for the passphrase with which later this volume / container is going be for decrypted.
You can have a benchmark before encryption and may be change the encryption method. Here is a sample
Open the volume
After adding LUKS on top the volume, we can open it using
The <NAME> can be your desired name, just notice with are dealing with special devices and /dev so we need root permission. After opening it we will see it in /dev/mapper
here derak is the name (<NAME>) I entered for the volume, you will have a different name.
Mount the volume
Now that we have the device (it is like a partition) we can mount it like other partitions
Check the mounted volume
Here is quick check
Use the volume
After you checked it with df -h then we can cd to /media and create file or cp other files into /media
Umount the volume
When we are done with adding / removing file into our volume, we can umount it
Close the LUKS / volume
Optionally we can check our mapped device befor closing it. Here for me ithe name is derak
Then close it
And check the device again, which we should not have it
# for me
sudo umount /dev/mapper/derak
# for you with your <NAME>
sudo umount /dev/mapper/<NAME>
# then the /media or your mount-point should be empty
ll /media
total 8
drwxr-xr-x 2 root root 4096 Jul 25 2018 ./
drwxr-xr-x 27 root root 4096 Mar 21 09:52 ../
ll /dev/mapper/
total 0
drwxr-xr-x 2 root root 80 Mar 23 15:10 ./
drwxr-xr-x 20 root root 4660 Mar 23 15:10 ../
crw------- 1 root root 10, 236 Mar 23 10:18 control
lrwxrwxrwx 1 root root 7 Mar 23 15:11 derak -> ../dm-0
# for me
sudo cryptsetup luksClose derak
# for you with the name you created
sudo cryptsetup luksClose <NAME>
ll /dev/mapper/
total 0
drwxr-xr-x 2 root root 60 Mar 23 16:31 ./
drwxr-xr-x 20 root root 4640 Mar 23 16:31 ../
crw------- 1 root root 10, 236 Mar 23 10:18 control