LUKS is the disk encryption for Linux.
First time when you encrypt a partition with LUKS (or when you select encrypt disk option during OS installation), you have to specify a password that will be used when you open the LUKS partition.
But, after that, you can mount and unmount the partition as many times as you like without having to enter the password until you reboot the system.
Imagine the following two scenarios:
In this tutorial, we’ll discuss everything that you need to know about LUKS key management.
In LUKS, for a single encrypted partition, you can have eight different keys.
Any one of the eight different keys can be used to open the encrypted partition.
You can choose to have only one key on a partition, or you can assign all eight different keys.
These keys are stored in LUKS key slots for the partition. So, there will be 8 key slots for a partition.
To view all key slots, use cryptsetup luksDump as shown below. In this example, it is using only two slots.
# cryptsetup luksDump /dev/sdb1 | grep Slot Key Slot 0: ENABLED Key Slot 1: ENABLED Key Slot 2: DISABLED Key Slot 3: DISABLED Key Slot 4: DISABLED Key Slot 5: DISABLED Key Slot 6: DISABLED Key Slot 7: DISABLED
In the above:
To add a new LUKS passphrase (LUKS key) to the /dev/sdb1 LUKS encrypted partition, use cryptsetup luksAddKey command as shown below.
# cryptsetup luksAddKey /dev/sdb1 Enter any passphrase: Enter new passphrase for key slot: Verify passphrase:
In the above:
Anytime you add a new LUKS key, it will be added to the next available slot. Since we already had two existing Keys, the new key was added to the slot#2, which was the next available slot.
So, Slot#0 through 2 will says “ENABLED”. We have three LUKS keys on /dev/sdba1.
# cryptsetup luksDump /dev/sdb1 | grep Slot Key Slot 0: ENABLED Key Slot 1: ENABLED Key Slot 2: ENABLED Key Slot 3: DISABLED Key Slot 4: DISABLED Key Slot 5: DISABLED Key Slot 6: DISABLED Key Slot 7: DISABLED
Instead of LUKS adding the new key to the next available slot, you can also add new key to a specific slot.
For this use -S option in the cryptsetup luksAddKey as shown below.
In this example, we are adding new LUKS key to Slot#5. I assigned the new passphrase as: PasswordforSlot5
# cryptsetup luksAddKey /dev/sdb1 -S 5
As we see below, now Slot#5 says “ENABLED”, while slot#3 and #4 are still in DISABLED state.
# cryptsetup luksDump /dev/sdb1 | grep Slot Key Slot 0: ENABLED Key Slot 1: ENABLED Key Slot 2: ENABLED Key Slot 3: DISABLED Key Slot 4: DISABLED Key Slot 5: ENABLED Key Slot 6: DISABLED Key Slot 7: DISABLED
At this stage, we have 4 LUKS key assigned to /dev/sdb1 encrypted partition.
To remove an existing key from LUKS partition, use cryptsetup luksRemoveKey as shown below.
In this example, you just have to enter the password (key) that you want to be erased.
Remove/Erase/Delete a LUKS key from a slot. You don’t have to specify the slot number. Instead specify the key to be deleted!.
In this example, when it prompts “Enter LUKS passphrase to be deleted:”, I entered the key that created in the previous example: PasswordforSlot5
# cryptsetup luksRemoveKey /dev/sdb1 Enter LUKS passphrase to be deleted:
As you see from the following luksDump output, the key in Slot#5 is now erased.
# cryptsetup luksDump /dev/sdb1 | grep Slot Key Slot 0: ENABLED Key Slot 1: ENABLED Key Slot 2: ENABLED Key Slot 3: DISABLED Key Slot 4: DISABLED Key Slot 5: DISABLED Key Slot 6: DISABLED Key Slot 7: DISABLED
If you don’t have the key for a particular slot, but want to just delete it, you can do it using cryptsetup luksKillSlot command as shown below.
In this example, we are deleting the key from LUKS slot#2.
For this, you have to enter the LUKS key for any one of the slots. This is only as a validation before it delete the Key from slot#2.
# cryptsetup luksKillSlot /dev/sdb1 2 Enter any remaining LUKS passphrase:
As you see from the following luksDump output, the key in Slot#2 is now erased.
# cryptsetup luksDump /dev/sdb1 | grep Slot Key Slot 0: ENABLED Key Slot 1: ENABLED Key Slot 2: DISABLED Key Slot 3: DISABLED Key Slot 4: DISABLED Key Slot 5: DISABLED Key Slot 6: DISABLED Key Slot 7: DISABLED
You can also add a new LUKS key based on a keyfile that you already have as shown below.
# cryptsetup luksAddKey /dev/sdb1 masterkeyfile Enter any passphrase:
In the above:
As you see from the following luksDump output, this new key from the masterkeyfile is added to slot#2
# cryptsetup luksDump /dev/sdb1 | grep Slot Key Slot 0: ENABLED Key Slot 1: ENABLED Key Slot 2: ENABLED Key Slot 3: DISABLED Key Slot 4: DISABLED Key Slot 5: DISABLED Key Slot 6: DISABLED Key Slot 7: DISABLED
If you rebooted the server, and unable to mount your encrypted LUKS partition, because you’ve forgotten your LUKS password, then you are out of luck, you can reset it.
But, if an encrypted LUKS partition is already opened, and if you have not rebooted the system, and you’ve forgot the LUKS password for the partition that is already mounted (at least LUKS opened once since the last reboot), then you can assign a new LUKS key.
In this “forgot my LUKS password” scenario, you can do the following two steps:
In this example, I have the /home1 partition mounted, which is a LUKS encrypted partition, but I don’t have the password for this.
# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 127G 44G 76G 37% / /dev/mapper/home1 93G 188M 88G 1% /home1
The volume name is whatever is there after “/dev/mapper/” in the 1st column of the above df command output. So, in our example, the volume name is “home1”
The following dmsetup table –showkeys will show the encrypted keys of all the partitions that are mounted on your system.
# dmsetup table --showkeys home1: 0 197259264 crypt aes-cbc-essiv:sha256 607f482870c795a9b1e307ffbfc6643eaa219e9ef8c6773de02cd298c8fcda3c 0 8:17 4096
The field after “aes-cbc-essiv:sha256” is the encrypted password. Get the encrypted LUKS key and store it in a file.
# vi existinglukskey.txt 607f482870c795a9b1e307ffbfc6643eaa219e9ef8c6773de02cd298c8fcda3c
Now, we have to convert this existing key from a text file to a binary file. Use xxd command as shown below.
# xxd -r -p existinglukskey.txt existinglukskey.bin
In the above:
Finally, add a new LUKS key by using the existing LUKS key that we extracted into the binary file.
# cryptsetup luksAddKey /dev/sdb1 --master-key-file <(cat existinglukskey.bin) Enter new passphrase for key slot: Verify passphrase:
In the above:
Again, remember this: If you forget the LUKS password after you reboot the system, you are out of Luks (out of luck), as you can’t reset it.
You can also dump the MasterKey and keep it safe somewhere. Use the –dump-master-key option along with luksDump as shown below.
# cryptsetup luksDump --dump-master-key /dev/sdb1 Are you sure? (Type uppercase yes): YES Enter LUKS passphrase: LUKS header information for /dev/sdb1 Cipher name: aes Cipher mode: cbc-essiv:sha256 Payload offset: 4096 UUID: 146d639a-757c-4bcb-aee6-8fe815345104 MK bits: 256 MK dump: 60 7f 48 28 70 c7 95 a9 b1 e3 07 ff bf c6 64 3e aa 21 9e 9e f8 c6 77 3d e0 2c d2 98 c8 fc da 3c
Keep in mind that you have to store the LUKS header dump in a safe place. Using the master key dump, someone can get to the LUKS encrypted partition.
Article Number: 345
Posted: Tue, Jul 31, 2018 3:54 PM
Last Updated: Tue, Jul 31, 2018 3:54 PM
Online URL: http://kb.ictbanking.net/article.php?id=345