{"id":2886,"date":"2022-05-20T01:04:33","date_gmt":"2022-05-20T01:04:33","guid":{"rendered":"https:\/\/dft.wiki\/?p=2886"},"modified":"2024-11-20T18:57:32","modified_gmt":"2024-11-20T23:57:32","slug":"how-to-encrypt-partitions-in-linux","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=2886","title":{"rendered":"How to Encrypt Partitions with LUKS"},"content":{"rendered":"<p>Linux Unified Key Setup (LUKS) is an utility for disk encryption based on the DMCrypt kernel module.<\/p>\n<p>It is not recommended to encrypt a partition that already contains data because of the risk of data loss.<\/p>\n<p>The safer way is attaching a new volume to the system, encrypt the new volume and then copy the files over.<\/p>\n<p>Edit the partition table of the new volume:<\/p>\n<pre>sudo fdisk -l\r\nsudo fdisk \/dev\/sdb<\/pre>\n<p>Press the following keys:<\/p>\n<ul>\n<li><strong>p<\/strong>\n<ul>\n<li>Prints the existent partitions (should have none yet).<\/li>\n<\/ul>\n<\/li>\n<li><strong>n<\/strong>\n<ul>\n<li>Creates a new partition.\n<ul>\n<li><strong>p<\/strong>\n<ul>\n<li>Primary\n<ul>\n<li><strong>1<\/strong>\n<ul>\n<li>Partition number.\n<ul>\n<li><strong>&lt;Enter&gt;<\/strong><\/li>\n<li><strong>&lt;Enter&gt;<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li><strong>p<\/strong>\n<ul>\n<li>Prints the partition table with the newly created partition.<\/li>\n<\/ul>\n<\/li>\n<li><strong>w<\/strong>\n<ul>\n<li>Writes the changes to the disk.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Install Crypt Setup:<\/p>\n<pre>sudo apt-get install cryptsetup -y<\/pre>\n<p>Encrypt the partition:<\/p>\n<pre>sudo cryptsetup luksFormat \/dev\/sdb1<\/pre>\n<p>Type YES and give it a passphrase and save it securely in a key vault or password manager.<\/p>\n<p>Open (unlock) the partition and give it a meaningful name:<\/p>\n<pre>sudo cryptsetup luksOpen \/dev\/sdb1 partitionName<\/pre>\n<p>Inspect the partition with the following command:<\/p>\n<pre>lsblk<\/pre>\n<p>Format the new partition:<\/p>\n<pre>sudo mkfs.ext4 \/dev\/mapper\/partitionName<\/pre>\n<p>Create a mounting point and mount the partition on it:<\/p>\n<pre>sudo mkdir -p \/encryptedVolume\r\nsudo mount \/dev\/mapper\/partitionName \/encryptedVolume<\/pre>\n<hr \/>\n<p><strong>MOUNTING ON BOOT<\/strong><\/p>\n<pre>sudo nano \/etc\/crypttab<\/pre>\n<p>Find out the UUID of the new partition:<\/p>\n<pre>sudo blkid<\/pre>\n<p>Add the following line:<\/p>\n<pre># &lt;target name&gt; &lt;source device&gt; &lt;key file&gt; &lt;options&gt;\r\npartitionName UUID=7769dc40-66f1-4028-9fc4-1ac0178a080e none luks<\/pre>\n<p><strong>Note:<\/strong> for passphrase use <strong>none<\/strong> on the third column (if using a key, add the path to the key file instead). And <strong>luks<\/strong> is the mounting option.<\/p>\n<p>Now edit the<strong> fstab<\/strong>:<\/p>\n<pre>sudo nano \/etc\/fstab<\/pre>\n<p>Append a line:<\/p>\n<pre>UUID=7769dc40-66f1-4028-9fc4-1ac0178a080e \/encryptedVolume ext4 defaults 0 0<\/pre>\n<p><strong>Note:<\/strong> during the boot will be prompted for the passphrase. Not all distributions will deal very well and might not prompt for the password on the boot.<\/p>\n<p>Alternatively, create a key file for the volume by issuing:<\/p>\n<pre>sudo cryptsetup luksAddKey \/dev\/sdb1 \/boot\/volume.key<\/pre>\n<p>Check if the key was added correctly:<\/p>\n<pre>sudo cryptsetup luksDump \/dev\/sdb1<\/pre>\n<p>Then, on the<strong> \/etc\/crypttab<\/strong> enter the path to the key:<\/p>\n<pre># &lt;target name&gt; &lt;source device&gt; &lt;key file&gt; &lt;options&gt;\r\npartitionName UUID=7769dc40-66f1-4028-9fc4-1ac0178a080e \/boot\/volume.key luks<\/pre>\n<p><strong>BONUS<\/strong><\/p>\n<p>1 &#8211; If the header of the drive gets corrupted there is no way the rest of the drive can be decrypted. So back it up:<\/p>\n<pre>cryptsetup luksHeaderBackup \/dev\/sdb1 --header-backup-file LUKS-Header.bin<\/pre>\n<p>2 &#8211; To restore use:<\/p>\n<pre>cryptsetup luksHeaderRestore \/dev\/sdb1 --header-backup-file LUKS-Header.bin<\/pre>\n<p>3 &#8211; Use the key TAB to see the available commands:<\/p>\n<pre>cryptsetup &lt;TAB&gt;<\/pre>\n<p>4 &#8211; Change the password:<\/p>\n<pre>cryptsetup luksChangeKey \/dev\/sdb1<\/pre>\n<p>5 &#8211; Add multiple keys:<\/p>\n<pre>cryptsetup luksAddKey \/dev\/sdb1<\/pre>\n<hr \/>\n<p><strong>BONUS<\/strong><\/p>\n<p>Encrypt drives, partitions, or create a container of encrypted files with VeraCrypt [<a href=\"https:\/\/www.veracrypt.fr\/code\/VeraCrypt\/\">Link<\/a>]. This open-source app is compatible with Windows, Linux, and MacOS. Additionally, it can run on ARM devices, such as Raspberry Pis.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux Unified Key Setup (LUKS) is an utility for disk encryption based on the DMCrypt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-2886","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2886"}],"version-history":[{"count":6,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2886\/revisions"}],"predecessor-version":[{"id":4472,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2886\/revisions\/4472"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}