Softpedia
 

NEWS CATEGORIES:



NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
TRENDING TODAY
Home > News > Linux > Ubuntu Tips and Tricks

July 29th, 2007, 14:50 GMT · By

Encrypted Ubuntu 7.04

SHARE:

Adjust text size:

Secure your data
Enlarge picture
Did you ever live with the fear that somebody may break into your system one day and steal your files? Well, those days are over, because you can now have an entire encrypted operating system. For this setup, we used a freshly installed Ubuntu 7.04 with up-to-date software, nothing else installed. But the following guide is supposed to work with your actual Ubuntu 7.04 installation (no reinstall needed). Beware though: if you don't have the partitions setup like it’s shown below, this will NOT work.

I will NOT be held responsible for any data loss on your hard drive if this process will NOT work for you, so you have been warned: TRY THIS AT YOUR OWN RISK!




Things needed:

- Ubuntu LiveCD
- cryptsetup software

Here is how your partitions should look like:

CODE
/dev/sda1 -> /boot (about 150-200 MB, mine is 150 MB)
/dev/sda2 -> swap (double as your computer RAM, mine is 2 GB because I have 1 GB of RAM)
/dev/sda3 -> root (/) (should be more than 5 GB, mine is 35 GB)

WARNING: I have a SATA drive, therefore my partitions are named sda. If you have an IDE drive, then you have to replace sda with hda in the guide.

STEP 1 – Boot from the LiveCD

Insert the Ubuntu 7.04 LiveCD into your optical drive and reboot your computer in order to boot from the CD. When the CD has loaded, open up a terminal (Applications -> Accessories -> Terminal) and become root by typing:

CODE
sudo su

You will be permanently root from now on (that means you will not have to type sudo anymore, until you exit this session).

STEP 2 – Prepare the environment and backup the data

Let's prepare the system for the encryption process by loading some necessary modules into the kernel. Type, or copy / paste the following lines in the terminal window:

CODE
modprobe aes
modprobe dm-crypt
modprobe dm-mod
modprobe sha256

Go to System -> Administration -> Software Sources, check the "Community-maintained Open Source software (universe)" and "Software restricted by copyright or legal issues (multiverse)" options, then click the "Close" button and when you'll be asked to reload the information about software sources, click the "Reload" button. Wait until the Software Source window disappears and then type in the terminal window:

CODE
apt-get install cryptsetup

Then let's backup the existing data by creating some temporary folders:

CODE
cd /mnt
mkdir boot root tmp

Then mount the existing partitions to the newly created folders:

CODE
mount /dev/sda1 boot
mount /dev/sda3 root

And now backup the data:

