Welcome!
Wednesday 08 September 2010 @ 15:39 CEST

Encrypted swap, tmp and home partition in Ubuntu 9.04

Security

I really would like to have an encrypted swap, tmp and home partition on my laptop. In case it gets stolen or if I should forget it somewhere, I can be sure that no-one would be able to read my private files. In this mini-howto I set my home partition using LVM, but using a regular partition should work just fine. This howto should also work, with minor modification, if you use another distribution than Ubuntu.

Updated:
May 2009: Updated for Ubuntu 9.04. Added encrypted /tmp.
May 2008: Init for Ubuntu 8.04.

Note! Both the "server" and "alternate" Ubuntu ISO-images provide the option to encrypt your home directory (but in a different way using eCryptfs. Swap and /tmp are not encrypted). It might be an easier solution if you find this page too hard to follow. The difference? They are two different implementations. eCryptfs is file level encryption, LUKS is block device (/dev/sda3). Think of it like SSL vs. IPSec. Both have their advantages and drawbacks. Read more here and here

By using Linux Unified Key Setup (LUKS) setting up encrypted partition in Linux is done in no time.

Prerequisites

Install required packages:

# apt-get install lvm2 cryptsetup libpam-mount

The device-mapper should be active (if not, reboot):

$ ls -l /dev/mapper/
total 0
crw-rw---- 1 root root 10, 61 2009-05-19 15:39 control

..with support for crypto:

# dmsetup targets | grep crypt
crypt v1.6.0

Good. Now we're ready.

Part I: Setting up encrypted swap

Step 1: Disable your current swap partition.

 # swapoff /dev/sda2

Step 2: Fill your swap with random data.

# dd if=/dev/urandom of=/dev/sda2 bs=1M
1954+0 records in
1953+0 records out
2048094208 bytes (2.0 GB) copied, 529.177 s, 3.9 MB/s

As you see, this might take some time depending on your swap size. So go grab a coffe.

Step 3: Configure encrypted swap.

Add this to your /etc/crypttab

# cat /etc/crypttab
...
cryptoswap /dev/sda2 /dev/urandom cipher=aes-cbc-essiv:sha256,size=256,hash=sha256,swap

Why /dev/urandom and not /dev/random? The latter blocks until it got enough entropy to continue, urandom don't. So if you use random instead urandom you might have to wait during boot until enough entropy is collected. (It does help to type your keyboard and move the mouse.) Use /dev/random if you're really paranoid.

Next, change your swap entry in /etc/fstab to this:

# cat /etc/fstab
...
/dev/mapper/cryptoswap swap swap sw 0 0

For every time we boot, swap will be encrypted with a different encryption key.

Step 4: Test it.

Reboot to test.

We now have an encrypted swap:

# cat /proc/swaps
Filename Type Size Used Priority
/dev/mapper/cryptoswap partition 2000084 0 -1

# cryptsetup status cryptoswap
/dev/mapper/cryptoswap is active:
cipher: aes-cbc-essiv:sha256
keysize: 256 bits
device: /dev/sda2
offset: 0 sectors
size: 4000185 sectors
mode: read/write

Good. Now we're safe right?

Part II: Dealing with /tmp

To protect /tmp, we have two choices. 1) we can encrypt it like we did with swap or 2) we can create a ramdisk. The content of a ramdisk don't survive a reboot and /tmp rarely is used for any big files, its is also a good option. But, paranoid as we are, we choose option 1)

The setup is almost identical as for swap:

Step 1: Setting up a tmp partition using LVM.

If you use a regular partition, you can easily skip this step.

# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
# vgcreate vg_storage /dev/sda3
Volume group "vg_storage" successfully created
# vgchange -a y vg_storage
0 logical volume(s) in volume group "vg_storage" now active
# lvcreate -L500M -nlv_tmp vg_storage
Logical volume "lv_tmp" created

