Create Encrypted Volume inside File
Posted on Sun 21 November 2021 in Software • 2 min read •
Prerequisite: The system should have cryptsetup
installed. On debian this can be
achieved by sudo apt install cryptsetup
.
At first creat the file that will act as encrypted drive/volume. Please ensure that
the file should be >=100MB in size. The file will then be formatted with
cryptsetup
. This step will demand either a passphrase or key file (following
example commands are for passphrase case).
1 2 |
|
In case, if you want to use key file, you can generate a key-file with random bits
using head -c 4kB /dev/urandom > key_file_path;sync
or
dd if=/dev/urandom of=key_file_path bs=1000 count=4;sync
.
Next while doing luksFormat
with cryptsetup
command,
add --key-file key_file_path
in the command. For security, save the key file at a
safe location with proper permission (for example chmod 400 key_file_path
), because
losing or modifying key-file will mean loss of data in encrypted volume.
It should be noted additional passphrase or key-files can be added to
encrypted_volume.enc
with cryptsetup luksAddKey
command.
Now, open the volume inside file with cryptsetup and format it with ext4
partition.
1 2 |
|
You can go with other filesystem or even with lvm2
. The volume can be mounted in any
folder by using mount
command.
1 |
|
After accessing the data in the volume, the volume should be unmounted and the mapping should be closed
1 2 |
|
Additional Tips
- To get information about existing encrypted device, use the command
cryptsetup luksDump device_path
. - If you do not want to enter passphrase everytime you open an encrypted drive, add a
key-file to it using
cryptsetup luksAddKey
command, get the UUID for the encrypted device bysudo blkid
(orcryptsetup luksDump
orcryptsetup luksUUID
), and then put following line in/etc/crypttab
:
1 |
|
- For added safety, you can backup LUKS Header of encrypted drive. Backed up LUKS Header can come handy in cases when header in actual encrypted drive is corrupted or missing. For backing and restoring header, following commands are used.
1 2 |
|
- After taking backup of LUKS Header, all pass-phrases and key-files can be removed from
encrypted device by
cryptsetup luksErase
. However, this operation will render the device unusable, so it is highly recommended that a backup of header is taken before. Such a device can then be opened usingcryptsetup luksOpen --header=header_file_path
command. - For an encrypted device, the command
cryptsetup -v isLuks <device_path>
givecommand successful
message (hence, it reveals that device is encrypted with LUKS). If you want to avoid detection, take a backup of header and then runwipefs -a <device_path>
to remove all information about filesystem in encrypted device. After this step, the encrypted drive can only be opened usingcryptsetup luksOpen --header=header_file_path
command.