4

On Mac OS X I had the option of creating an encrypted sparse image. I had this automount at boot and used it to store files that I wanted encrypted (tax documents, bank statements, or other personally identifiable information). It was useful because I don't want to encrypt my entire home directory, or my entire hard drive; the number of files I actually keep encrypted is pretty small so it could still be conveniently backed up to the cloud as a single file, preserving my private documents without making them accessible if someone were to gain access to that data somehow.

I'm curious if there is an analog to this on Ubuntu (or Linux in general) or if there is at least a means to achieve the same ends.

  • I know I can create an encrypted hard drive partition, but I don't want to do this because I want to minimize the space used while still leaving room for growth, and I want to be able to sync the file to the cloud (with a small image this is not a huge issue).

  • I know I can encrypt my home directory but this is not what I want to do either. I don't want to encrypt things like music, movies, configuration files, or most of the work I do since I don't care about keeping them private and I want to be able to sync a lot of it without having to update a huge blob every time I sync. I suppose theoretically I can store all my personal files in another directory and symbolically link to them from my encrypted home, but I'm still not sure if this accomplishes what I want (can I backup the blob somewhere and restore on another (not necessarily Ubuntu or Linux) machine?) or if it would cause too much of a hassle.

  • I've found some information about creating and mounting sparse images, but nothing I found mentioned enabling encryption.

Any ideas?

3

It is feasible, you must first create a sparse file, suppose 512MB:

truncate -s 512M file.img

So with cryptsetup, let's encrypt the file as a device (the syntax is the same, here only use the basic options)

cryptsetup -v luksFormat file.img

WARNING!
========
This will overwrite data on file.img irrevocably.

Are you sure? (Type uppercase yes): YES
Enter passphrase:
Verify passphrase:
Command successful.

We open, therefore, the encrypted volume:

 cryptsetup luksOpen file.img sparse_file
Enter passphrase for file.img:

and we create the fs:

mkfs.ext4 /dev/mapper/ sparse_file
mke2fs 1:42:12 (29-Aug-2014)
Creating filesystems with 1k blocks 522240 and 130560 inodes
Filesystem UUID: ec4e3529-0ee5-4b31-9487-5ae1c598c9d3
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

mount the encrypted file:

 mount /dev/mapper/sparse_file /mnt/sparse_file/

du -h --apparent-size file.img
512M file.img
du -h file.img
17M file.img
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/sparse_file 486m 2.3M 455M 1% /mnt/sparse_file

et voilà

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.