For more details on how to use LVM, please check out the excellent LVM HOWTO.

Step 2: Fill the partition with random data.

# dd if=/dev/urandom of=/dev/vg_storage/lv_tmp
1024001+0 records in
1024000+0 records out
524288000 bytes (524 MB) copied, 139.983 s, 3.7 MB/s

Step 3: Add entry in /etc/crypttab

# cat /etc/crypttab
...
cryptotmp /dev/vg_storage/lv_tmp /dev/random cipher=aes-cbc-essiv:sha256,size=256,hash=sha256,tmp

Now, since /tmp is encrypted with a new key every time, the filsystem must be created every time as well. The option "tmp" fixes that for us and calls mkfs before mount. Since it is created with filesystem ext2, we add in fstab:

# cat /etc/fstab
...
/dev/mapper/cryptotmp /tmp ext2 defaults 0 0

Step 4: Test it.

Reboot to test.

We now have an encrypted /tmp partition as well. Great!

# cryptsetup status cryptotmp
/dev/mapper/cryptotmp is active:
cipher: aes-cbc-essiv:sha256
keysize: 256 bits
device: /dev/mapper/vg_storage-lv_tmp
offset: 0 sectors
size: 1024000 sectors
mode: read/write

Part III: Creating and setting up an encrypted home partition

Step 1: Setting up a home partition using LVM.

If you use a regular partition, you can easily skip this step.

# lvcreate -L20G -nlv_home vg_storage
Logical volume "lv_home" created

Step 2: Fill your soon-to-be home partition with random data.

 # dd if=/dev/urandom of=/dev/vg_storage/lv_home
20481+0 records in
20480+0 records out
21474836480 bytes (21 GB) copied, 5554.23 s, 3.9 MB/s

This will take even longer than the swap partition. So go for lunch or something.

Step 3: Initialize the partition and set initial key.

Remember, if you use a weak password, your screwed. If you forget the password, its game over.

# cryptsetup -c aes-cbc-essiv:sha256 -y -s 256 luksFormat /dev/vg_storage/lv_home

WARNING!
========
This will overwrite data on /dev/vg_storage/lv_home irrevocably.

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

We use cipher "aes-cbc-essi", since the default is vulnerable to Watermarking attack.

Step 4: Create a device mapping.

# cryptsetup luksOpen /dev/vg_storage/lv_home cryptohome
Enter LUKS passphrase:
key slot 0 unlocked.
Command successful.

This will create a device mapping, as can bee see under:

$ ls -l /dev/mapper/
total 0
crw-rw---- 1 root root 10, 61 2009-05-19 15:39 control
brw-rw---- 1 root disk 252, 4 2009-05-19 15:52 cryptohome
brw-rw---- 1 root disk 252, 1 2009-05-19 15:39 cryptoswap
brw-rw---- 1 root disk 252, 2 2009-05-19 15:39 cryptotmp
brw-rw---- 1 root disk 252, 3 2009-05-19 15:52 vg_storage-lv_home
brw-rw---- 1 root disk 252, 0 2009-05-19 15:39 vg_storage-lv_tmp

Note that LVM also uses the device-mapper (that is why LVM volumes also are listed).

Or, you can use the command dmsetup ls to list the mapped devices:

$ dmsetup ls
cryptoswap (252, 1)
vg_storage-lv_tmp (252, 0)
cryptotmp (252, 2)
vg_storage-lv_home (252, 3)
cryptohome (252, 4)

Step 5: Create a filesystem.

We now have an encrypted partition. To use it, we need to create a filesystem on it:

