Encrypt your /home this Christmas: part two - creating an encrypted partition

9 December 2007

0. The Plan

So if you read the last post, you will know that I am suggesting that everyone who owns a Linux laptop encrypts their home directory by Christmas. As part of that I am explaining how I implemented encryption on my laptop. To quickly recap:

1. Make yourself a new partition

So the first step is to make yourself a new partition. You will possibly have to resize existing partitions to make space. This is not a big problem, one easy way is to use the graphical tool called gparted.

If you don't know what partitions are then read my `introduction to disk partitions`_ and possibly my post on `setting up partitions for Gentoo`_.

One you have room, you need to make the new partition with gparted or with the curses program cfisk or the plain command line version fdisk (e.g. type sudo cfdisk - be careful what you press!).

Remember or write down the name of the partition that you make. For example, it may be called /dev/sda4. If you forget then sudo fdisk -l will tell you what partitions you have.

Note, don't worry about formatting a filesystem at this point, we will need to reformat it later anyway.

2. Install cryptsetup

The next step is to install the required userspace tool. The easiest way is to use your friendly neighbourhood package manager. Let's take two examples:

On Gentoo Linux:

sudo emerge cryptsetup-luks

If you have compiled your own kernel, then you need add certain modules, crib them from here.

On Ubuntu/Debian Linux:

sudo apt-get install cryptsetup

3. Encrypt the partition

There are lots of available algorithms. We will use a pretty standard one - the AES algorithm with a 256 key. Here is what the NSA says:

> The design and strength of all key lengths of the AES algorithm (i.e., 128, 192 and 256) are sufficient to protect classified information up to the SECRET level. TOP SECRET information will require use of either the 192 or 256 key lengths (NSA 2003, page 2).

So AES with a 256 key should be secure enough to stop any potential identity thieves.

Imagine the partition is called /dev/sda4, then we need to use the following command to encrypt the partition:

sudo cryptsetup luksFormat -c aes-cbc-essiv:sha256 /dev/sda4

You will then be asked for a passphrase, make sure it is strong and that you remember it. Do not write it down on this computer, for example in a text file or document, that would undermine the whole approach, for reasons we shall see later. If you have to write it down at this point, write it on paper, but never keep the paper with the computer.

Here is what happened when I encrypted my partition:

> ::user@warrior:~$ sudo cryptsetup luksFormat -c aes-cbc-essiv:sha256 /dev/sda4 > > WARNING! > ======== > This will overwrite data on /dev/sda4 irrevocably. > > Are you sure? (Type uppercase yes): YES > Enter LUKS passphrase: > Verify passphrase: > Command successful.

4. Add the partition to the crypt table

The next step is add the partition to the /etc/crypttab file. You can do this with any text editor that you like such as gedit, nano, vim, or for example with emacs:

sudo emacs /etc/crypttab

You then need to add the following line, as always replacing /dev/sda4 with whatever your partition was called in part 1 above:

crypt-home /dev/sda4 none luks

We can see if this worked by unlocking the partition, use the following command:

sudo /etc/init.d/cryptdisks start

This should work as follows:

> ::user@warrior:~$ sudo /etc/init.d/cryptdisks start > * Starting remaining crypto disks... > Enter LUKS passphrase: > key slot 0 unlocked. > Command successful. > [ OK ]

The system can now see a partition at: /dev/mapper/crypt-home

**5. Format the encrypted partition **

System Message: WARNING/2 (<string>, line 119); backlink

Inline strong start-string without end-string.

Now we need to give the partition a filesystem. I chose to have a classic reiserfs filesystem, as shown below, I have skipped some of the output to make it more consise:

> ::user@warrior:~$ sudo mkfs.reiserfs /dev/mapper/crypt-home > > ALL DATA WILL BE LOST ON '/dev/mapper/crypt-home'! > Continue (y/n):y > Initializing journal - 0%....20%....40%....60%....80%....100% > Syncing..ok > > ReiserFS is successfully created on /dev/mapper/crypt-home.

6. Add the encrypted partition to the file system table

Next we need to add the partition to the file system table. For the moment, we will just use a dummy mount point, later on we will change it to /home.

So firstly, lets create the mount point:

sudo mkdir /mnt/sda4

Now we need to edit the filesystem table, as before substitute the word emacs for your favourite text editor:

sudo emacs /etc/fstab

Add the following line:

/dev/mapper/crypt-home /mnt/sda4 reiserfs defaults 0 2

We can then try to mount the partition:

sudo mount /mnt/sda4/

If all goes well, we should be able to see the partition in the disk space list provided by df:

> ::user@warrior:~$ df > > /dev/mapper/crypt-home 29293052 32840 29260212 1% /mnt/sda4

