Encrypting an existing Debian lenny installation
Posted by mikhailian on Wed 8 Jul 2009 at 16:33
Once in a time, I get to travel to places that make me worry about the data on my laptop. This time, it is not the US, but another openly democratic country where they kill you for a joint, let alone nude pictures. Enough politics, though.
I have a laptop with the /boot in a separate partition, followed by a /root partition and a /swap. Having a separate /boot is mandatory, as the BIOS has to load an unencrypted kernel and its initrd image before being able to access the encrypted partition. Another option is to keep /boot on a USB stick, but this setup can take a whole other post.
First things first, let us install software for managing encrypted disks and updating the initrd image:
#aptitude install cryptsetup initramfs-tools
We have to make sure that the encryption modules are present in the initrd image, so I add the following three modules to the initrd config:
#echo aes-i586 >>/etc/initramfs-tools/modules #echo dm-crypt >>/etc/initramfs-tools/modules #echo dm-mod >>/etc/initramfs-tools/modules
This step is probably unnecessary as initramfs-update is able to figure out the modules needed by parsing /etc/crypttab and /etc/fstab and by checking the loaded modules.
Next step is to inform cryptsetup and inittab of the partition mapping between /dev/hda2 (the physical device) and /dev/mapper/root (its encryption interface).
#echo "root /dev/hda2 none luks" >>/etc/crypttab #sed -i 's#/dev/hda2#/dev/mapper/root#' /etc/fstab
We also have to change the root device for grub the same way we did it for inittab:
#sed -i 's#/dev/hda2#/dev/mapper/root#' /boot/grub/menu.lst
Now, recreate the initrd image by issuing
#update-initramfs -k all -u
We are now ready to shutdown and to boot from a LiveCD in order to make a backup, create an encrypted partition and copy back the root filesystem contents on an already encrypted partition. I leave the reader at the exercise of choosing available backup options. A simple "cp -ax /mnt/root/* /mnt/backup" command will be enough to backup, though.
Once the backup is ready, erase the data on the partition by issuing
#shred -n1 -v /dev/hda2
and then create the encrypted partition with
#cryptsetup luksFormat /dev/hda2 #cryptsetup luksOpen /dev/hda2 root
After the encrypted device is set up and open, create a filesystem, mount it and copy the backup of the root partition to the encrypted device.
#mkfs.ext3 /dev/mapper/root #mount /dev/mapper/root /mnt/root #cp -ax /mnt/backup/* /mnt/root/
You are now ready to boot into the encrypted root partition.
Once the root encryption works, addding swap encryption is a piece of cake. Just add it to /etc/crypttab and modify the /etc/fstab accordingly:
#echo "swap /dev/hda3 /dev/random swap" >>/etc/crypttab #sed -i 's#/dev/hda3#/dev/mapper/swap#' /etc/fstab
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
On a laptop, having suspend/resume is very important.
What would you have to change to make it work?
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
Worked out of the box for me.
[ Parent | Reply to this comment ]
http://sunoano.name/ws/public_xhtml/debian_security.html#filesyst em-level_encryption
good thing is, it also features key management and all the metadata is within the files so those become portable
[ Parent | Reply to this comment ]
greate article
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
for example we have disk /dev/hda1
cryptsetup create my-disk /dev/hda1
# encrypt disk:
dd if=/dev/hda1 of=/dev/mapper/my-disk
#decrypt disk:
dd if=/dev/mapper/my-disk of=/dev/hda1
Also You can use which way for change encryption type.
Recently i used to loop-aes encryption module, now i am using cryptdisk. I got across fromto without nessesary backups :)
[ Parent | Reply to this comment ]
Doesn't mean you shouldn't back up, but even if you do back up, this still makes the process simpler.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
I have only /swap /root and /home separate.
Thanks and Greetings ;)
[ Parent | Reply to this comment ]
-copy the content of the initial /boot into it,
-rename the initial /boot into something like /_boot and create an empty /boot directory,
-add a line about this new /boot volume into /etc/fstab & mount it,
-launch update-grub (make sure that the parameter "# groot=(hdX,Y)" is set properly in /boot/grub/menu.lst),
You're done! (I hope I didn't forget something important!)
[ Parent | Reply to this comment ]
Could you do a post on how to install a new debian system using this approach?
[ Parent | Reply to this comment ]
http://glandium.org/blog/?p=139
http://glandium.org/blog/?p=141
[ Parent | Reply to this comment ]
My solution is hiding in plain sight. My main laptop OS is a minimum Lenny distro which is able to setup a (working) VPN connection with the office. This explains why there aren't any recent files in my home or other directories.
It also contains a Xen image which boots my 'normal' lenny distro.
BTW: Just make sure that bash on the boot image don't keep a history file.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]