Revision [213]

This is a past revision of FullDiskEncryptionTheDebianWay made by skullfire on 2011-01-13 06:21:34

Full Disk Encryption - The Debian Way


Tested with: sidux 2009-04, 32bit + 64bit, aptosid 2010-02 64bit kde light

multiple tests with Aptosid 2010-02 "Κῆρες", based on Debian sid, kernel 2.6.35 and KDE 4.4.5 full 64bit install were successful !

successful attempt with aptosid 2010-03 "Ἀπάτη" kernel 2.6.36.2 kde full 64bit released december 27, 2010. i changed a few of the settings below during my install to use ext4 on the boot and / partitions. i also adjusted my partition size to only encrypt my aptosid LVM partition for / and swap, as i want to chainload other systems on my other partitions. this successful attempt i let the bootloader write to the mbr (default choice) making the grub of aptosid the 1st choice during system start up.

Please update this note if you made it with a newer release!

Motivation


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).

Options and Other Considerations


If you want to chainload other operating systems this method can also be used to encrypt only the aptosid installation. But care is needed to make sure you change certain commands below to install and apply changes to the correct partitions. If aptosid is NOT your first installed system on your hard drive or you want to save information on any other partitions then you want to skip the "Wiping disk and filling with random data" section, and move on to "setting partitions". To check your current partition scheme open a terminal and enter the following:

 su

 fdisk -l

If you are chainloading you will also need to decide which system to write to the mbr of the bootloader and make any needed changes to the bootloader using the installer during installation.

Wiping disk and filling with random data


Warning: THESE METHODS WILL WIPE ALL DATA ON A HARDRIVE ! ALL DATA WILL LOST! IF YOU DO NOT WANT TO DO THAT, THEN SKIP TO THE NEXT STEP! IF YOU ARE USING MULTIPLE HARD DRIVES YOU MAY WANT TO PHYSICALLY UNPLUG THE HARD DRIVES YOU DONT WANT OVERWRITTEN TO PREVENT ANY TRAGETIES DURING WIPING, THEN PLUG THEM BACK IN AFTER WIPING TO GET YOUR BOOTLOADER RIGHT.

Choose one of the methods below:

Method 1


Boot from the aptosid Live CD, 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!

dd if=/dev/urandom of=/dev/sda bs=10MB


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

pkill -USR1 ^dd$


Method 2 Using Shred


Using a single pass of shred should be fine for common users. You can choose as many passes as you like, but passes 1,13, and 25 write random data. A single pass will take about 1-2 hours depending on your hardware. To use a single pass of shred open a terminal, switch to root, and enter the following command:

shred -n 1 -v /dev/sda


Setting Partitions


Once this is finished, we create our two partitions:

fdisk /dev/sda

Note: if you didn't overwrite the disk, you still have a partition table there. Erase it using the fdisk command "o".

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

# Set the bootable flag on partition 1:

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.

If the kernel doesn't recognize the new partition table, reboot from the Live CD to continue.

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

mkfs.ext3 -L boot /dev/sda1
tune2fs -c0 -i0 -o journal_data /dev/sda1


Create Encrypted Container & LVM


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:

cryptsetup luksFormat -y --cipher aes-xts-plain --key-size 512 /dev/sda2
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.

FIXME: Is this still true?
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:

pvcreate /dev/mapper/sda2_crypt
vgcreate cryptVG /dev/mapper/sda2_crypt
lvcreate -n swap -L 3G cryptVG
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:

mkswap -L swap /dev/cryptVG/swap
mkfs.ext3 -L root /dev/cryptVG/root
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.

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

To reinstall, if needed:
apt-get update && apt-get install --reinstall install-gui


Post-Installation

Now mount the disks.

mkdir /media/aptosid
mount /dev/cryptVG/root /media/aptosid
mkdir /media/aptosid/boot
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

Now to edit the file /media/aptosid/etc/crypttab :

nano /media/aptosid/etc/crypttab

Make the file 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.

press "ctrl x" to save, then y , then enter.


Now to edit the file /media/aptosid/etc/fstab (note that aptosid installer might have already set it up correctly):

nano /media/aptosid/etc/fstab

Adjust the files for your new devices so 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


FIXME: I think these kind of entries are no longer needed, right?
/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.

press "ctrl x" to save,then y,then enter

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

nano /media/aptosid/etc/initramfs-tools/modules

Add the following to the file:

xts
gf128
sha256

# for 32bit:
aes-i586

#for x86_64:
aes-x86_64

press "ctrl x" to save,then y,then enter

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 :

pico /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.

press "ctrl x" to save,then y,then enter

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:

chroot /media/aptosid
mount -t proc proc /proc
mount -t sysfs sysfs /sys
update-initramfs -u
umount proc
umount sys
exit


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 this keyboard 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 considered absolutely safe 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:

mkdir /media/aptosid
cryptsetup luksOpen /dev/sda2 sda2_crypt

FIXME: What are these 3 lines for? Answer: you might not see the partitions in /dev/cryptVG if you don't scan.

pvscan
lvscan
lvchange -ay /dev/cryptVG/root

mount /dev/cryptVG/root /media/aptosid
mount /dev/sda1 /media/aptosid/boot
mount --bind /dev/ /media/aptosid/dev
mount --bind /proc/ /media/aptosid/proc


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


How to regenerate initrd


If you need to generate new initrd to boot your encrypted system chroot into it as described above.
After that you can do so like this:

update-initramfs -u -k all


-u stands for update
-k specifies the version to update. Using -k all updates all initrds available.


Notes


Original version comes from:
http://sidux.com/index.php?module=Wikula&tag=FullDiskEncryptionTheDebianWay
Which superseded this article before:
http://sidux.com/index.php?module=Wikula&tag=FullDiscEncryptionLaptopLVM

Questions or Comments


If you are having problems with this howto, ask your questions over here in the forum:

Forum thread for "Installing aptosid on an encrypted harddrive (the Debian way)"


CategoryInstall