{"id":2886,"date":"2022-05-20T01:04:33","date_gmt":"2022-05-20T01:04:33","guid":{"rendered":"https:\/\/dft.wiki\/?p=2886"},"modified":"2026-06-08T21:47:55","modified_gmt":"2026-06-09T01:47:55","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 a 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 due to the risk of data loss.<\/p>\n<p>The safer approach is to attach a new volume to the system, encrypt it, 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 existing 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 cryptsetup:<\/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, set 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:<\/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 mount point and mount the partition:<\/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 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 a passphrase, use <strong>none<\/strong> in the third column. If using a key file, add its path instead. <strong>luks<\/strong> is the mount option.<\/p>\n<p>Now edit <strong>fstab<\/strong>:<\/p>\n<pre>sudo nano \/etc\/fstab<\/pre>\n<p>Append the following line:<\/p>\n<pre>UUID=7769dc40-66f1-4028-9fc4-1ac0178a080e \/encryptedVolume ext4 defaults 0 0<\/pre>\n<p><strong>Note:<\/strong> During boot you will be prompted for the passphrase. Not all distributions handle this well and may not prompt for it correctly.<\/p>\n<p>Alternatively, create a key file for the volume:<\/p>\n<pre>sudo cryptsetup luksAddKey \/dev\/sdb1 \/boot\/volume.key<\/pre>\n<p>Verify the key was added correctly:<\/p>\n<pre>sudo cryptsetup luksDump \/dev\/sdb1<\/pre>\n<p>Then, in <strong>\/etc\/crypttab<\/strong>, enter the path to the key file:<\/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 drive header gets corrupted, the rest of the drive cannot be decrypted. Back it up with:<\/p>\n<pre>cryptsetup luksHeaderBackup \/dev\/sdb1 --header-backup-file LUKS-Header.bin<\/pre>\n<p>2 &#8211; To restore the header:<\/p>\n<pre>cryptsetup luksHeaderRestore \/dev\/sdb1 --header-backup-file LUKS-Header.bin<\/pre>\n<p>3 &#8211; Press TAB to see all available commands:<\/p>\n<pre>cryptsetup &lt;TAB&gt;<\/pre>\n<p>4 &#8211; Change the passphrase:<\/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, and also runs on ARM devices such as Raspberry Pis.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux Unified Key Setup (LUKS) is a 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":7,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2886\/revisions"}],"predecessor-version":[{"id":5678,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2886\/revisions\/5678"}],"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}]}}