Revision [103]

This is a past revision of FullDiskEncryptionTheDebianWay made by HennR on 2010-09-27 22:12:35

Full Disk Encryption - The Debian Way


This howto gets updated ATM, please give me some more time :)

Original version comes from:
http://sidux.com/index.php?module=Wikula&tag=FullDiskEncryptionTheDebianWay

Installing aptosid on an encrypted harddrive (full harddisk encryption - the Debian way)

Tested with: sidux 2009-04, 32bit + 64bit

Securing your data is not only relevant for laptop users, but should be the default for every installation (my opinion). Everbody has some sensible data on his harddisk, may it be passwords, confidential documents, pictures, emails, downloaded copyrighted stuff, whatever. If your computer gets stolen, or if you get busted by the police (why should you?), it would be all open to the offender. That's what we are trying to prevent here, by encrypting just about everything on your harddisk, so that nobody without the passphrase can get a clue, what's on it. This method even ensures, that you can savely hibernate your computer, because the swap-partition is an encrypted volume as well.

The Debian installer (which is also used by Ubuntu & Co) already has a nice option to perform an installation with full harddisk encryption, which means just a small unencrypted /boot partition, and everything else on an encrypted LUKS partition. Unfortunately aptosid is not using the Debian installer, and since it's a LiveCD?, some manual work is required to get to the same result.

This article is about installing aptosid onto an empty harddisk, so be sure, that there is no data on it, that you still need, because it will get DELETED! DO NOT FOLLOW THESE INSTRUCTIONS, IF YOU HAVE A MULTIBOOT ENVIRONMENT, OR ANYTHING ELSE ON YOUR HARDDISK, THAT MATTERS TO YOU!

Once the installation is finished, your drive will look like this:
Partition Mountpoint Comment
/dev/sda1 /boot Unencrypted boot partition
/dev/sda2 Encrypted LUKS partition holding the logical volumes

Basic Linux / aptosid experience is required to understand and follow this article! Feel free to adjust the proceeding to fit your needs (especially if you hardware setup should differ).

Preparations


Boot from the aptosid LiveCD?, and open a root-terminal.

At first we should ensure, that really nothing useful can be found on your harddisk, by overwriting the entire disk with random data. Depending on the capacity of your drive, this can take a very long time (on my laptop it took about 4 1/2 hours for a 60 GB harddisk)!

Warning: AT THIS POINT, ALL STORED DATA ON THE HARDDRIVE WILL BE LOST FOREVER!

root@aptosid:/home/aptosid# dd if=/dev/urandom of=/dev/sda bs=10MB


If you want to check the progress, open another root-terminal and enter:

root@aptosid:/home/aptosid# pkill -USR1 ^dd$


Once this is finished, we create our two partitions:

root@aptosid:/home/aptosid# fdisk /dev/sda

Command (m for help): n

p

Partition number (1-4): 1
First cylinder (1-7296, default 1): [Enter]
Last cylinder or +size or +sizeM or +sizeK (1-7296, default 7296): +256M

Command (m for help): a
Partition number (1-4): 1

Command (m for help): n

p

Partition number (1-4): 2
First cylinder (33-7296, default 33): [Enter]
Last cylinder or +size or +sizeM or +sizeK (33-7296, default 7296): [Enter]

Command (m for help): p

Disk /dev/sda: 60.0 GB, 60011642880 bytes
255 heads, 63 sectors/track, 7296 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x65da6411
Device Boot Start End Blocks Id System
/dev/sda1 * 1 32 257008+ 83 Linux
/dev/sda2 33 7296 58348080 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


Format the /boot partition and apply some optimizations (which I always use for !ext3, check the !tune2fs manpage for details):

root@aptosid:~# mkfs.ext3 -L boot /dev/sda1
root@aptosid:~# tune2fs -c0 -i0 -o journal_data /dev/sda1


Now we create the encrypted container on the second partition, and open it as sda2_crypt, which will create the device-node /dev/mapper/sda2_crypt:

root@aptosid:~# cryptsetup luksFormat -y --cipher aes-xts-plain --key-size 512 /dev/sda2
root@aptosid:~# cryptsetup luksOpen /dev/sda2 sda2_crypt


