Create an encrypted container in linux

In case you have some files which you want to have encrypted in a secured container, like a password-ed zip file. When mapping this container file you have to enter a password or you can use a key file to unlock the file! I will use a manual password entry to unlock the container.

Installing cryptsetup

Use yum, apt to install cryptsetup or download the source to build your own. For now I will use apt as an example.

# apt-get install cryptsetup

Creating the container file

Create a 1GB private file

# fallocate -l 1GB PRIVATE

Set the file as luks

# cryptsetup -v luksFormat PRIVATE

Decrypt the file and create /dev/mapper/private_file

# cryptsetup -v luksOpen PRIVATE private_file

Format private file as ext4

# mkfs -t ext4 /dev/mapper/private_file

Mounting and unmounting the container file

Make sure the file is decrypted:

# cryptsetup -v luksOpen PRIVATE private_file

Mount the private_file

# mkdir /mnt/private_file
# mount /dev/mapper/private_file /mnt/private_file

Umount and close file

# umount /mnt/private_file
# cryptsetup luksClose private_file
Was this post helpful?
Let me know if you liked the post.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.