[转载]Encrypted home partition in Linux with DM_Crypt
原始出处:[url]http://polishlinux.org/howtos/encrypted-home-partition-in-linux/[/url][b]Have you ever wondered what would have happened if all the important data have been stolen from your mobile PC? For example the information about a confidential project you have been working on for the last 2 years in your company … A horrifying vision, isn’t it? If you don’t want it come true, please consider encrypting your home drive. Here is how to do this in Linux.[/b]
[i]Author: Marcin Lipiec[/i]
This is what you will need to prepare your Linux computer for encrypting your home drive:
[list][li][url=http://en.wikipedia.org/wiki/DM-Crypt]DM-Crypt[/url] — open-source tool for encryption the whole HDD partition. It encrypts data before writing them on HDD. Available for kernel 2.6.4 and later. [/li][li][url=http://luks.endorphin.org/about]LUKS[/url] - (Linux Unified Key Setup) — standard HDD encryption system for Linux. [/li][li][url=http://www.saout.de/tikiwiki/tiki-index.php?page=cryptsetup]cryptsetup[/url] — enables simultaneous use of LUKS and DM-Crypt. [/li][/list][size=4][b]Necessary software download[/b][/size]
The program you will need is [b]cryptsetup[/b] with [b]LUKS[/b] handling. In order to install it you may either use your distribution’s repositories or use the general method (compilation from sources).
The distro-specific method is recommended. In Ubuntu you only need to type [font=nsimsun]apt-get install cryptsetup[/font] in the terminal windows (or use Synaptic Package Manager to do the same in a graphical interface.
If you prefer the source-based way, just open up a terminal and type [font=nsimsun]wget [url]http://luks.endorphin.org/source/cryptsetup-luks-1.0.4.tar.bz2[/url][/font]. The installation proceeds as usual:
[pre]./configuremakemake install[/pre]Done. If all went right, the installation should be now finished and the encryption software ready to be used.
[size=4][b]Key [i]cryptsetup[/i] options[/b][/size]
Cryptsetup enables you to perform different types of encryption and offers some additional options you may be interested in. Here is a brief overview of them.
[pre]root@host:~# cryptsetup OPTIONS action name device - general syntaxOPTIONS:-c - here enter a cipher algorithm, the default is AES with 256-bit key-h - displays help menu-y - user verification, you will be prompted twice for the password-d - loads password from the file. This option is ignored when youuse [i]-y[/i] switch-s - you can enter the key size (in bits)ACTIONS:create - creates HDD mapremove - deletes HDD mapreload - reloads HDD mapsize - increases or decreases map sizestatus - displays statusname - map name in [i]/dev/mapper/[device][/i] - [device] is the name of theHDD you want to encrypt[/pre]Now you know all the basic options, so you can get back to your main job — encrypt the home partition. [b]Cryptsetup[/b] is a console program and there is no GUI layer available, so if you don’t like working with command line tools… you will have to give it a second chance [img]http://polishlinux.org/wp-includes/images/smilies/icon_smile.gif[/img]
[size=4][b]Initializing of [i]/home[/i] directory[/b][/size]
For the purpose of this article, let’s say that the partition which we want to encrypt is [b]/dev/hdc6[/b]. Before you will be able to make the encrypted disc partition you have to initialize it first. This is how you do it:
[pre]root@host:~# cryptsetup luksFormat /dev/hdc6WARNING!=========This will overwrite data on /dev/hdc6 irrevocably.Are you sure? (Type uppercase yes): [YES]Enter LUKS passphrase:Verify passphrase:Command successful.[/pre]You will be prompted for a password. You need to enter it twice. This password will be used for verification later on, so don’t forget it!
[size=4][b]Choosing the encryption algorithm[/b][/size]
Then you have to decide which algorithm you will use to encrypt the [b]/home[/b] directory. The algorithm you use have to be supported by your kernel version. To check the available algorithms enter:
[pre]cat /proc/crypto[/pre]If there is no built-in crypto options (algorithms shouldn’t be loaded as modules) configure the kernel before you start the program (crypto options are in the [b]Cryptographic options[/b] tab of the kernel configuration).
Next, you type:
[pre]root@host:~# cryptsetup -c [i]chosen_algorithm[/i] -y luksFormat /dev/hdc6[/pre]If you don’t know what algorithm you want just skip [b]-c[/b] switch. The default algorithm is [url=http://pl.wikipedia.org/wiki/AES]AES[/url].
[size=4][b]Mapping the drive and creating a filesystem.[/b][/size]
The next step is to create the mapping. Type:
[pre]root@host:~# cryptsetup luksOpen /dev/hdc6[/pre]
You will be prompted to enter your password again to verify your identity. If there are no problems, the [b]/home[/b] directory map should appear in [b]/dev/mapper[/b].
A good idea is to fill the new drive with random data. This may be a lengthy operation, so only do this when you have a modern computer. Here is an example command to perform this action:
[pre]dd if=/dev/urandom of=/dev/mapper/home[/pre]Then you should create a filesystem on your virtual directory’s copy:
[pre]root@host:~# mkfs.ext2 -m 0 /dev/mapper/home[/pre]Here I’ve chosen [i]ext2[/i], but it can be any filesystem e.g. ext3 or reiserFS.
Now copy your current content of /home directory to the mapped drive:
[pre]root@host:~# cp -a /home /dev/mapper/home[/pre]
You are almost done, already [img]http://polishlinux.org/wp-includes/images/smilies/icon_smile.gif[/img]
[size=4][b]Creating additional user passwords[/b][/size]
[b]LUKS[/b] has a very interesting feature. Namely, not only [b]root[/b] can access the encrypted directory, but also the regular users can, which is very helpful when you want to use the encrypted drive as your home partition. To enable a non-root user the access to the directory you have to add some new passwords for those guys. To add those new passwords run [b]cryptsetup[/b] with option [b]luksAddKey[/b]:
[pre]root@host:~# cryptsetup luksAddKey /dev/hdc6Enter any LUKS passphrase:Verify passphrase:key slot 0 unlocked.Enter new passphrase for key slot:Verify passphrase:Command successful.[/pre]You will be prompted for your password which you have set at the beginning and then you enter the new password for the chosen user. Passwords can be the same as the user’s account password of course. This is handy since it doesn’t force users to remember another passphrase, but of course makes the system security a bit weaker because a hacker who knows the user’s password can easily see the content of the encrypted partition.
To list all the added keys/passwords type the following:
[pre]root@host:~# cryptsetup luksDump /dev/hdc6[/pre]To remove a password type:
[pre]root@host:~# cryptsetup luksDelKey [i]key_no[/i][/pre][size=4][b]Mounting the encrypted drive[/b][/size]
Finally you can mount your partition as a separate drive:
[pre]root@host:~# mount /dev/mapper/home /mnt/home/[/pre]And that’s all. From now on your [i]/home[/i] partition will be encrypted. But what about mounting after reboot? Well… then you will have to map the [i]/home[/i] directory again by typing:
[pre]root@host:~# cryptsetup -y luksOpen /dev/hdc6root@host:~# mount /dev/mapper/home /mnt/home[/pre]If this seems annoying, read on [img]http://polishlinux.org/wp-includes/images/smilies/icon_smile.gif[/img]
[size=4][b]Automatic mounting of encrypted drives[/b][/size]
As you may suspect, manual mounting can become annoying, especially in case you often reboot or turn off the computer. But there is a way to avoid it. To automate mounting you will need these two programs:
[list][li][url=http://en.wikipedia.org/wiki/Pluggable_Authentication_Modules]PAM[/url] — lets to combine a few crypto technologies, what’s why the user isn’t bothered by separate implementation of each key encrypted with different algorithm. [/li][li][url=http://pam-mount.sourceforge.net/]pam_mount[/url] — this is a special layer for PAM which enables of mounting separate volumes for each user and what is most important — it handles the [b]crypt[/b] as well. [/li][/list][b]Pam_mount[/b] works almost seamlessly because mounting is invisible for the user. Installation of [b]PAM[/b] and its plug-in proceeds as usual:
[pre]./configuremakemake install[/pre]When you have already installed both programs you have to check the content of [b]/etc/security/pam_mount.conf[/b] file. I won’t show whole the file because there is everything perfectly described inside. You have to add the following line at the end of this file:
[pre]volume * crypt - /dev/hdc6/home /mnt/home fstype=ext2,async,nodev,fsck - -[/pre]The general syntax looks like this:
[pre][b]volume[/b] username filesystem server volume mounting_point mounting_options \encryption_algorithm path_to_key[/pre]Next, you have to edit [b]/etc/pam.d/pam_mount[/b]. Check if there are following lines (if not, add them):
[pre]auth required pam_mount.so use_first_passsession required pam_mount.so use_first_pass[/pre]Eventually, open [b]/etc/pam.d/login[/b] and right after the line:
[pre]@include common-session[/pre]Add this one:
[pre]@include common-pammount[/pre]And here is the (happy) end of the story. The encrypted partition should now be automatically mounted upon login and you should not worry anymore about potential stealth of your data. I strongly advice you to keep and eye on your laptop anyway since the thief may not be aware of the advanced technologies you just installed [img]http://polishlinux.org/wp-includes/images/smilies/icon_smile.gif[/img]
[size=4][b]Conclusion[/b][/size]
Configuring the drive encryption in Linux isn’t as easy as it should be. That’s why if you don’t have to do it, better leave it alone. Be aware that if — during the encryption process — you are cut off the power supply and you have no UPS, the situation won’t be funny. This is because the encryption is being held on the fly. The files are encrypted before being written on HDD, so in such case you may lose the document you currently work on. But the advantages are also huge enough to consider such encryption. When your PC is stolen, the thieves won’t have any access to your encrypted data (in this case, the whole user partition). This is just a HOWTO, the decision is yours [img]http://polishlinux.org/wp-includes/images/smilies/icon_smile.gif[/img]
页:
[1]