# mkfs.ext4 -j -m 1 -O dir_index,filetype,sparse_super /dev/mapper/cryptohome
mke2fs 1.41.4 (27-Jan-2009)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
1310720 inodes, 5242623 blocks
52426 blocks (1.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Step 6: Testing!

We start by closing and reopen the encrypted partition before we mount it:

# cryptsetup luksClose cryptohome
# cryptsetup luksOpen /dev/vg_storage/lv_home cryptohome
Enter LUKS passphrase:
key slot 0 unlocked.
Command successful.
# mkdir -p /mnt/cryptohome
# mount /dev/mapper/cryptohome /mnt/cryptohome
# touch /mnt/cryptohome/testfile
# ls /mnt/cryptohome/
lost+found testfile

We can also confirm that it works by issuing the command:

# cryptsetup status cryptohome
/dev/mapper/cryptohome is active:
cipher: aes-cbc-essiv:sha256
keysize: 256 bits
device: /dev/mapper/vg_storage-lv_home
offset: 2056 sectors
size: 41940984 sectors
mode: read/write

Now would be a good time to move your current home to this partition.

Finally we umount:

 # umount /mnt/cryptohome
# cryptsetup luksClose cryptohome

Step 7: Cryptohome mounted at boot or at login?

Now you have to take a choice. You can enable the partition at boot time, but then the boot sequence is interrupted asking you for the LUKS password. If you want the partition automatically mounted when you login, skip to the next section.

Instead of manually typing in password, you can have the key stored externally - for instance on a usb-stick. Read more about that here.

You want to enable mounting at boot time? Then update /etc/crypttab:

# cat /etc/crypttab
...
cryptohome /dev/vg_storage/lv_home none luks

And /etc/fstab:

# cat /etc/fstab
...
/dev/mapper/cryptohome /home/ ext4 relatime,errors=remount-ro 0 2

When you now reboot, the boot process is interrupted asking you for the LUKS password. If you type it correctly, the home partition is mounted. When you now log in, you will have an encrypted home partition ready waiting for you.

Part IV: Automatically mount when logging in.

A more elegant solution would be to automatically mount the home partition the same time you log in. This require that you use the same password for login as for the encrypted partition. (Actually that is not entirely true. You may have the password stored on file somewhere. But in this howto, we assume you have the same password for both.)

Step 1: Remove home partition from /etc/fstab

If there is an entry to your (encrypted) home partition in /etc/fstab, remove it

# cat /etc/fstab
...
/dev/mapper/cryptohome /home ext4 relatime,errors=remount-ro 0 2 # this gotta go

Step 2: Update /etc/crypttab

Make sure the you have a line in /etc/crypttab that reads as follows:

# cat /etc/crypttab
...
cryptohome /dev/vg_storage/lv_home noauto luks

Step 3: Configure pam_mount

Add the following entry in /etc/security/pam_mount.conf.xml. This file is heavily commented, and it may be useful to read the comments.

# cat /etc/security/pam_mount.conf.xml
...
<volume user="lars" fstype="crypt" path="/dev/vg_storage/lv_home" mountpoint="/home" />

Step 4: Configure PAM

No longer necessary. As of 9.04 all options already included.

Step 5: Test!

Log out and back in. You should now have an encrypted home:

$ df -h
...
/dev/mapper/_dev_mapper_vg_storage-lv_home
20G 296M 20G 2% /home

Congratulation, you now have an encrypted swap, tmp and home partition!

A final advice: Take regular backups.

Useful links:

Trackback

Trackback URL for this entry: http://blog.larsstrand.org/trackback.php?id=EncryptedSwapAndHomeUbuntu

Here's what others have to say about 'Encrypted swap, tmp and home partition in Ubuntu 9.04':

How To: Encriptar particiones swap y Home en Ubuntu 8.04 (Eng) from meneame.net
Un sencillo tutorial para encriptar las particiones swap y home en Ubuntu, manteniendo tus datos seguros de miradas ajenas. Está en inglés pero no requiere mucho conocimiento para entenderlo. [read more]
Tracked on Tuesday 02 September 2008 @ 12:31 CEST

[Howto] Encriptar las particiones Home y Swap en Ubuntu 8.04 | el tecnicida
Tracked on Tuesday 02 September 2008 @ 13:20 CEST

Coredump &raquo; Setting up encrypted swap on Ubuntu 7.10 (Gutsy Gibbon)
Tracked on Saturday 20 September 2008 @ 17:17 CEST

carrierdetect.com &raquo; Migrating a MacBook Pro to GNU/Linux
Tracked on Wednesday 17 December 2008 @ 00:25 CET

Verschlüsselung, Fehler und MySQL ohne InnoDB &laquo; the jump to open source
Tracked on Tuesday 01 September 2009 @ 10:36 CEST

Duality &#8211; GOD has created you &raquo; Amankan seluruh data Ubuntu
Tracked on Friday 13 November 2009 @ 11:01 CET

Encrypted home directory in Ubuntu 9.10 | hoanglai.no
[...] Texmaker and Quick Build Encrypted home directory in Ubuntu 9.10 So you are one of those guys who has followed this blog to encrypt your system? You are not alone, at least were… After upgrading to Ubuntu 9.10, I have changed to use [...] [read more]
Tracked on Monday 08 March 2010 @ 17:38 CET

Como asegurar tus datos en un portátil | NooWhy???
[...] yo como usuario de Linux propongo utilizar LUKS que está soportada por el kernel. Yo me basé en una publicación de Lars Strand en la cual explica el proceso para Ubuntu 9.04, yo lo voy a actualizar para Ubuntu 9.10 (es prácticamente idéntico) [...] [read more]
Tracked on Saturday 22 May 2010 @ 19:15 CEST

Encrypted swap, tmp and home partition in Ubuntu 9.04 | 29 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Encrypted swap and home partition in Ubuntu 8.04
Authored by: Anonymous on Wednesday 18 June 2008 @ 21:47 CEST
Thanks for posting this HowTo.
I'm using LUKS encrypted home+swap with unencrypted root for some months now, or years? works like a charm :-)

some comments (especially for part III):
with pam-mount you do not need the /etc/crypttab entry. I do only have the cryptswap-entry.

ubuntus pam-mount installs a common-pammount file in /etc/pam.d/ you can use:
I added "@include common-pammount" to my common-auth and common-session instead of loading pam_mount.so directly.
this way you'll benefit from changes the package maintainer grants to you ;-) but nevertheless if dpkg updates common-auth/session ... you know...

and at least:
why do you use lvm to create only one volume in it? ;-)
  • Re: - Authored by: home loans on Tuesday 24 August 2010 @ 18:36 CEST