CODE
mkdir tmp/root
cp -axv root/* tmp/root

This last code will output a lot of text (the files that are being copied), so wait until it stops. It takes about 6-7 minutes (depending on the number of files).

STEP 3 – Encrypt the filesystem

Good, now that the backup has finished, umount the drive with:

CODE
umount root

And encrypt the filesystem with the following command:

CODE
cryptsetup -c aes-cbc-essiv:sha256 -y -s 256 luksFormat /dev/sda3

WARNING: All the data will be permanently erased!

Type YES when asked, and enter a strong password (twice). Then type:

CODE
cryptsetup luksOpen /dev/sda3 root
mkfs.ext3 /dev/mapper/root
mount /dev/mapper/root root

Now let's copy back the data from the temporary folder to the newly created encrypted root partition:

CODE
cp -axv tmp/root/* root

Same as above, it will output a lot of text, so wait until it finishes and remove the temporary folder:

CODE
rm -rf tmp/root

STEP 4 – Final adjustments

The filesystem is encrypted now, but it will not work until you do some final adjustments. Type, or copy / paste the following lines:

CODE
mkdir root/boot
mount /dev/sda1 root/boot
chroot root

At this moment, you are "virtually" in your root partition, and you can make modifications to it. Let's begin by installing the cryptsetup software:

CODE
apt-get update
apt-get install cryptsetup

Then let's add the necessary kernel modules to the /etc/initramfs-tools/modules file, so that they can be loaded at boot time:

CODE
nano etc/initramfs-tools/modules

And add the following lines to the end of the file:

CODE
aes
dm-crypt
dm-mod
sha256

Hit CTRL+O to and then ENTER to save the file. Hit CTRL+X to close the nano editor.

You must adjust the /etc/fstab file to mount the correct encrypted root partition:

CODE
nano etc/fstab

And change the line that looks like this (the UUID is just an example... yours will be different):

CODE
# /dev/sda3 UUID=4565t675-6c67-56hg-hg7j-67g5jk00b562 / ext3 defaults,errors=remount-ro 0 1

To look like this one:

CODE
/dev/mapper/root / ext3 defaults,errors=remount-ro 0 1

So basically, you just replace (# /dev/sda3 UUID=4565t675-6c67-56hg-hg7j-67g5jk00b562) with (/dev/mapper/root).

Hit CTRL+O and then ENTER to save the file. Hit CTRL+X to close the nano editor.

Now you must edit the /etc/crypttab file:

CODE
nano etc/crypttab

And add the following line at the end of the file:

CODE
root /dev/sda3 none luks,retry=1,cipher=aes-cbc-essiv:sha256

Hit CTRL+O and then ENTER to save the file. Hit CTRL+X to close the nano editor.

And now you have to edit the /boot/grub/menu.lst file:

CODE
nano boot/grub/menu.lst

Search the line that looks like this (the UUID is just an example... yours will be different):

CODE
# kopt=root= UUID=4565t675-6c67-56hg-hg7j-67g5jk00b562 ro

And change it to look like this:

CODE
# kopt=root=/dev/mapper/root ro

Hit CTRL+O and then ENTER to save the file. Hit CTRL+X to close the nano editor.

Update GRUB with the following command:

CODE
update-grub

And check the /boot/grub/menu.lst file to see if the entries changed like this:

CODE
title Ubuntu, kernel 2.6.20-16-generic

root (hd0,0)

kernel /vmlinuz-2.6.20-16-generic root=/dev/mapper/root ro quiet splash vga=775

initrd /initrd.img-2.6.20-16-generic

quiet

savedefault


As you can see, I have an extra option at the end of the kernel line: vga=775. You are not supposed to have or add this option! Just make sure that you have root=/dev/mapper/root option. If so, then you can update initramfs with the following command:

CODE
update-initramfs -u All

WARNING: If you see an error message about "libdevmapper", just ignore it and continue with the guide.

Exit the chrooted environment and reboot the system with:

CODE
exit
reboot

When the system starts, you will see the Ubuntu boot splash, which will disappear after a few seconds and all you'll be able to see is a blinking line on the top left side of your monitor. Now you should type the password you've setup when you encrypted the filesystem and hit ENTER. You will notice that (if you typed the password correctly), the system continues to boot. That's it folks, your whole Ubuntu 7.04 is now fully encrypted!
FILED UNDER:
Ubuntu
encrypted
secure


33,423 hits · 15 comments
Link to this article · Print article · Send to friend

MUST-READ RELATED ARTICLES:


What Should You Expect from Ubuntu 7.04

How to Install Ubuntu 7.04 (Windows User P.O.V.)

Ubuntu 7.04 on PS3

Ubuntu Is Enterprise Friendly

Ubuntu 8.04 Is an LTS Release

READER COMMENTS:


Comment #1 by: marriouss on 30 Jul 2007, 07:43 UTC reply to this comment

Great tutorial!!! Thanks!!!

I've one question: what happens if somehow my system gets damaged (doesn't boot anymore) - will I be able to read the data that I have on the encrypted partition (maybe using the ubuntu live cd)?
A tutorial about these would be great.

Comment #1.1 by: marius.nestor on 30 Jul 2007, 08:15 GMT

Hi! Thanks for your nice words!

It's very simple to mount encrypted partitions.

1. Boot from an Ubuntu LiveCD,
2. Install the cryptsetup software (remember to enable the Universe repository),
3. Create a folder somewhere (sudo mkdir /mnt/tmp)
4. Open the encrypted partition with:

cryptsetup luksOpen /dev/sda3 /mnt/tmp

You'll be asked for the password :)

After that, Ubuntu will immediately open a folder with the hard drive's content.

NOTE: The partition can be /dev/sda3 like in the example above, or any other...

Regards


Comment #2 by: marriouss on 30 Jul 2007, 09:18 UTC reply to this comment

Many thanks, Marius!

For a ubuntu laptop this thing is absolutely great.

Thanks again :)

Comment #2.1 by: marius.nestor on 30 Jul 2007, 21:42 GMT

You're welcome!

There is a little mistake in the example I gave you:

4. Open the encrypted partition with:

sudo cryptsetup luksOpen /dev/sda3 tmp

You'll be asked for the password :)

5. Mount the encrypted partition with:

sudo mount /dev/mapper/tmp /mnt/tmp

And you will find the hard drive's content in /mnt/tmp


Comment #3 by: marriouss on 31 Jul 2007, 09:13 UTC reply to this comment

Good to know:) Thanks


Comment #4 by: joinsmith on 22 Sep 2007, 16:04 UTC reply to this comment

Great guide, but I have a large problem.

I am new to Linux, so this is probably not a big one for you guys.

I have made a fresh install of Ubuntu 7.04 (desktop edition). I have the same setup as int the example above, except that my root is much larger (250GB). I have not installed any additional components before I started this guide.

When I try cp -axv root/* tmp/root, it starts okay, but soon I get problem and run out of space on tmp/root. I don't understand what tmp/root is. Is tmp/root a ramdisk? it could hardly be my CD-ROM drive or harddisk. What should I do ? Please help me!

Comment #4.1 by: marius.nestor on 22 Sep 2007, 17:29 GMT

Hello,

How much RAM do you have? 512 MB?


Comment #5 by: joinsmith on 22 Sep 2007, 17:49 UTC reply to this comment

Hi Marius!

I have 2GB.


Comment #6 by: marius.nestor on 22 Sep 2007, 17:58 UTC reply to this comment

That's very strange... could you please paste here the exact error you receive when you ran out of space?

And please try to run the whole thing again, only this time create a temporary partition on the /mnt folder. Just replace /tmp with /mnt in the above guide.

P.S.: How big is your SWAP partition?

Comment #6.1 by: joinsmith on 23 Sep 2007, 13:06 GMT

I started all over again. I removed all partitions and created new ones, those were also formated. I am getting good at installing Ubuntu now :-)

Here is a log of what I have done. I did not attach all lines from the copy process, only the last lines, but there you can see the errors. I also tried replacing tmp with mnt, but with the same result as in the log below.

root@ubuntu:/home/ubuntu# sudo su
root@ubuntu:/home/ubuntu# fdisk -l

Disk /dev/sda: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 19 152586 83 Linux
/dev/sda2 66 563 4000185 82 Linux swap / Solaris
/dev/sda3 831 30401 237529057 83 Linux
root@ubuntu:/home/ubuntu# modprobe aes
root@ubuntu:/home/ubuntu# modprobe dm-crypt
root@ubuntu:/home/ubuntu# modprobe dm-mod
root@ubuntu:/home/ubuntu# modprobe sha256
root@ubuntu:/home/ubuntu# apt-get install cryptsetup
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
cryptsetup
0 upgraded, 1 newly installed, 0 to remove and 76 not upgraded.
Need to get 266kB of archives.
After unpacking 762kB of additional disk space will be used.
Get:1 http://ftp.ds.karen.hj.se feisty/universe cryptsetup 2:1.0.4 svn26-1ubuntu2 [266kB]
Fetched 266kB in 0s (565kB/s)
Selecting previously deselected package cryptsetup.
(Reading database ... 89899 files and directories currently installed.)
Unpacking cryptsetup (from .../cryptsetup_2:1.0.4 svn26-1ubuntu2_amd64.deb) ...
Setting up cryptsetup (1.0.4 svn26-1ubuntu2) ...
update-initramfs: Generating /boot/initrd.img-2.6.20-15-generic

root@ubuntu:/home/ubuntu# cd /mnt
root@ubuntu:/mnt# mkdir boot root tmp
root@ubuntu:/mnt# mount /dev/sda1 boot
root@ubuntu:/mnt# mount /dev/sda3 root
root@ubuntu:/mnt# mkdir tmp/root
root@ubuntu:/mnt# cp -axv root/* tmp/root
.
.
cp: writing `tmp/root/var/run/motd': No space left on device
`root/var/run/klogd' -> `tmp/root/var/run/klogd'
`root/var/run/klogd/kmsg' -> `tmp/root/var/run/klogd/kmsg'
`root/var/run/samba' -> `tmp/root/var/run/samba'
`root/var/run/NetworkManager' -> `tmp/root/var/run/NetworkManager'
`root/var/run/NetworkManager/NetworkManager.pid' -> `tmp/root/var/run/NetworkManager/NetworkManager.pid'
cp: writing `tmp/root/var/run/NetworkManager/NetworkManager.pid': No space left on device
`root/var/run/avahi-daemon' -> `tmp/root/var/run/avahi-daemon'
`root/var/run/cups' -> `tmp/root/var/run/cups'
`root/var/run/cups/certs' -> `tmp/root/var/run/cups/certs'
`root/var/run/dbus' -> `tmp/root/var/run/dbus'
`root/var/run/hal' -> `tmp/root/var/run/hal'
`root/var/run/screen' -> `tmp/root/var/run/screen'
`root/var/run/utmp' -> `tmp/root/var/run/utmp'
`root/var/tmp' -> `tmp/root/var/tmp'
`root/var/local' -> `tmp/root/var/local'
`root/var/opt' -> `tmp/root/var/opt'
`root/var/mail' -> `tmp/root/var/mail'
`root/vmlinuz' -> `tmp/root/vmlinuz'
root@ubuntu:/mnt#

Comment #6.2 by: marius.nestor on 26 Sep 2007, 14:04 GMT

It looks like only on some files it gives you the "No space left on device" warning... which is quite strange...

You didn't tell me how much is your SWAP partition.

Did you try to finish the guide and see if you can boot the system?


Comment #7 by: joinsmith on 30 Sep 2007, 15:44 UTC reply to this comment

Hi Marius!

My SWAP partition is 4GB but i have also tried 6GB and 10GB. But I gave up yesterday. I installed another harddisk and used that one as tmp-storage and then there were no more errors. Maybe this problem I have is because I use the 64bit version of Ubuntu.

The rest of your guide worked fine.

Comment #7.1 by: marius.nestor on 30 Sep 2007, 15:48 GMT

It could be from the 64-bit, I didn't try it on a 64-bit Ubuntu... but I will when Gutsy will be released.


Comment #8 by: Daniel on 03 Dec 2007, 18:05 UTC reply to this comment

What iss with the /home Partition?? How can I encrypt these,if I use that partition scheme?

/boot dev/hda1
/swap /dev/hada2
/ /dev/hda3
/home /dev/hda5


Comment #9 by: Arsenal on 16 Jan 2009, 22:46 UTC reply to this comment

You just saved my Ubuntu, somehow it got something wrong after installing a new kernel and updating the menu.lst. So I got quite some errors, but with your nice tutorial I was able to pinpoint it and change it accordingly.

Thank you very much.

Copyright © 2001-2013 Softpedia. Contact/Tip us at

WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM