Archive for the ‘ GNU/Linux ’ Category

How to create a portable encrypted file system on a loop file

Here I’m going to explain how to create an encrypted file system over a loop file. I also have a encrypted filesystem on a LVM partition but having them on a file has advantages like the capacity of copy the encrypted file in another PC and mount the file system there ( a portable encrypted file system ) or when you are, for example, in a server and you can’t create a new partition.

I do this with LUKS (Linux Unified Key Setup).

This “how to” is for Debian or Ubuntu but if you have another GNU/Linux distribution, it shouldn’t be too different, just install the packages like you always do.

First of all, use apt to install these packages:

apt-get install lvm2 cryptsetup e2fsprogs

Now let’s create, for example, a 500MB file:

dd if=/dev/zero of=/home/you/cryptfile bs=1M count=500

Asociate it with a loop device:

losetup /dev/loop0 /home/you/cryptfile

(if you have /dev/loop0 in use, just use another, like /dev/loop1, /dev/loop2, …)

Fill the file with random data:

badblocks -s -w -t random -v /dev/loop0

Using badblocks is better than create the file from /dev/urandom.
If you haven’t loaded the kernel module for the encryption you want, load them:

modprobe blowfish

When I write this, the default encryption algorithm was AES (if you prefer this use “modprobe aes”).

Create the encrypted file system asociated with the loop device:

cryptsetup -y luksFormat -c blowfish -s 256 /dev/loop0
cryptsetup luksOpen /dev/loop0 crypt_fun
mkfs.ext3 -j /dev/mapper/crypt_fun
e2fsck -f /dev/mapper/crypt_fun

In this case I create a ext3 file system, you can choose any other.

Also you can use another encryption algorithm with another options.

Try “man mkfs.ext3” and “man cryptsetup” to see different parameters and options.

Create a folder to mount the encrypted file system:

mkdir /media/fun

I made a couple of scripts to mount and unmount the file system:

mountCrypt.sh:

………………………………………

#! /bin/sh

(losetup /dev/loop0 /home/you/cryptfile || echo) && (cryptsetup luksOpen /dev/loop0 crypt_fun && mount /dev/mapper/crypt_fun /media/fun)
………………………………………

umountCrypt.sh:

………………………………………

#! /bin/sh

umount /media/fun && cryptsetup luksClose crypt_fun && losetup -d /dev/loop0
………………………………………
And that’s all, you have your portable encrypted file system ready!

How to create a LVM encrypted partition

Be carefully with all this commands, with some of them you can erase all the data in a partition, always use ‘man’… of course, I’m using GNU/Linux.

I do this in Debian, works perfect for me, I’m working, mounting and unmounting the partition for more than a year without any problems.

Well, let’s do it…

First, create the LVM partition(in this case named lv_fun):

lvcreate -n lv_fun –size 1G VolGr01

Then, fill the partition with random data:

badblocks -s -w -t random -v /dev/mapper/VolGr01-lv_fun

Now let’s create the encrypted partition with dm-crypt and luks:

cryptsetup -y luksFormat /dev/mapper/VolGr01-lv_fun
cryptsetup luksOpen /dev/mapper/VolGr01-lv_fun crypt_fun

You must write the passphrase after this commands, use a good passphrase, a reasonable hint is using leters, numbers, some other sign and it should have 20 or more characters (just a quick hint, theres a lot to talk about this).

Use the ‘man’, you can modify a lot of parameters in the previous commands.

OK, the encrypted partition is done! Let’s make the filesystem in this:

mkfs.ext3 -j /dev/mapper/crypt_fun
e2fsck -f /dev/mapper/crypt_fun

In this case I make a ext3 FS, you can do anything else.

And it’s done!

Now you can have some privacy… just some… ;)

We only need to know how to mount and unmount it:

Mount:

cryptsetup luksOpen /dev/mapper/VolGr01-lv_fun crypt_fun && mount /dev/mapper/crypt_fun /media/fun

Umount:

umount /media/fun && cryptsetup luksClose crypt_fun

And that’s it, you have your privacy with a LVM encrypted partition.

If you can’t create a partition or you want a portable encrypted file system you can read my other post about privacy and encryption on linux:

How to create a portable encrypted file system on a loop file