Encrypted swap and home partition in Ubuntu 8.04
Authored by: Anonymous on Thursday 19 June 2008 @ 04:05 CEST

I successfully used your post to encrypt the /home and swap partitions on my laptop running LinuxMint. Mint, unfortunately, does not have an alternative install CD so if one wants LVM and/or encryption one has to do the setup manually.

A minor correction in the line for the swap in /etc/fstab, the mount point should be "none" instead of "swap":

/dev/mapper/cryptoswap none swap sw 0 0

Thank you for the post,
Peter

Encrypted swap and home partition in Ubuntu 8.04
Authored by: Anonymous on Friday 20 June 2008 @ 08:45 CEST
while encrypting a previously unencrypted swap, there remains a "resume"-entry in /etc/initramfs-tools/conf.d/resume. You can delete or comment out (#) the "RESUME=" line. Otherwise the splash screen (usplash) will show the console while boot i.e. the splash won't show the full booting time.
don't forget to rewrite the initrd: e.g. with "dpkg-reconfigure [yourkernelimage, e.g. linux-image-2.6.24-19-generic]" or use aptitude/synaptic or even update-initramfs directly.
Encrypted swap and home partition in Ubuntu 8.04
Authored by: Anonymous on Saturday 05 July 2008 @ 06:28 CEST
for those who might not have known: the 'alternate' install disc gives you the option to set up a fully encrypted system, so if you're doing a fresh install and don't mind having system files encrypted as well, using the install option is probably easier.
Encrypted swap and home partition in Ubuntu 8.04
Authored by: Anonymous on Wednesday 10 September 2008 @ 10:07 CEST
I found that following the directions posted here for modifying PAM, I was asked for a second password from the GDM.

I added only "@include common-pammount" in both /etc/pam.d/common-auth and /etc/pam.d/common-session and the login unencryption worked correctly with a single password.
Encrypted swap and home (or /tmp?) partition in Ubuntu 8.04
Authored by: Anonymous on Monday 15 September 2008 @ 19:04 CEST
Hello, Lars!

You have written a very useful article.

If one wanted to encrypt /tmp instead of /home, what should one do differently? The big difference is that /tmp should be encrypted with a throw-away key because I prefer NOT to be asked for a password at the boot time for opening up /tmp. It is because of this that I haven't been able to encrypt /tmp. :-/ Maybe, or hopefully, you could help?

Much oblige!

Tuomas
Turkish Translation
Authored by: Anonymous on Saturday 27 September 2008 @ 00:14 CEST
Hi Lars,

Here is your document's Turkish translation: http://docs.comu.edu.tr/howto/encrypted-swap-howto.html

Best regards

Necdet Yucel
Encrypted swap and home partition in Ubuntu 8.04
Authored by: Anonymous on Thursday 04 December 2008 @ 18:23 CET
Thank you, Lars. I worked for three days trying various approaches (dmsetup, truecrypt) to get an encrypted /home on boot up and it wasn't until I tried your steps that I met with success. I did have one difference on my ubuntu 8.10 setup from what you show (the encrypted swap didn't 'engage' after the initial boot in Step 4.; that is, running cryptsetup status cryptoswap showed nothing at first; it wasn't until I continued on with the rest of the steps that the swap was automagically enabled after one of the reboots, but I couldn't tell you exactly when). Otherwise, it appears to be workng well! Many, many thanks. It's folks like you that help make the Linux world work (god knows I couldn't figure it out on my own and I work in IT for pete's sake :-)

<quote>
[11:18:35] ~> sudo cryptsetup status _dev_sda3
/dev/mapper/_dev_sda3 is active:
cipher: aes-cbc-essiv:sha256
keysize: 256 bits
device: /dev/sda3
offset: 2056 sectors
size: 220377614 sectors
mode: read/write
[11:18:45] ~> sudo cryptsetup status cryptoswap
/dev/mapper/cryptoswap is active:
cipher: aes-cbc-essiv:sha256
keysize: 256 bits
device: /dev/sda5
offset: 0 sectors
size: 9847719 sectors
mode: read/write
</quote>

Best regards,

Guy S.
Encrypted swap, tmp and home partition in Ubuntu 9.04
Authored by: Anonymous on Wednesday 27 May 2009 @ 07:26 CEST
It seems to me that in Part 3 step 3 concerning the entry for /etc/fstab you don't want none listed as the mount point, you want /tmp. Otherwise no data will be put into the newly created encrypted space that is desired for tmp. Could be wrong though. Thanks for the great article.
Encrypted swap, tmp and home partition in Ubuntu 9.04
Authored by: Anonymous on Sunday 07 June 2009 @ 21:40 CEST
I tried following your tutorial while using an existing ext4 partition, everything seemed fine until I tried:

sudo cryptsetup -c aes-cbc-essiv:sha256 -y -s 256 luksFormat /dev/sda8


I got this error:

pam_mount(pam_mount.c:100): unknown pam_mount option "use_first_pass"
WARNING!
========
This will overwrite data on /dev/sda8 irrevocably.
Are you sure? (Type uppercase yes): YES
Command failed: Can not access device

Could you please tell me if I did something wrong?
Encrypted swap, tmp and home partition in Ubuntu 9.04
Authored by: Anonymous on Wednesday 24 June 2009 @ 23:49 CEST
Hi Lars

