How can I make a password protected Virtual Hard drive in Linux?

I have an Ubuntu server 20.4 installed in Virtual Box. I wanted to make a Virtual Hard Drive. So I made an Image to Hold Virtual Drive Volume and formated the EXT4 file system type in the image file with these commands.

  • sudo dd if=/dev/zero of=VHD.img bs=1M count=1200
  • mkfs -t ext4 VHD.img

After, I mounted the image file in the/mnt directory with this.

  • mount -t auto -o loop VHD.img /mnt

This is the output of the df -HT command.

udev                              devtmpfs  469M     0  469M   0% /dev
tmpfs                             tmpfs     103M  1.1M  102M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4       22G  5.6G   15G  29% /
tmpfs                             tmpfs     515M  1.2M  514M   1% /dev/shm
tmpfs                             tmpfs     5.3M     0  5.3M   0% /run/lock
tmpfs                             tmpfs     515M     0  515M   0% /sys/fs/cgroup
/dev/loop0                        squashfs   74M   74M     0 100% /snap/lxd/21029
/dev/loop1                        squashfs   59M   59M     0 100% /snap/core18/2128
/dev/loop3                        squashfs   34M   34M     0 100% /snap/snapd/12883
/dev/loop2                        squashfs   34M   34M     0 100% /snap/snapd/12704
/dev/sda2                         ext4      1.1G  112M  842M  12% /boot
/dev/loop4                        ext4      482M  775k  445M   1% /mnt   # Image I mounted

Now I can access the /mnt directory and work with it. But I want this drive to be encrypted with a password. Meaning that when the user(even root) tries to mount it,a password must be specified in order to mount the image. My final goal is to have an image file encrypted with a password and with a size of over 400MB.


Solution 1:

A simple solution would be to use the free and open-source VeraCrypt.

It supports full-disk encryption, but also encrypted containers. A container looks like a file that requires a password for mounting, so can be used cross-platform (select for that a cross-platform file-system format).

You will find documentation on the VeraCrypt site, but also many articles on the internet.