**7. Reboot to test the system. **

System Message: WARNING/2 (<string>, line 163); backlink

Inline strong start-string without end-string.

Assuming this is a laptop not a server, let's reboot to test what happens. If this is a server then you might want to restart the relevant init scripts instead to have the same effect.

What should happen is that while your system boots, it will ask you for your passphrase on the command line. When the system has booted you should be able to see the encrypted partition in df (or gnome-system-monitor if you prefer). You should also be able to write to the partition (as root at least).

Make sure everything works as you expect before continuing, in the next post we will copy our data to this partition.

If you have forgotten your passphrase already then go back to 3 above and start over, don't forget it this time.

There are ways to improve the setup, for example gcryptmount allows you to enter the password at the graphical boot screen instead. Currently gcryptmount is only packaged for Gentoo, with some minor fiddling of the paths (e.g. to represent your /etc layout), you should be able to get to it work on your distro. I may come back to this later.

Continue with part three - moving your data to the encrypted partition.

References

Discuss this post - Leave a comment

1 Erdem says...

Can we do this on desktop PC's? Good article. Keep on the good work! :)

Posted at 9:15 p.m. on December 10, 2007


2 Zeth says...

Hi Erdem,

Sure desktop Linux PCs will be fine. I focused on laptops because their physical security is often at a high level of risk. But desktops can get stolen too.

Posted at 10:40 a.m. on December 11, 2007


3 pioto says...

Should we be using sys-fs/cryptsetup instead of sys-fs/cryptsetup-luks in gentoo? It looks like upstream renamed the package recently, but I could be wrong.

Posted at 7:41 p.m. on December 14, 2007


What do you have to say?

Show Editing Help


About

Hello, my name is Zeth, I'll be your host here.

Command Line Warriors is about taking control of your own technology, it looks at our experiences of computing; especially using GNU/Linux, the Python programming language, the command-line and issues such as techno-ethics, best practices and whatever is cool now. If you take control of your technology then you are a Warrior too!

This site is your site too which means that you can contribute and get involved. You can leave comments using the facility provided. For me, the comments and discussions are by far the best part of the site. So please do have your say!

Latest Discussions

Nui

July 18, 2008
Hmm, this would be more persuasive as an argument with some evidence. I am a happy admin of Windows and a novice user of Linux, so I have taken the ...
Give Linux a chance

Paddy3118

July 18, 2008
Hi, I too work with Electronic Design Automation tools, where Tcl is used extensively. I tend to only occasionally have to write in Tcl and so find the TclTutor utility: ...
Python and TCL

Cliff Wells

July 17, 2008
I personally cannot live without the Web Developer extension or Firebug. Unfortunately these are probably both among the more difficult to port extensions. Given how poorly Firefox functions on Linux ...
Will Epiphany be able to compete with Firefox's extensions?

making money on the internet

July 17, 2008
[url=http://www.divinecaroline.com/public/user/profile?user_id=83997]extra money 101waystoincome.com[/url]
A year after my 2007 predictions - the score card

Leatherjackets99

July 16, 2008
New Style in Leather Jackets For Man and Woman at http://www.Leatherjackets99.com They Offer Free Shipment Worldwide.
Email Syntax Check in Python

Åke Forslund

July 13, 2008
I'm pretty much a novice in both of these languages but I find them both easy to use and preform the tasks I give them. However I rarely use them ...
Python and TCL

Christopher Thoday

July 12, 2008
A single test is not sufficient to give you confidence that the algorithm is working. You should make 'number' an argument of 'main' so that you can test some boundary ...
Python and TCL

paul21

July 10, 2008
Shame on Mozilla. They should make developers specify the extension license before hosting it. They should show the license next to download button as well.
Are your Firefox extensions proprietary software?

Tris

July 8, 2008
Justin - You say they had not heard of Linux? That doesn't sound very professional to me!
Give Linux a chance

michael

July 8, 2008
what about Galeon? in Gnome i use Galeon mostly. it is fast and stable and has a nice portal with search masks for Debian, FSF, Freshmeat and so on. wtf ...
Will Epiphany be able to compete with Firefox's extensions?

vermin

July 7, 2008
> Eventually, after a bit of digging and Googling, I found their Toolbar-License... You simply found the license of the StumbleUpon Toolbar for Internet Explorer. This is another product, much ...
Are your Firefox extensions proprietary software?

Andrew West

July 6, 2008
Both the Python and the Tcl example could do with error checking. While at first this may not seem on topic with the post I think it better shows the ...
Python and TCL

Kurushiyama

June 30, 2008
XML is no replacement for SGML, it's a subset.
An Introduction to ReStructuredText