Create encrypted disk image file
Posted: Wed Jul 29, 2015 1:55 am
Here's an instruction how to create an image file that behaves like an encrypted partition.
This is useful to conveniently store data securely, handling like a virtual encrypted USB stick.
Create a disk image:
In order to create a 2 GB image file, execute the following command:
Find a free loop device:
The following command should return the name of a loop device that's free for you to use:
In most cases that will be /dev/loop0, since you probably don't have any loops in use.
But if you do, then it will list the next "free" loop device
Setup partition encryption (LUKS):
$DEV is a variable for the loop device, assigned to this image. In our example it's "/dev/loop0".
$IMAGE is the image file, created previously using "dd".
This will map the image file "4gb_sd.bin" to "/dev/loop0" and initialize the encryption layer. This is where you enter your passphrase.
The "cryptsetup luksFormat" dialog will look somewhat like this:
You will now be prompted for the passphrase you've entered before:
If everything works correctly, it will return nothing.
Partition / format it:
Close the encrypted partition:
Links:
This is useful to conveniently store data securely, handling like a virtual encrypted USB stick.
Create a disk image:
In order to create a 2 GB image file, execute the following command:
Code: Select all
$ dd if=/dev/zero of=4gb_sd.bin bs=1G count=4
The following command should return the name of a loop device that's free for you to use:
Code: Select all
$ sudo losetup -f
But if you do, then it will list the next "free" loop device
Setup partition encryption (LUKS):
$DEV is a variable for the loop device, assigned to this image. In our example it's "/dev/loop0".
$IMAGE is the image file, created previously using "dd".
Code: Select all
$ sudo losetup $DEV $IMAGE
$ sudo cryptsetup luksFormat $DEV
The "cryptsetup luksFormat" dialog will look somewhat like this:
Now, open the encrypted partition:WARNING!
========
This will overwrite data on /dev/loop3 irrevocably.
Are you sure? (Type uppercase yes): YES
Enter LUKS passphrase:
Verify passphrase:
Code: Select all
$ sudo cryptsetup luksOpen $DEV $NAME
Enter it.Enter passphrase for /dev/loop3:
If everything works correctly, it will return nothing.
Partition / format it:
Code: Select all
$ sudo mkfs.ext4 -F -L "$PARTITON_LABEL" "$/dev/mapper/$NAME"
Code: Select all
$ sudo cryptsetup luksClose /dev/mapper/$NAME