It is possible to set up Luks from the 9.04 alternate CD/DVD. I've done it on my laptop.

Just set up the whole disk as encrypted LVM, and say no to the question about encrypted home directory ( it's the eCryptfs thing ).

Then I made a backdoor for it which transmit my LUKS phassphrase using the leds on my X61 as a covert channel.... but that's another story.

Espen G
Encrypted swap, tmp and home partition in Ubuntu 9.04
Authored by: Anonymous on Friday 17 July 2009 @ 20:22 CEST
I know this is not such a recent tutorial, but I really hope someone reads this and can give a hand, I am kind of clueless here...

I used your tutorial to encrypt my home partition (but not swap or tmp, yet) everything seems to be working fine, but recently I found a very annoying and unexpected side effect to it: user crontabs are not executed anymore . I get a segfault error in /var/log/syslog every time the cron is supposed to run. Here's an example:

Jul 17 20:07:01 user-name kernel: [ 4547.150367] cron[5932]: segfault at 0 ip 00000000 sp bff1dd3c error 4 in cron[8048000+8000]

I believe some modifications should be done in the files located in /etc/pam.d/ but I have not found which yet.

Any of you had a similar problem? And if so, what did you do to fix it?

Any help will be much appreciated!
Encrypted swap, tmp and home partition in Ubuntu 9.04
Authored by: Anonymous on Thursday 23 July 2009 @ 19:59 CEST
Lars,

One question, how was the original partitioning set up? My vanilla Jaunty install had /sda1 with everything but what /sda5 took up for swap. lvmcreate doesn't let me play with /sda1../sda4 so I'm not sure if I'm missing something on the original partitioning scheme.
Encrypted swap, tmp and home partition in Ubuntu 9.04
Authored by: Anonymous on Sunday 23 August 2009 @ 18:49 CEST
I had tried this once and then done a reinstall of Ubuntu. When I set up the encrypted /tmp again, using the same /tmp partition and jumping straight to the fs- and crypttab entries it didn't work.
Apparently it doesn't overwrite anything with a luks header, so you really need the random data in step 2 (found that hint at archlinux). Works fine after that, with fstab as /dev/mapper/cryptotmp /tmp ext2 defaults 0 0 and crypttab as cryptotmp /dev/sda8 /dev/urandom cipher=aes-cbc-essiv:sha256,size=256,hash=sha256,tmp
Encrypted swap, tmp and home partition in Ubuntu 9.04
Authored by: Anonymous on Thursday 10 September 2009 @ 20:59 CEST
Your howto is excellent, thank you very much. The only problem I noticed was that you have the mount points mixed up in your swap and tmp sections. swap should have "none" and tmp should have "/tmp".
Encrypted swap, tmp and home partition in Ubuntu 9.04
Authored by: Anonymous on Tuesday 22 September 2009 @ 13:43 CEST
In Part 2 step 3, the entry in crypttab is /dev/random, this can cause a long 'hang' during boot while the entropy is collected - in my case over 5 minutes (before I remembers to hit some keys!). I changed this to /dev/urandom, with a much quicker and hands-free boot. Incidentally you seem confused between crypttab (correct) and cryptab (incorrect), just typos.
But this is a very very useful article, thanks very much.
Encrypted swap, tmp and home partition in Ubuntu 9.04
Authored by: Anonymous on Monday 21 December 2009 @ 13:26 CET
Why fill the partition with random data first? This howto does not do it:
https://help.ubuntu.com/community/Enc...ystemHowto

This can be unbearably slow for a large partition, say 100GB...
Encrypted swap, tmp and home partition in Ubuntu 9.04
Authored by: Anonymous on Thursday 12 August 2010 @ 19:29 CEST
Encrypted swap, tmp and home partition in Ubuntu 9.04
Authored by: Anonymous on Saturday 21 August 2010 @ 16:53 CEST