Remark:
Choose your passphrase wisely, because if you ever should forget it, you are doomed!
Choose a sufficient long password as well (16+) and use many different characters (special chars, capitals etc)!
A LUKS encrypted partition has 8 key slots, which means you can have more than one passphrase to unlock the encrypted container, and you can change your passphrase afterwards (by first adding another one, and then deleting the former one), which is recommended from time to time. Please have a look at the manpage (man cryptsetup) for more details.
Additionally: do not encrypt partitions over 1TB, since the aes-xts-plain security guarantee deteriorates as more data is encrypted with the same key.

Next comes the creation of the LVM physical volume, volume group and volumes onto the encrypted container:

root@aptosid:~# pvcreate /dev/mapper/sda2_crypt
root@aptosid:~# vgcreate cryptVG /dev/mapper/sda2_crypt
root@aptosid:~# lvcreate -n swap -L 3G cryptVG
root@aptosid:~# lvcreate -n root -l 100%FREE cryptVG


This creates a logical volume for swap with 3 GB (= /dev/!cryptVG/swap), and allocates the remaining free space to the logical volume for our / (= /dev/!cryptVG/root). Since I want to use hibernation, and my laptop has 2 GB RAM, I think, 3 GB for swap is a good number, and since it is all contained in one encrypted physical partition, The point for splitting up / into more separate logical volumes is increased security and preventing your system of growing out of space, the con is that you have choose wisely the space you allocate. But using a separate partition for /var is also used sometimes on production servers. A more practical case for a general case is having a separate partition to put the data that matters all in same place which allow for simple backup, if you want to be able to have a copy on you, consider using a virtual drive (a simple file used as a loop device.

The final step of our preparations is to create the filesystems on the logical volumes, create the necessary mountpoints, and mount our new / and /boot partitions:

root@aptosid:~# mkswap -L swap /dev/cryptVG/swap
root@aptosid:~# mkfs.ext3 -L root /dev/cryptVG/root
root@aptosid:~# tune2fs -c0 -i0 -o journal_data /dev/cryptVG/root

Installation


Now it is time to start the aptosid-installer.

In the second tab (Partitioning), have a look, if /dev/!sda1 shows up in the device-list. If not (like it was in my case), start gparted by clicking on "Execute", quit it without changing anything, and /dev/!sda1 should be shown now.

You only need to unselect "format with ext3", and select /boot as mountpoint for /dev/!sda1.

After these changes, the "Partitioning" tab has to look like this:

Partitioning tab of the aptosid-installer

Then configure the settings in the other tabs, and finally start the installation.


If the Installer doesnt recognize the crypt partitions, start gparted through the 'execute' button, reinstall the Installer or manually include the mountpoints in /root/.sidconfig

apt-get update && apt-get install --reinstall install-gui

Post-Installation

Now mount the disks.

root@aptosid:~# mkdir /media/aptosid
root@aptosid:~# mount /dev/cryptVG/root /media/aptosid
root@aptosid:~# mkdir /media/aptosid/boot
root@aptosid:~# mount /dev/sda1 /media/aptosid/boot



Once the installation has finished, we need to modify a few things to tell our new system about our encrypted setup.

Hint: You can find out the UUIDs of your devices for the above and following steps by taking a look into the folder /dev/disk/by-uuid as in: ls -al /dev/disk/by-uuid.

Edit the file /media/aptosid/etc/crypttab to make it look like this:

# <target name> <source device> <key file> <options>
# /dev/mapper/sda2_crypt = /dev/dm-0
sda2_crypt /dev/disk/by-uuid/[UUID of your /dev/sda2] none luks


Replace [UUID of your /dev/!sda2] with the actual UUID of your /dev/!sda2.



Edit the file /media/aptosid/etc/fstab to make it look like this:

# <filesystem> <mount point> <fstype> <mount options> <dump> <pass>

# /dev/cryptVG/root = /dev/mapper/cryptVG-root = /dev/dm-2
UUID=[UUID of your /dev/dm-2] / ext3 defaults,errors=remount-ro,noatime,user_xattr 0 1

# /dev/sda1
UUID=[UUID of your /dev/sda1] /boot ext3 noatime 0 2

# /dev/cryptVG/swap = /dev/mapper/cryptVG-swap = /dev/dm-1
UUID=[UUID of your /dev/dm-1] none swap sw 0 0

/dev/cdrom /media/cdrom auto noauto,rw,user,exec,utf8 0 0


Replace the three [UUID of your ...] with the actual UUIDs of your devices.

edit the file /media/aptosid/etc/initramfs-tools/modules to load:

xts
gf128
sha256

# for 32bit:
aes-i586

#for x86_64:
aes-x86_64


Remark: Somebody could test whether really all modules are needed.
EDIT: As it seems for 64bits only the two modules (aes-x86_64, xts) are really needed.

Create the file /media/aptosid/etc/initramfs-tools/conf.d/cryptroot and put the following in it:

target=sda2_crypt,source=UUID=[UUID of your /dev/sda2],lvm=cryptVG-root


Replace [UUID of your /dev/!sda2] with the actual UUID of your /dev/!sda2.

Remark: It is not possible, to boot this kind of installation without that file being added to the initrd-image in the next step, but strangely when doing such an installation with the Debian installer on Debian or *Ubuntu, this file is not present. I could not find out, how the Debian installer handles that matter differently. If anybody knows more about this, please leave a comment in the forum thread (see at the end of this page).

Now we have to chroot into our new installation and regenerate the initrd-image to incorporate the made changes:

root@aptosid:~# chroot /media/aptosid
root@aptosid:/# mount -t proc proc /proc
root@aptosid:/# mount -t sysfs sysfs /sys
root@aptosid:/# update-initramfs -u
root@aptosid:/# umount proc
root@aptosid:/# umount sys
root@aptosid:/# exit


Some cleanup before we reboot (is this necessary?):

root@aptosid:~# umount /media/aptosid/boot
root@aptosid:~# umount /media/aptosid


Done!

You are now ready to reboot into your new system.

Remark: The keyboard might be mapped as 'qwerty' (US standard). So, if you get an error like: "cryptsetup failed, bad password or option?" have a look at that picture∞ and find the corresponding keys for your password.

Additional Precautions


BIOS: Use a setup password, and configure the first boot device to be your boot-harddrive.
GRUB: Set a grub password to protect your grub menu.
CRYPTSETUP: Change your passphrase for the LUKS encrypted container from time to time (have a look at man cryptsetup).

Keep in mind: Anyone with physical access to your harddrive can make changes to your /boot partition (like sniffing your password the next time you boot).
Remark: One could install /boot on an USB stick ...

Troubleshooting


This installation method is to be considered absolutely save and solid.

If your encrypted machine nevertheless should not boot for any reason, simply boot from the aptosid LiveCD?, open a root-console, and mount your encrypted filesystem manually the following way, to be able to correct the problem:

TODO: Put these entries into a table for better copy/paste possibility

header1header2
cell1cell2

root@aptosid:~# mkdir /media/aptosid
root@aptosid:~# cryptsetup luksOpen /dev/sda2 sda2_crypt
root@aptosid:~# pvscan
root@aptosid:~# lvscan
root@aptosid:~# lvchange -ay /dev/cryptVG/root
root@aptosid:~# mount /dev/cryptVG/root /media/aptosid
root@aptosid:~# mount /dev/sda1 /media/aptosid/boot

TODO: how to update ramdisk and add mount --bind /dev/ to the chroot preparation

Now you have full access to your installation again, and you can chroot into /media/aptosid, if needed (especially necessary, if you need to make changes to the file /etc/initramfs-tools/conf.d/cryptroot and regenerate the initrd-image).

chroot /media/aptosid


Notes


Large parts of this article have shamelessly been stolen from Full disc encryption for your laptop with luks + using LVM2 (during installation) by capone, since I used his article as a template. His instructions kind of work as well, but have some flaws, which made me write this guide, since I consider this the one & only proper way, of setting up full disk encryption "the Debian way".

Questions or Comments


TODO: Create new forum topic and link to it

English Forum Thread: Installing aptosid on an encrypted harddrive (the Debian way)∞


Installation