LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-01-2010, 03:34 PM   #1
alpha_lt
Member
 
Registered: Jul 2009
Location: Denmark
Distribution: Fedora
Posts: 95

Rep: Reputation: 15
Encrypting disk after innstallation


Dear all,

Currently I'm using Ubuntu 9.04 and when I installed it I got a question if I want to encrypt disk contents. I answered no. Now I think it is good idea to have such possibility. So is it possible to have the same encryption on running system ? I mean to make system such as I had answered 'yes' to that question.
Thanks.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-01-2010, 06:15 PM   #2
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
It's not a trivial exercise, and if you've not much effort invested in your system then the best course of action is to start over with a fresh install. If you want to go ahead and encrypt your drive keeping your existing install then just say so and I'll outline the steps.

If this system is a server, then reconsider. A server is normally permanently turned on, and most compromises occur over the network, so an encrypted disk won't offer much in the way of protection.

The only time an encrypted drive offers protection is when the machine is turned off and is physically stolen. So a laptop is a good candidate for an encrypted drive.

This, assumes you want to encrypt everything on the drive. If you want encrypted containers that are only temporarily mounted for short periods of time then the above doesn't apply, and you should be able to get going with your existing system.
 
Old 01-02-2010, 09:16 AM   #3
alpha_lt
Member
 
Registered: Jul 2009
Location: Denmark
Distribution: Fedora
Posts: 95

Original Poster
Rep: Reputation: 15
Hello,

Thank you for answering. First, its server and its important to encrypt data in my case.
Now I'm considering about what you said and what to choose. If I would find fast way to restore existing system after installation then I would go with fresh install. Currently I have made a lot of configs and scripts in the system, so writing them from scratch would be so painful. If it is possible to save something and after install restore than ok. If not, I would choose encrypt existing installation.
So if you will help me I will be greatly thankful for your effort.
 
Old 01-02-2010, 01:22 PM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
OK. Backup the existing system using rsync:

rsync -avH --exclude=<mount_point_of_backup drive> <mount_point_of_backup_drive>/

If there are multiple disk partitions on the system, you'll need to partition the backup drive into multiple partitions. If the backup drive needs partitioning, it will need formatting. The great thing about rsyncing entire disk partitions is this defragments the file system.

After you have a full backup, and rsync verifies files, preserves file attributes, copies links, and basically makes the system identical to what it was, if you use rsync -avH.

Then you can reinstall the system using the installation media. Boot up the new system, and restore the old system to the new system. The drive encryption info in embedded in the drive itself. If you ever want to change the password, you have do the whole process again. Or worse, if you ever forget the password, there is now way but guessing to get it back.

I can understand encrypting data, but binaries? A better way to go might be with an encrypted loop device. Because, if the whole drive in encrypted, whenever the system is running, none of the files are encrypted. Disk encryption is only an effective deterrent is the machine is turned off, and someone gains physical access to it while it's off.

When the machine is running, encryption is useless. Lets say you want to encrypt /etc/, but not the entire '/' directory. You'd make a sparse file like so:

dd if=/dev/urandom of=/etc.file bs=1M count=2000

That shouldn't take too long. Once the sparse file is made, load some modules:

modprobe loop
modprobe cryptoloop
modprobe aes


setup the loop device:

losetup -e aes /dev/loop1 /etc.file
password:


I'd pick at least a 20 character password. You can make good passwords, like I'll make an easy one to remember, but hard to guess: MarSh3333MarShmall0wS.

So now the loop device is setup with the sparse file. Format the loop device:

mkfs.ext3 /dev/loop1

Make a mount directory:
mkdir /etchold

Mount the drive:
mount -o loop,encryption=aes /etcfile /etchold
password:

Use the same password for mounting as for losetup.

/etchold is the encrypted loop device mount point.

Copy over /etc
rsync -avH /etc/* /etchold/
mv /etc /etc1
mv /etchold /etc


Write an /etc/init.d/ script, called loop:

#!/bin/bash
case "$1" in
start)

modprobe loop
modprobe cryptoloop
modprobe aes

losetup -e aes /dev/loop1 /etcfile
mount -o loop,encryption=aes /etcfile /etc
;;

stop)

losetup -d /dev/loop1
;;

*)
echo "usage: /etc/init.d/loop {start|stop}"

exit 1
;;

esac

exit 0

esac


Make links to loop in /etc/rc.d/rc.1, rc.2, rc.3, rc.5, place fairly early in the sequences for those runlevels. You'll figure it out. The same procedure applies to any directory, not only /etc
 
2 members found this post helpful.
Old 01-02-2010, 02:45 PM   #5
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
bah, Wish I'd seen your post 1/2 an hour earlier.

Anyhow, while I've done it, here it is:

Hello,

Just before we start, we can use `dpkg -l > pkgs.txt` to create a list of all installed packages and you can copy that with /etc to another hard drive. Then after you've reinstalled you can install all the packages from the list and copy the config files back into place.

OK, here we go:

First of all you will need a spare drive to backup to and a Live CD, the current Ubuntu one will be perfect. Boot your machine up using the live cd, and make a directory to work from, lets say /mnt/os.

Now, mount the paritions on your hardrive under /mnt/os in the same fasion as they would be mounted if the system was booted from disk, so mount your root partition to /mnt/os, if you have a /boot partition, mount that /mnt/os/boot, and the same with any other partitions you might have, for example /home or /usr.

Make another directory, for example /mnt/backup and mount your spare drive to that, then cd into /mnt/os and tar it all up to a file in /mnt/backup:

Code:
# cd /mnt/os
# tar czf /mnt/backup/server-20100102.tar.gz
Once that is completed, unmount everything.

Now, time to nuke the drive, even if we formatted at this point and created encrypted partitions, a determined individual/organisation may be able to recover what was on the drive prior to the encrypted drives.

Code:
# dd if=/dev/urandom of=/dev/sda bs=4M
# dd if=/dev/urandom of=/dev/sda bs=4M
# dd if=/dev/zero of=/dev/sda bs=4M
This will take several hours, but should do the job nicely. If you want it done quicker then you can use a utility that does uses poorer quality randomisation.

Now you can create a new partition layout using cfdisk or fdisk, set the types correctly, 0x82 for swap and 0x83 for file system partitions. You _will_ need a separate /boot partition and that can't be encrypted otherwise the system won't be able to boot.

Follow sections 6 and 7 on the encryption page of the Arch Linux wiki: http://wiki.archlinux.org/index.php/...S_for_dm-crypt

Now create filesystems on the open containers:

Code:
# mkfs.ext2 /dev/sda1 // for /boot
# mkfs.ext3 /dev/mapper/root
# mkfs.ext3 /dev/mapper/home
Mount the new filesystems as you did earlier to /mnt/os, so assuming you have the above paritions:

Code:
# mount /dev/mapper/root /mnt/os
# mount /dev/mapper/home /mnt/os/home
# mount /dev/sda1 /mnt/os/boot
Next step is to untar your files over the new layout:

Code:
# cd /mnt/os
# tar xzf /mnt/backup/server-20100102.tar.gz
We now need to make some final config changes, and for some stuff we need to chroot into the install.

Code:
# for d in dev proc sys; do mount --bind /$d /mnt/os/$d; done
# chroot /mnt/os
Change /etc/fstab to suit the new devices for example /dev/sda2 to /dev/mapper/root. Also, if you didn't previously have a /boot partition you will need to alter /boot/grub/menu.lst and set the paths correctly:

From:
Code:
# kopt=root=/dev/sda2 ro
To:

Code:
# kopt=root=/dev/mapper/root ro
Also, we need a new file: /etc/cryptsetup

Code:
# <target name> <source device>         <key file>        <options>
swap            /dev/sda3               "apassword"       swap
root            /dev/sda2               none              luks
home            /dev/sda4               "myotherpassword" luks
Once that is done you need to install grub back to the mbr and update the initramfs image:

Code:
# update-initramfs -u
# grub
> root (hd0,0)
> setup (hd0)
> quit/exit // I can never remember which on it is.
exit the chroot environment, unmount all the partitions and reboot, removing the cd. If all has gone to plan, your system should boot up from the hard drive, and everything is done and dusted.

Useful Links/Further reading:

https://help.ubuntu.com/community/EncryptedFilesystems
https://help.ubuntu.com/community/En...ilesystemHowto
https://help.ubuntu.com/community/En...stemOnIntrepid
http://wiki.archlinux.org/index.php/...S_for_dm-crypt


Disclaimer:

Do not blindly follow this step by step. It isn't complete, it is intended to give you the order and what needs doing to accomplish this. Read the information at the links I've provided and think about what your doing. If a command doesn't do exactly what you thought, don't panic, think carefully about the error message. Have a look on google, or post the error here.

Also, a lot of the ubuntu how tos talk about creating hooks and editing configuration for initramfs-tools, I'm not sure this is necessary in the current version. My laptop is encrypted and has the default configuration, nothing specifically mentions encryption in /etc/initramfs-tools, so it looks like and encrypted root is support "out-of-the-box".
 
1 members found this post helpful.
Old 01-02-2010, 02:47 PM   #6
alpha_lt
Member
 
Registered: Jul 2009
Location: Denmark
Distribution: Fedora
Posts: 95

Original Poster
Rep: Reputation: 15
Hi,

Thank you for this mega tutorial. Its a little bit too difficult for me. Anyway I need functionality like you said - only from physical intervention. If I need to encrypt only some file then I would use ccrypt. Therefore you gave me a very good idea - I don't need to encrypt binaries, basically I need to encrypt one partition /home. Is it possible ? I can freely move all files from this partition now and I can even delete it, but later I need it to act like it was encrypted originally from Ubuntu installation.
 
Old 01-02-2010, 02:52 PM   #7
alpha_lt
Member
 
Registered: Jul 2009
Location: Denmark
Distribution: Fedora
Posts: 95

Original Poster
Rep: Reputation: 15
WOW. Great posts, great help. Yes it might be too complicated for me, but I think it is still possible. Thank you both very much for your effort and spent time writing these posts. Its great help.
 
Old 01-02-2010, 03:03 PM   #8
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
Just encrypting /home is much more straightforward. You can either follow AwesomeMachine's tutorial, or follow this tutorial: https://help.ubuntu.com/community/En...lesystemHowto3 Stick with the Ubuntu 6.06 bits, they still apply, the 5.10 parts of the howto are not relevant.

Either method is fine, however using luks containers is a more modern method.

You should really consider encrypting the swap partition too.

Edit:

With a it being a server, your requirements might dictate that you need to keep the logs encrypted too, in which case you'll need to encrypt /var

Last edited by phil.d.g; 01-02-2010 at 03:06 PM.
 
1 members found this post helpful.
Old 01-02-2010, 03:06 PM   #9
alpha_lt
Member
 
Registered: Jul 2009
Location: Denmark
Distribution: Fedora
Posts: 95

Original Poster
Rep: Reputation: 15
Thanks again for your effort ! I'm very glad that I'm able to find a lot of help and support in these forums.
Now I'm gone to encrypt
 
Old 01-03-2010, 05:33 AM   #10
alpha_lt
Member
 
Registered: Jul 2009
Location: Denmark
Distribution: Fedora
Posts: 95

Original Poster
Rep: Reputation: 15
Hello,

Little more help needed. All my existing partitions in fstab is identified with UUID and there are no mountpoints with /dev/sda or /dev/hda. All partitions mounted as /dev/mapper. So the link for Ubuntu 6.04 is not good for me I think.....
 
Old 01-03-2010, 05:38 AM   #11
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
Sounds like you may be using LVM, the UUID for /home will change

can you post the output of `sudo lvs`, `mount` and `cat /etc/fstab`. If you are using LVM then things will change a little, but most of the tutorial is still relevant.
 
Old 01-03-2010, 05:51 AM   #12
alpha_lt
Member
 
Registered: Jul 2009
Location: Denmark
Distribution: Fedora
Posts: 95

Original Poster
Rep: Reputation: 15
Hello,

Yes, I use LVM. Here is my outputs:

'sudo lvs'
Code:
  LV     VG     Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  home   as9723 -wi-ao 453.35G                                      
  root   as9723 -wi-ao   6.52G                                      
  swap_1 as9723 -wi-ao   5.65G
'mount'
Code:
/dev/mapper/as9723-root on / type ext3 (rw,relatime,errors=remount-ro)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
varrun on /var/run type tmpfs (rw,nosuid,mode=0755)
varlock on /var/lock type tmpfs (rw,noexec,nosuid,nodev,mode=1777)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
lrm on /lib/modules/2.6.28-17-server/volatile type tmpfs (rw,mode=755)
/dev/sda5 on /boot type ext2 (rw,relatime)
/dev/mapper/as9723-home on /home type ext3 (rw,relatime)
securityfs on /sys/kernel/security type securityfs (rw)
'cat /etc/fstab'
Code:
# /etc/fstab: static file system information.
#
# Use 'vol_id --uuid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
# / was on /dev/mapper/as9723-root during installation
UUID=93784424-6768-41f8-89aa-0eed93cbecbc /               ext3    relatime,errors=remount-ro 0       1
# /boot was on /dev/sda5 during installation
UUID=4fb0b05b-2862-443e-922a-731b1f68dd97 /boot           ext2    relatime        0       2
# /home was on /dev/mapper/as9723-home during installation
UUID=f0c9163f-f28b-4fcd-b549-61d79b954403 /home           ext3    relatime        0       2
# swap was on /dev/mapper/as9723-swap_1 during installation
UUID=a4e69841-e676-4068-b18e-c53c9be076e5 none            swap    sw              0       0
 
Old 01-03-2010, 06:05 AM   #13
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
Ok, encrypting /home is going to be quite straight forward.

Backup /home, then follow the tutorial but use /dev/mapper/as9723-home as the device to create the container on. Once you have formatted the encrypted container you will need to update the UUID in /etc/fstab.

You can find out the new UUID by running:

Code:
# tune2fs -l /dev/mapper/home
 
Old 01-03-2010, 06:06 AM   #14
alpha_lt
Member
 
Registered: Jul 2009
Location: Denmark
Distribution: Fedora
Posts: 95

Original Poster
Rep: Reputation: 15
I hope the same goes for swap ?
 
Old 01-03-2010, 06:35 AM   #15
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
Yes you can do the same with swap.

Just make sure that in the boot process lvm is setup before the luks containers.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Custom kernel problems after encrypting disk brooky9999 Slackware 15 09-18-2008 02:30 PM
encrypting tariq07 Linux - Security 1 03-13-2007 01:35 AM
Encrypting a file vedanu Programming 11 03-03-2007 01:49 AM
Encrypting harddrive Ephracis Linux - Security 9 07-31-2005 10:55 PM
encrypting im1crazyassmofo Linux - General 1 04-20-2003 09:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration