{"id":2600,"date":"2016-02-20T02:16:22","date_gmt":"2016-02-19T20:46:22","guid":{"rendered":"https:\/\/www.suramya.com\/blog\/?p=2600"},"modified":"2022-06-16T15:17:02","modified_gmt":"2022-06-16T09:47:02","slug":"how-to-encrypt-your-hard-drive-in-linux","status":"publish","type":"post","link":"https:\/\/www.suramya.com\/blog\/2016\/02\/how-to-encrypt-your-hard-drive-in-linux\/","title":{"rendered":"How to encrypt your Hard-drive in Linux"},"content":{"rendered":"<p>We have heard multiple stories where someone looses a pendrive or a laptop containing sensitive\/private data which is then published by the person who found the drive embarrassing the owner of the data. The best way to prevent something like that from happening to you if you loose a disk is to make sure all your data is encrypted. Historically this used to be quite painful to setup and required a lost of technical know-how. Thankfully this is no longer the case. After trying a bunch of different options I found Linux Unified Key Setup-on-disk-format (LUKS) to be the most user-friendly and easy to setup option for me. <\/p>\n<p>Setting it up is quite easy by following the instructions over at <a href='http:\/\/www.cyberciti.biz\/hardware\/howto-linux-hard-disk-encryption-with-luks-cryptsetup-command\/'>www.cyberciti.biz<\/a>. However since things on the internet have a tendency of disappearing on a fairly frequent basis, I am using this post to save a paraphrased version of the installation instructions (along with my notes\/comments) just in case the original site goes down and I need to reinstall. All credit goes to original author. So without further ado here we go:<\/p>\n<p><b>Install cryptsetup<\/b><\/p>\n<p>First we need to install cryptsetup utility which contains all the utilities we need to encrypt our drive. To install it in Debian\/Ubuntu you just issue the following command as root:<\/p>\n<pre class='code'>apt-get install cryptsetup<\/pre>\n<p><b>Configure LUKS partition<\/b><\/p>\n<blockquote><p>Warning: This will remove all data on the partition that you are encrypting. So make sure you have a working backup before proceeding amd don&#8217;t blame me if you manage to destroy your data\/device.<\/p><\/blockquote>\n<p>Run the following command as root to start the encryption process:<\/p>\n<pre class='code'>cryptsetup -y -v luksFormat &lt;device><\/pre>\n<p>where &lt;device> is the partition we want to encrypt (e.g. \/dev\/sda1). The command will ask you for confirmation and a passphrase. This passphrase is not recoverable so make sure you don&#8217;t forget it. <\/p>\n<p><b>Create drive mapping<\/b><\/p>\n<p>Once the previous command completes you need to create a mapping of the encrypted drive by issuing the following command:<\/p>\n<pre class='code'>cryptsetup luksOpen &lt;device> backup2<\/pre>\n<p>You can also map a  partition to using its UUID (which is what I do) by issuing the following command instead (This works great if you want to script automated backups to an external drive):<\/p>\n<pre class='code'>cryptsetup luksOpen UUID=88848060-fab7-4e9e-bac2-f9a2323c7c29 backup2<\/pre>\n<p>Replace the UUID in the example with the UUID of your drive. (Instructions on how to find the UUID are available <a href='https:\/\/help.ubuntu.com\/community\/UsingUUID#Finding_UUIDs'>here<\/a>).<\/p>\n<p>Use the following command to see the status for the mapping and to check if the command succeeded:<\/p>\n<pre class='code'>cryptsetup -v status backup2<\/pre>\n<p><b>Format LUKS partition<\/b><\/p>\n<p>Now that we have created the mapping we need to write zeroes to the encrypted device, to ensure that the outside world sees this as random data and protects the system against disclosure of usage by issuing the following command:<\/p>\n<pre class='code'>dd if=\/dev\/zero of=\/dev\/mapper\/backup2<\/pre>\n<p>Since this command can take a long time to complete depending on the drive size and dd by default doesn&#8217;t give any feedback on the percentage completed\/remaining I recommend that you use the pv command to monitor the progress by issuing the following command instead:<\/p>\n<pre class='code'>pv -tpreb \/dev\/zero | dd of=\/dev\/mapper\/backup2 bs=128M<\/pre>\n<p>This will take a while to run so you can go for a walk or read a book while it runs. Once the command completes you can create a filesystem on the device (I prefer to use ext4 but you can use any filesystem you like) by formatting the device:<\/p>\n<pre class='code'>mkfs.ext4 \/dev\/mapper\/backup2<\/pre>\n<p>After the filesystem is created you can mount and use the partition as usual by issuing the following command:<\/p>\n<pre class='code'>mount \/dev\/mapper\/backup2 \/mnt\/backup<\/pre>\n<p>That&#8217;s it. You now have an encrypted partition that shows up as a regular partition in Linux which you can use as a regular drive without having to worry about anything. No special changes are needed to use this partition which means any software can use it without requiring changes. <\/p>\n<p><b>How to unmount and secure the data<\/b><\/p>\n<p>After you are done transferring data to\/from the drive you can unmount and secure the partition by issuing the following commands as root:<\/p>\n<pre class='code'>umount \/mnt\/backup<\/pre>\n<p>followed by<\/p>\n<pre class='code'>cryptsetup luksClose backup2<\/pre>\n<p><b>Creating a backup of the LUKS headers<\/b><\/p>\n<p>Before you start anything else, you should create a backup copy of the LUKS header because if this header gets corrupted somehow then all data in the encrypted partition is lost forever with no way to recover it. From the cryptsetup man page:<\/p>\n<blockquote><p>\u00e2\u20ac\u0153LUKS header: If the header of a LUKS volume gets damaged, all data is permanently lost unless you have a header-backup. If a key-slot is damaged, it can only be restored from a header-backup or if another active key-slot with known passphrase is undamaged. Damaging the LUKS header is something people manage to do with surprising frequency. This risk is the result of a trade-off between security and safety, as LUKS is designed for fast and secure wiping by just overwriting header and key-slot area.\u00e2\u20ac\u009d<\/p><\/blockquote>\n<p>Create a backup by issuing the following command:<\/p>\n<pre class='code'>cryptsetup luksHeaderBackup &lt;device> --header-backup-file &lt;file><\/pre>\n<p>Important note: a LUKS header backup can grant access to most or all data, therefore you need to make sure that nobody has access to it. <\/p>\n<p>In case of disaster where our LUKS header gets broken, we can restore it by issuing the following command:<\/p>\n<pre class='code'>cryptsetup luksHeaderRestore &lt;device> --header-backup-file &lt;file><\/pre>\n<p><b>How to remount the encrypted partition?<\/b><\/p>\n<p>Issue the following commands in sequence to mount the partition:<\/p>\n<pre class='code'>cryptsetup luksOpen &lt;device> backup2\r\nmount \/dev\/mapper\/backup2 \/mnt\/backup\r\n<\/pre>\n<hr \/>\n<p>Please note that data encrypted by LUKS is quite obvious with most Linux systems identifying it as an encrypted partition automatically. So if someone examines your system they will know you have encrypted data and can force you to divulge the password by various means (including the use of <a href='https:\/\/en.wikipedia.org\/wiki\/Rubber-hose_cryptanalysis'>Rubber-hose Cryptanalysis<\/a>. ) <\/p>\n<p>If you want the encrypted partition to be hidden then you can use <a href='https:\/\/en.wikipedia.org\/wiki\/Deniable_encryption'>Deniable encryption<\/a>\/<a href='http:\/\/www.csoonline.com\/article\/2130782\/investigations-forensics\/three-steps-to-properly-protect-your-personal-data.html'>Hidden Partition<\/a> or use steganography. I haven&#8217;t really used either so can&#8217;t comment on how to set it up correctly but maybe I can talk about it in a future post after I explore them a bit more.<\/p>\n<p>Well this is all for now, hope you find this useful. Will write more later.<\/p>\n<p>&#8211; Suramya<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have heard multiple stories where someone looses a pendrive or a laptop containing sensitive\/private data which is then published by the person who found the drive embarrassing the owner of the data. The best way to prevent something like that from happening to you if you loose a disk is to make sure all [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[19,24,13,17,2],"tags":[],"class_list":["post-2600","post","type-post","status-publish","format-standard","hentry","category-computer-security","category-knowledgebase","category-linuxunix-related","category-security-tutorials","category-techie-stuff"],"_links":{"self":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/2600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/comments?post=2600"}],"version-history":[{"count":12,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/2600\/revisions"}],"predecessor-version":[{"id":2612,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/2600\/revisions\/2612"}],"wp:attachment":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/media?parent=2600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/categories?post=2600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/tags?post=2600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}