DM-Crypt HOWTO

Printer-friendly versionPrinter-friendly version

Dm-crypt

This tutorial is about how to set up a crypted partition on linux using dmcrypt.
Of course, you can use either fisical partitions or files using a loopback device.

Loopback or partition?

A loopback means that you have a file that is on a partition that you then mount using a special device called a loopback. The loop device then acts as a normal block device transforming your file into just another hard disk :)

This is useful if for example you wish to store all your ssh keys safely but don't want to have to make another partition for it!

Configuring the kernel

You will need a kernel configuration similar to this in order of use dmcrypt:

You must first enable the device mapper (dm):

1
2
3
4
Device Drivers -->;
 [*] Multiple devices driver support (RAID and LVM)
  <*>; Device mapper support
  <*>; Crypt target support

Then you must enable the cipher (aes):

1
2
 Cryptographic API -->;
  <*>; AES cipher algorithims (i586)

If you're going to be using dmcrypt on a loopback file, not a partition:

1
2
 Device Drivers -->; Block Devices -->;
 <*> Loopback device support # Remember, cryptoloop is not dmcrypt

Installing needed tools

You need cryptsetup in order to create ciphered partition. The exact package name may differ on various distros:

  • Gentoo: sys-fs/cryptsetup
  • Debian: cryptsetup

Creating ciphered file

..on a partition

If you wish to use dmcrypt on a partition then read this, otherwise see below for information on using it with a loopback device.

First we create a device mapper device called 'mycrypt' on a partition, say /dev/hda7 (we will use that throughout the guide)

1
cryptsetup -y create mycrypt /dev/hda7

Has it worked?

1
dmsetup ls

It should display 'mycrypt'

Now create a filesystem (replace mke2fs with whatever your filesystem creation tool is):

1
2
3
4
5
6
7
mke2fs /dev/mapper/mycrypt
<code>
 
<p>Now mount it:</p>
 
<code>
mount /dev/mapper/mycrypt /mnt/point

Test it worked, congratulations!

To bring it down:

1
2
3
4
5
6
7
8
9
10
11
12
13
umount /mnt/point
cryptsetup remove mycrypt
<code>
 
<h4>..on a file using loopback device</h4>
 
<p>This is for using dmcrypt with a loopback device; see above for using it with a partition.</p>
 
<p>First, create our file:</p>
 
<code>
touch protected
shred -n1 -s50M protected

This creates a file called 'protected' in your current directory of 50MB.
By prefilling it with random data, it's impossible to see afterwards how much has been used.

Now let's set a loopback device to use this file.
First find the name of the first unused loop device :

losetup -f

Use this loop device to set a loopback (in this case /dev/loop0 is available)

1
losetup /dev/loop0 /path/to/protected

Now lets create an encrypted device mapper device using cryptsetup:

1
cryptsetup -y create mycrypt /dev/loop0

Check it worked:

1
dmsetup ls

You should see 'mycrypt' listed

Now create a filesystem (replace mke2fs with whatever your filesystem creation tool is) :

1
mke2fs /dev/mapper/mycrypt

Now mount it:

1
mount /dev/mapper/mycrypt /mount/point

Check it works for a while, and be happy, then continue reading :)

To unmount it:

1
2
3
umount /mount/point
cryptsetup remove mycrypt
losetup -d /dev/loop0

To automate this process you could write your own script (see below) or try the tool cryptmount.
umount/mount Scripts

1
2
3
4
5
6
#!/bin/bash
losetup /dev/loop0 /Your/Container
sleep 1
cryptsetup create Container /dev/loop0
sleep 1
mount /dev/mapper/Container /mnt/Container

replace Container with your file, save it under /usr/bin/something and chmod +x /usr/bin/something

1
2
3
4
#!/bin/bash
umount /dev/mapper/Container
cryptsetup remove Container
losetup -d /dev/loop0

Replace Container with your file, save it under /usr/bin/somethingother and chmod +x /usr/bin/somethingother

5 comments

 
Anonymous wrote 5 weeks 2 days ago

video jedi

fake ring amen ego rimonabant lola chapel timid

 
Anonymous wrote 10 weeks 15 hours ago

kept

clot liars manic sold host viagra köp gnome quip rush adolf pedro köp levitra ludwig sheer

 
Anonymous wrote 11 weeks 6 days ago

gush nne

dee psych wynn buy valtrex akin bees salon clam

 
Anonymous wrote 19 weeks 3 days ago

gauche hype

buick hut acomplia irvin broke posh kiln coca comprar soma online skim chunk xvii

 
Anonymous wrote 23 weeks 2 days ago

prey

W e _ a r e _ v e r y _ s o r r y _ f o r _ a n y _ i n c o n v i n i e n c e _ c a u s e d _ t o _ y o u _ b y _ o u r _ m e s s a g e . W e _ w e r e _ s e e k i n g _ f o r _ o l d _ " n o b o d y ' s " _ w e b s i t e s . _ I f _ i t ' s _ a _ m i s t a k e _ a n d _ w e _ d i s t u r b e d _ y o u , _ p l e a s e _ d e l e t e _ o u r _ m e s s a g e , _ a n d _ w e ' l l _ n e v e r _ r e t u r n _ h e r e . O n e _ m o r e _ t i m e , _ s o r r y _ f o r _ a n y _ t r o u b l e s _ t h a t _ w e ' v e _ c a u s e d _ b y _ o u r _ a c t i v i t y . levitra online

Who Am I?

~redShadow~ A.K.A. Samuele Santi is an Italian Open Source developer, currently working as a freelance developer, mainly in the web applications sector. Favourite programming languages: PHP and, of course, Python!

aircrack (1) aoe (1) code (3) blogroll (7) debug (1) como lake rovers (1) awstats (3) 3d (3) contact manager (1) development (11) Drupal (21) cars (1) blender (3) e-mail (2) database (3) C++ (2) archive (1) dmcrypt (1) alcool (1) algorythms (1) apache (1) audio (1) cryptography (1) debian (1) citroen (1) arduino (1) 2v (1) camera mia (1) bash (11) documentation (2) circuits (1) backup (3) apt (1) curl (1) caos (1) cocktails (1) cartoons (1) address book (2) Drupal Forms (1) doku (1)