{"id":2369,"date":"2018-08-24T15:40:56","date_gmt":"2018-08-24T19:40:56","guid":{"rendered":"https:\/\/easy-admin.ca\/?p=2369"},"modified":"2020-08-30T15:55:06","modified_gmt":"2020-08-30T19:55:06","slug":"cryptseup","status":"publish","type":"post","link":"https:\/\/easy-admin.ca\/index.php\/2018\/08\/24\/cryptseup\/","title":{"rendered":"Cryptsetup on Centos 7 Server"},"content":{"rendered":"<h2>Linux encryption methods<\/h2>\n<p>There are two methods to encrypt your data:<\/p>\n<h3>#1: Filesystem stacked level encryption<\/h3>\n<ol>\n<li><a href=\"https:\/\/launchpad.net\/ecryptfs\" target=\"_blank\" rel=\"noopener noreferrer\">eCryptfs<\/a> \u2013 It is a cryptographic stacked Linux filesystem. eCryptfs stores cryptographic metadata in the header of each file written, so that encrypted files can be copied between hosts; the file will be decrypted with the proper key in the Linux kernel keyring. This solution is widely used, as the basis for Ubuntu\u2019s Encrypted Home Directory, natively within Google\u2019s ChromeOS, and transparently embedded in several network attached storage (NAS) devices.<\/li>\n<li><a href=\"http:\/\/www.arg0.net\/encfs\" target=\"_blank\" rel=\"noopener noreferrer\">EncFS<\/a> -It provides an encrypted filesystem in user-space. It runs without any special permissions and uses the FUSE library and Linux kernel module to provide the filesystem interface. You can find links to source and binary releases below. EncFS is open source software, licensed under the GPL.<\/li>\n<\/ol>\n<h3>#2: Block device level encryption<\/h3>\n<ol>\n<li><a href=\"https:\/\/sourceforge.net\/projects\/loop-aes\/\" target=\"_blank\" rel=\"noopener noreferrer\">Loop-AES<\/a> \u2013 Fast and transparent file system and swap encryption package for linux. No source code changes to linux kernel. Works with 3.x, 2.6, 2.4, 2.2 and 2.0 kernels.<\/li>\n<li><a href=\"https:\/\/www.veracrypt.fr\/\" target=\"_blank\" rel=\"noopener noreferrer\">VeraCrypt<\/a> \u2013 It is free open-source disk encryption software for Windows 7\/Vista\/XP, Mac OS X and Linux based on TrueCrypt codebase.<\/li>\n<li><a href=\"https:\/\/gitlab.com\/cryptsetup\/cryptsetup\" target=\"_blank\" rel=\"noopener noreferrer\">dm-crypt+LUKS<\/a> \u2013 dm-crypt is a transparent disk encryption subsystem in Linux kernel v2.6+ and later and DragonFly BSD. It can encrypt whole disks, removable media, partitions, software RAID volumes, logical volumes, and files.<\/li>\n<\/ol>\n<p>In this post, I will explain how to encrypt your partitions using Linux Unified Key Setup-on-disk-format (LUKS) on your Linux based computer or laptop.<\/p>\n<h2>Step #1: Install cryptsetup utility<\/h2>\n<p>You need to install the following package. It contains cryptsetup, a utility for setting up encrypted filesystems using Device Mapper and the dm-crypt target. Debian \/ Ubuntu Linux user type the following <a href=\"https:\/\/www.cyberciti.biz\/tips\/linux-debian-package-management-cheat-sheet.html\">apt-get command<\/a> or <a title=\"See Linux\/Unix apt command examples for more info\" href=\"https:\/\/www.cyberciti.biz\/faq\/ubuntu-lts-debian-linux-apt-command-examples\/\">apt command<\/a>:<br \/>\n<code># apt-get install cryptsetup<\/code><br \/>\nOR<br \/>\n<code>$ sudo apt install cryptsetup<\/code><br \/>\nSample outputs:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"ini\">Reading package lists... Done\r\nBuilding dependency tree       \r\nReading state information... Done\r\nThe following additional packages will be installed:\r\n  console-setup console-setup-linux cryptsetup-bin kbd keyboard-configuration xkb-data\r\nSuggested packages:\r\n  dosfstools keyutils\r\nThe following NEW packages will be installed:\r\n  console-setup console-setup-linux cryptsetup cryptsetup-bin kbd keyboard-configuration xkb-data\r\n0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.\r\nNeed to get 3,130 kB of archives.\r\nAfter this operation, 13.2 MB of additional disk space will be used.\r\nDo you want to continue? [Y\/n] y\r\nGet:1 http:\/\/deb.debian.org\/debian stretch\/main amd64 kbd amd64 2.0.3-2+b1 [343 kB]\r\nGet:2 http:\/\/deb.debian.org\/debian stretch\/main amd64 keyboard-configuration all 1.164 [644 kB]\r\nGet:3 http:\/\/deb.debian.org\/debian stretch\/main amd64 console-setup-linux all 1.164 [983 kB]\r\nGet:4 http:\/\/deb.debian.org\/debian stretch\/main amd64 xkb-data all 2.19-1 [648 kB]\r\nGet:5 http:\/\/deb.debian.org\/debian stretch\/main amd64 console-setup all 1.164 [117 kB]\r\nGet:6 http:\/\/deb.debian.org\/debian stretch\/main amd64 cryptsetup-bin amd64 2:1.7.3-4 [221 kB]\r\nGet:7 http:\/\/deb.debian.org\/debian stretch\/main amd64 cryptsetup amd64 2:1.7.3-4 [174 kB]\r\nFetched 3,130 kB in 0s (7,803 kB\/s)\r\nPreconfiguring packages ...\r\nSelecting previously unselected package kbd.\r\n(Reading database ... 22194 files and directories currently installed.)\r\nPreparing to unpack ...\/0-kbd_2.0.3-2+b1_amd64.deb ...\r\nUnpacking kbd (2.0.3-2+b1) ...\r\nSelecting previously unselected package keyboard-configuration.\r\nPreparing to unpack ...\/1-keyboard-configuration_1.164_all.deb ...\r\nUnpacking keyboard-configuration (1.164) ...\r\nSelecting previously unselected package console-setup-linux.\r\nPreparing to unpack ...\/2-console-setup-linux_1.164_all.deb ...\r\nUnpacking console-setup-linux (1.164) ...\r\nSelecting previously unselected package xkb-data.\r\nPreparing to unpack ...\/3-xkb-data_2.19-1_all.deb ...\r\nUnpacking xkb-data (2.19-1) ...\r\nSelecting previously unselected package console-setup.\r\nPreparing to unpack ...\/4-console-setup_1.164_all.deb ...\r\nUnpacking console-setup (1.164) ...\r\nSelecting previously unselected package cryptsetup-bin.\r\nPreparing to unpack ...\/5-cryptsetup-bin_2%3a1.7.3-4_amd64.deb ...\r\nUnpacking cryptsetup-bin (2:1.7.3-4) ...\r\nSelecting previously unselected package cryptsetup.\r\nPreparing to unpack ...\/6-cryptsetup_2%3a1.7.3-4_amd64.deb ...\r\nUnpacking cryptsetup (2:1.7.3-4) ...\r\nSetting up keyboard-configuration (1.164) ...\r\nSetting up xkb-data (2.19-1) ...\r\nSetting up kbd (2.0.3-2+b1) ...\r\nProcessing triggers for systemd (232-25+deb9u1) ...\r\nSetting up cryptsetup-bin (2:1.7.3-4) ...\r\nProcessing triggers for man-db (2.7.6.1-2) ...\r\nSetting up console-setup-linux (1.164) ...\r\nCreated symlink \/etc\/systemd\/system\/sysinit.target.wants\/keyboard-setup.service \u2192 \/lib\/systemd\/system\/keyboard-setup.service.\r\nCreated symlink \/etc\/systemd\/system\/multi-user.target.wants\/console-setup.service \u2192 \/lib\/systemd\/system\/console-setup.service.\r\nSetting up console-setup (1.164) ...\r\nSetting up cryptsetup (2:1.7.3-4) ...\r\nupdate-initramfs: deferring update (trigger activated)\r\nProcessing triggers for systemd (232-25+deb9u1) ...\r\nProcessing triggers for initramfs-tools (0.130) ...\r\nupdate-initramfs: Generating \/boot\/initrd.img-4.9.0-3-amd64<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>RHEL \/ CentOS \/ Oracle \/ Scientific Linux user type the following <a href=\"https:\/\/www.cyberciti.biz\/faq\/rhel-centos-fedora-linux-yum-command-howto\/\">yum command<\/a>:<br \/>\n<code># yum install cryptsetup-luks<\/code><br \/>\nOR Fedora Linux user use the dnf command:<br \/>\n<code># dnf install cryptsetup-luks<\/code><\/p>\n<h2>Step #2: Configure LUKS partition<\/h2>\n<div><img decoding=\"async\" title=\"Warning examples may crash your computer and data\" src=\"https:\/\/s0.cyberciti.org\/images\/misc\/warning-40px.png\" \/><strong>WARNING!<\/strong> The following command will remove all data on the partition that you are encrypting. You WILL lose all your information! So make sure you backup your data to an external source such as NAS or hard disk before typing any one of the following command.<\/div>\n<p>In this example, I\u2019m going to encrpt \/dev\/xvdc. Type the following command:<br \/>\n<code># cryptsetup -y -v luksFormat \/dev\/xvdc<\/code><br \/>\nSample outputs:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"ini\">WARNING!\r\n========\r\nThis will overwrite data on \/dev\/xvdc irrevocably.\r\n\u00a0\r\nAre you sure? (Type uppercase yes): YES\r\nEnter LUKS passphrase: \r\nVerify passphrase: \r\nCommand successful.<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>This command initializes the volume, and sets an initial key or passphrase. Please note that the passphrase is not recoverable so do not forget it.Type the following command create a mapping:<br \/>\n<code># cryptsetup luksOpen \/dev\/xvdc backup2<\/code><br \/>\nSample outputs:<\/p>\n<pre>Enter passphrase for \/dev\/xvdc:<\/pre>\n<p>You can see a mapping name \/dev\/mapper\/backup2 after successful verification of the supplied key material which was created with luksFormat command extension:<br \/>\n<code># ls -l \/dev\/mapper\/backup2 <\/code><br \/>\nSample outputs:<\/p>\n<pre>lrwxrwxrwx 1 root root 7 Oct 19 19:37 \/dev\/mapper\/backup2 -&gt; ..\/dm-0<\/pre>\n<p>You can use the following command to see the status for the mapping:<br \/>\n<code># cryptsetup -v status backup2<\/code><br \/>\nSample outputs:<\/p>\n<pre>\/dev\/mapper\/backup2 is active.\r\n  type:    LUKS1\r\n  cipher:  aes-cbc-essiv:sha256\r\n  keysize: 256 bits\r\n  device:  \/dev\/xvdc\r\n  offset:  4096 sectors\r\n  size:    419426304 sectors\r\n  mode:    read\/write\r\nCommand successful.\r\n<\/pre>\n<p>You can dump LUKS headers using the following command:<br \/>\n<code># cryptsetup luksDump \/dev\/xvdc<\/code><br \/>\nSample outputs:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"ini\">LUKS header information for \/dev\/xvdc\r\n\u00a0\r\nVersion:       \t1\r\nCipher name:   \taes\r\nCipher mode:   \txts-plain64\r\nHash spec:     \tsha256\r\nPayload offset:\t4096\r\nMK bits:       \t256\r\nMK digest:     \t21 07 68 54 77 96 11 34 f2 ec 17 e9 85 8a 12 c3 1f 3e cf 5f \r\nMK salt:       \t8c a6 3d 8c e9 de 16 fb 07 fd 8e d3 72 d7 db 94 \r\n               \t7e e0 75 f9 e0 23 24 df 50 26 fb 92 f8 b5 dd 70 \r\nMK iterations: \t222000\r\nUUID:          \t4dd563a9-5bff-4fea-b51d-b4124f7185d1\r\n\u00a0\r\nKey Slot 0: ENABLED\r\n\tIterations:         \t2245613\r\n\tSalt:               \t05 a8 b4 a2 54 f7 c6 ee 52 db 60 b6 12 7f 2f 53 \r\n\t                      \t3f 5d 2d 62 fb 5a b1 c3 52 da d5 5f 7b 2d 38 32 \r\n\tKey material offset:\t8\r\n\tAF stripes:            \t4000\r\nKey Slot 1: DISABLED\r\nKey Slot 2: DISABLED\r\nKey Slot 3: DISABLED\r\nKey Slot 4: DISABLED\r\nKey Slot 5: DISABLED\r\nKey Slot 6: DISABLED\r\nKey Slot 7: DISABLED<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>Step #3: Format LUKS partition<\/h2>\n<p>First, you need to write zeros to \/dev\/mapper\/backup2 encrypted device. This will allocate block data with zeros. This ensures that outside world will see this as random data i.e. it protect against disclosure of usage patterns:<br \/>\n<code># dd if=\/dev\/zero of=\/dev\/mapper\/backup2<\/code><br \/>\nThe dd command may take many hours to complete. I suggest that you use <a href=\"https:\/\/www.cyberciti.biz\/open-source\/command-line-hacks\/pv-command-examples\/\">pv command to monitor the progress<\/a>:<br \/>\n<code># pv -tpreb \/dev\/zero | dd of=\/dev\/mapper\/backup2 bs=128M<\/code><br \/>\nSample outputs:<\/p>\n<pre>dd: error writing '\/dev\/mapper\/backup2': No space left on device                                                                                                            ]\r\n 200GiB 0:16:47 [ 203MiB\/s] [                      &lt;=&gt;                                                                                                                      ]\r\n1600+1 records in\r\n1599+1 records out\r\n214746267648 bytes (215 GB, 200 GiB) copied, 1008.19 s, 213 MB\/s<\/pre>\n<p>You can <a title=\"Linux dd Command Show Progress Copy Bar With Status\" href=\"https:\/\/www.cyberciti.biz\/faq\/linux-unix-dd-command-show-progress-while-coping\/\">also pass the status=progress option to the dd command<\/a>:<br \/>\n<code># dd if=\/dev\/zero of=\/dev\/mapper\/backup2 status=progress<\/code><br \/>\nSample outputs:<\/p>\n<pre>2133934592 bytes (2.1 GB, 2.0 GiB) copied, 142 s, 15.0 MB\/s<\/pre>\n<p>Next, <a href=\"https:\/\/www.cyberciti.biz\/faq\/linux-disk-format\/\">create a filesystem<\/a> i.e. format filesystem, enter:<br \/>\n<code># mkfs.ext4 \/dev\/mapper\/backup2<\/code><br \/>\nSample outputs:<\/p>\n<pre>mke2fs 1.42.13 (17-May-2015)\r\nCreating filesystem with 52428288 4k blocks and 13107200 inodes\r\nFilesystem UUID: 1c71b0f4-f95d-46d6-93e0-cbd19cb95edb\r\nSuperblock backups stored on blocks: \r\n\t32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, \r\n\t4096000, 7962624, 11239424, 20480000, 23887872\r\n\r\nAllocating group tables: done                            \r\nWriting inode tables: done                            \r\nCreating journal (32768 blocks): done\r\nWriting superblocks and filesystem accounting information: done<\/pre>\n<p>To <a href=\"https:\/\/www.cyberciti.biz\/faq\/mount-command-in-unix\/\">mount the new filesystem at \/backup2<\/a>, enter:<br \/>\n<code># mkdir \/backup2<br \/>\n# mount \/dev\/mapper\/backup2 \/backup2<br \/>\n# df -H<br \/>\n# cd \/backup2<br \/>\n# ls -l<\/code><\/p>\n<h2>How do I unmount and secure data?<\/h2>\n<p>Type the following commands:<br \/>\n<code># <a href=\"https:\/\/www.cyberciti.biz\/faq\/tag\/umount-command\/\">umount<\/a> \/backup2<br \/>\n# cryptsetup luksClose backup2<\/code><\/p>\n<h2>How do I mount or remount encrypted partition?<\/h2>\n<p>Type the following command:<br \/>\n<code># cryptsetup luksOpen \/dev\/xvdc backup2<br \/>\n# mount \/dev\/mapper\/backup2 \/backup2<br \/>\n# df -H<br \/>\n# mount<\/code><br \/>\nSample outputs:<\/p>\n<figure id=\"attachment_693\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-693\" src=\"https:\/\/www.cyberciti.biz\/media\/new\/cms\/2012\/10\/df-ls-outputs.jpg\" sizes=\"auto, (max-width: 599px) 85vw, 599px\" srcset=\"https:\/\/www.cyberciti.biz\/media\/new\/cms\/2012\/10\/df-ls-outputs.jpg 599w, https:\/\/www.cyberciti.biz\/media\/new\/cms\/2012\/10\/df-ls-outputs-300x168.jpg 300w\" alt=\"Fig.01: Encrypted partition mounted on \/backup2\" width=\"599\" height=\"336\" \/><figcaption class=\"wp-caption-text\">Fig.01: Encrypted partition mounted on \/backup2<\/figcaption><\/figure>\n<p>See <a href=\"https:\/\/bash.cyberciti.biz\/security\/linux-opens-luks-partition-mount-dm-crypt-partition\/\">shell script wrapper that opens LUKS partition<\/a> and sets up a mapping for nas devices.<\/p>\n<h2>Can I run fsck on LUKS based partition \/ LVM volume?<\/h2>\n<p>Yes, you can use the <a href=\"https:\/\/www.cyberciti.biz\/faq\/linux-unix-check-file-system-consistency\/\">fsck command<\/a> On LUKS based systems:<br \/>\n<code># umount \/backup2<br \/>\n# fsck -vy \/dev\/mapper\/backup2<br \/>\n# mount \/dev\/mapper\/backup2 \/backu2<\/code><br \/>\nSee how to run <a href=\"https:\/\/www.cyberciti.biz\/faq\/howto-centos-rhel-fedora-debian-fsck-ext3-on-luks-volume\/\">fsck On LUKS (dm-crypt) based LVM physical volume<\/a> for more details.<\/p>\n<h2>How do I change LUKS passphrase (password) for encrypted partition?<\/h2>\n<p>Type the following command<br \/>\n<code>### see key slots, max -8 i.e. max 8 passwords can be setup for each device ####<br \/>\n# cryptsetup luksDump \/dev\/xvdc<br \/>\n# cryptsetup luksAddKey \/dev\/xvdc<\/code><\/p>\n<pre>Enter any passphrase: \r\nEnter new passphrase for key slot: \r\nVerify passphrase: \r\n<\/pre>\n<p>Remove or delete the old password:<br \/>\n<code># cryptsetup luksRemoveKey \/dev\/xvdc<\/code><br \/>\nPlease note that you need to enter the old password \/ passphrase.<\/p>\n<h2>What next?<\/h2>\n<p>You can store files or store backups using following software:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.cyberciti.biz\/faq\/linux-rsnapshot-backup-howto\/\">Debian \/ Ubuntu Linux Install and Configure Remote Filesystem Snapshot with rsnapshot Incremental Backup Utility<\/a><\/li>\n<li><a href=\"https:\/\/www.cyberciti.biz\/faq\/redhat-cetos-linux-remote-backup-snapshot-server\/\">How To Set Red hat \/ CentOS Linux Remote Backup \/ Snapshot Server<\/a><\/li>\n<\/ul>\n<h2>Check out related media<\/h2>\n<p>This tutorial also available in video format:<br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/OYE0OsWjd6M\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\" data-mce-fragment=\"1\"><\/iframe><\/p>\n<p><center><small>(Video 01: <a href=\"https:\/\/youtu.be\/OYE0OsWjd6M\">cryptsetup command demo<\/a>)<\/small><\/center><\/p>\n<h2>Conclusion<\/h2>\n<p>You now have an encrypted partition for all of your data.<\/p>\n<h3>Pros:<\/h3>\n<ol>\n<li>LUKS encrypts entire block devices and is therefore well-suited for protecting the contents of mobile devices such as removable storage media (usb pen) or laptop disk drives.<\/li>\n<li>You can also use with your nas server to protect backups.<\/li>\n<li>Intel and AMD cpus with AES-NI (Advanced Encryption Standard Instruction Set) can accelerate dm-crypt based encryption for Linux kernel v2.6.32+. This will speed up harddisk encryption.<\/li>\n<li>Works with swap partition too so that your laptop can use hibernation feature (suspend-to-disk) that writes out the contents of RAM to the swap partition before turning off the machine.<\/li>\n<\/ol>\n<h3>Cons:<\/h3>\n<ol>\n<li>LUKS only support up to 8 passwords i.e. only 8 users can have distinct access keys to the same device.<\/li>\n<li>LUKS is also not recommend for applications requiring file-level encryption.<\/li>\n<\/ol>\n<p>For more information see cryptsetup man page and read <a href=\"https:\/\/access.redhat.com\/knowledge\/docs\/Red_Hat_Enterprise_Linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">RHEL 6.x documentation<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux encryption methods There are two methods to encrypt your data: #1: Filesystem stacked level encryption eCryptfs \u2013 It is a cryptographic stacked Linux filesystem. eCryptfs stores cryptographic metadata in the header of each file written, so that encrypted files can be copied between hosts; the file will be decrypted with the proper key in &hellip; <a href=\"https:\/\/easy-admin.ca\/index.php\/2018\/08\/24\/cryptseup\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Cryptsetup on Centos 7 Server<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Cryptsetup on Centos 7 Server - HP Server","description":"Linux encryption methods There are two methods to encrypt your data: #1: Filesystem stacked level encryption eCryptfs \u2013 It is a cryptographic stacked Linux file"},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2369","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/2369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/comments?post=2369"}],"version-history":[{"count":0,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/2369\/revisions"}],"wp:attachment":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/media?parent=2369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/categories?post=2369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/tags?post=2369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}