{"id":997,"date":"2025-02-21T09:30:18","date_gmt":"2025-02-21T06:30:18","guid":{"rendered":"https:\/\/itgen.itbumper.com\/?page_id=997"},"modified":"2025-02-26T10:16:37","modified_gmt":"2025-02-26T07:16:37","slug":"0039_creating-a-luks-encrypted-disk-with-auto-mounting-key-file","status":"publish","type":"page","link":"https:\/\/itgen.itbumper.com\/?page_id=997","title":{"rendered":"0039_Creating a LUKS-encrypted disk with auto-mounting (key file)"},"content":{"rendered":"<p style=\"text-align: justify;\" data-tadv-p=\"keep\">Let&#8217;s say we added a 20G disk that needs to be encrypted and configured so that it is automatically mounted by the system using a key file. (it is assumed that the system is also located on an encrypted disk)<br>1. Check the list of disks (<em>we are interested in the <strong>vda<\/strong> disk<\/em>): <\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nlsblk -e7\u00a0\n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"775\" height=\"250\" src=\"https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_027.png\" alt=\"\" class=\"wp-image-998\" srcset=\"https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_027.png 775w, https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_027-300x97.png 300w, https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_027-768x248.png 768w\" sizes=\"(max-width: 775px) 85vw, 775px\" \/><\/figure><\/div>\n\n<div style=\"text-align: justify;\">2. When marking up the disk, the partition table should be marked up as GPT, and one primary partition, which occupies the entire disk, should be created.<\/div>\n<div>&nbsp;<\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo parted \/dev\/vda\nmklabel gpt\nmkpart primary 1 100%\n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"722\" height=\"206\" src=\"https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_028.png\" alt=\"\" class=\"wp-image-999\" srcset=\"https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_028.png 722w, https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_028-300x86.png 300w\" sizes=\"(max-width: 722px) 85vw, 722px\" \/><\/figure><\/div>\n\n<p style=\"text-align: justify;\" data-tadv-p=\"keep\">3. Generate a 2048-bit key<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo dd\u00a0 if=\/dev\/urandom of=\/root\/secret.key bs=1024 count=2\n<\/pre><\/div>\n\n<p>4. Change the file&#8217;s read-only permissions to the owner:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo chmod 0400 \/root\/secret.key\n<\/pre><\/div>\n\n<p>5. Create a LUKS partition using the created key:<\/p>\n<p><em>A warning about data destruction will appear. Enter YES in capital letters.<\/em><\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo cryptsetup luksFormat \/dev\/vda1 \/root\/secret.key\n<\/pre><\/div>\n\n<hr>\n<p><strong>Important:<\/strong><\/p>\n<p><em>When third parties try to access your disk, they need to have a key <\/em><em>to decrypt the data on the disk. Don&#8217;t tell anyone the path to the key.<\/em><\/p>\n<hr>\n<p>6. Before using a LUKS partition, you must display and format it correctly. To do this, first use the luksOpen option, which creates an I\/O device that allows you to interact with the partition:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo cryptsetup luksOpen \/dev\/vda1 secret\u00a0 --key-file=\/root\/secret.key\n<\/pre><\/div>\n\n<p>The LUKS I\/O device is now available in <em><strong>\/dev\/mapper\/secret<\/strong><\/em>.<\/p>\n<p>7. Next, specify the size of the LUKS partition (<em>the maximum size will be used without parameters<\/em>), or if it starts asking for a password, use this command<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo cryptsetup resize secret --key-file=\/root\/secret.key\n<\/pre><\/div>\n\n<p>8. Making the file system ext4&nbsp;<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo mkfs.ext4 \/dev\/mapper\/secret\n<\/pre><\/div>\n\n<p>9. Check the status and key slots<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo cryptsetup -v status secret\nsudo cryptsetup luksDump \/dev\/vda1\n<\/pre><\/div>\n\n<p>10. Mounting the LUKS partition:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo mkdir -p \/secret\nsudo chmod 755 \/secret\nsudo mount \/dev\/mapper\/secret \/secret\ndf -h\n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"747\" height=\"645\" src=\"https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_029.png\" alt=\"\" class=\"wp-image-1001\" srcset=\"https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_029.png 747w, https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_029-300x259.png 300w\" sizes=\"(max-width: 747px) 85vw, 747px\" \/><\/figure><\/div>\n\n<hr>\n<p><span style=\"text-decoration: underline;\"><strong>Automatic mounting<\/strong><\/span><\/p>\n<p>1. First, find out the UUID for the encrypted partition:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo ls -l \/dev\/disk\/by-uuid\nlsblk\n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"746\" height=\"381\" src=\"https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_030.png\" alt=\"\" class=\"wp-image-1002\" srcset=\"https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_030.png 746w, https:\/\/itgen.itbumper.com\/wp-content\/uploads\/2025\/02\/crypto_030-300x153.png 300w\" sizes=\"(max-width: 746px) 85vw, 746px\" \/><\/figure><\/div>\n\n<p>2. Change your account to<strong> root<\/strong><br>3. Export the variable (your value will be different):<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nexport UUID=&quot;619b3901-94cd-4595-9a4c-ce3fdfad0e6f&quot;\n<\/pre><\/div>\n\n<p>4. Add the key link to the <em><strong>\/etc\/crypttab<\/strong><\/em> file:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\necho &quot;secret UUID=${UUID} \/root\/secret.key luks&quot; &gt;&gt; \/etc\/crypttab\n<\/pre><\/div>\n\n<p>5. Finally, create an entry in the <em><strong>\/etc\/fstab<\/strong><\/em> file for automatic mounting:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\necho &quot;\/dev\/mapper\/secret\/secret auto&quot; &gt;&gt; \/etc\/fstab\n<\/pre><\/div>\n\n<p>6. Mount<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nmount -a\n<\/pre><\/div>\n\n<p>7. Reboot and check<\/p>","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say we added a 20G disk that needs to be encrypted and configured so that it is automatically mounted by the system using a key file. (it is assumed that the system is also located on an encrypted disk)1. Check the list of disks (we are interested in the vda disk): 2. When marking &hellip; <a href=\"https:\/\/itgen.itbumper.com\/?page_id=997\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;0039_Creating a LUKS-encrypted disk with auto-mounting (key file)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages\/997"}],"collection":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=997"}],"version-history":[{"count":8,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages\/997\/revisions"}],"predecessor-version":[{"id":1021,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages\/997\/revisions\/1021"}],"wp:attachment":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}