How to configure encrypted storage with LUKS using passphrases
Environment
- Red Hat Enterprise Linux 9
- Red Hat Enterprise Linux 8
- Red Hat Enterprise Linux 7
- Red Hat Enterprise Linux 6
- Red Hat Enterprise Linux 5
- LUKS
Issue
- How to configure LUKS encryption on a partition?
- How to setup LUKS on top of a logical volume?
- How to encrypt physical volumes with LUKS?
- How to encrypt a filesystem with LUKS?
Resolution
See also: How to configure encrypted storage with LUKS using exportable keys instead of passphrases and How to add a passphrase, key, or keyfile to an existing LUKS device
-
Initialize a LUKS partition with the command:
cryptsetup luksFormat DEV
Potential containers for a "LUKS partition":/dev/sda3,/dev/sdb,/dev/VG/LV,/dev/mapper/mpath1, etc.
From here on in this document, this will be referred to asDEV
Examples:# cryptsetup luksFormat /dev/sda3OR
# cryptsetup luksFormat /dev/VG/LV -
Choose and make note of a name for the dm-device which will offer a decrypted view of
DEV
From here on in this document, this will be referred to asMAPPING
Examples:- luks-DEV
luks-sda3luks-logvol_nameluks-mpath1
- luks-UUID-of-DEV
luks-33fbddf3-0a10-4911-aef4-77102201a938luks-$(cryptsetup luksUUID /dev/sda3)
- completely-arbitrary-name
sda3-unlockedmpath1-decryptedsecret_monkeys
Note that the full path to the decrypted view of
DEVwill be/dev/mapper/MAPPING-- this will be the device on which further data can be laid. - luks-DEV
-
Next up, a choice must be made -- interactive boot-time decryption or manual decryption?
-
Should
DEVprompt for a passphrase at boot?
If so, a new entry must be added to/etc/crypttabin the form of: "MAPPING DEV"MAPPINGis the arbitrary name chosen in step 2DEVis either the path or UUID of device from step 1
Examples:
# echo "luks-sda3 /dev/sda3" >>/etc/crypttabOR
# echo "luks-$(cryptsetup luksUUID /dev/sda3) UUID=$(cryptsetup luksUUID /dev/sda3)" >>/etc/crypttabNote: Device names in Linux are extremely volatile (e.g., multipath maps and VG/LV names can be renamed; SCSI dev node names like
sda&sdbare non-deterministic and can be different between boots). For this reason the author must unequivocally recommended the use of device UUIDs for the second field. A LUKS device's UUID can be found withcryptsetup luksUUID DEV(as seen above) or withblkid DEV.*- Should
DEVrequire manual intervention to be unlocked?
If so, nothing needs to be added to/etc/crypttab
Instead, root (or users withsudoaccess) will have to executecryptsetup luksOpenandcryptsetup luksClosecommands (not to mention potentiallymountandumount)
Examples:
# cryptsetup luksOpen DEV MAPPING # cryptsetup luksOpen /dev/sda3 sda3-unlocked # cryptsetup luksOpen UUID=$(cryptsetup luksUUID /dev/sda3) sda3-unlocked # cryptsetup luksClose MAPPING # cryptsetup luksClose sda3-unlockedIf this manual intervention method is chosen, Step 4 can be skipped
-
-
Recommended: reboot to ensure there are no problems auto-unlocking
DEVat boot time
Once the system comes up, check thatDEVwas unlocked using one of the following:# cryptsetup status MAPPING- (Should say "/dev/mapper/MAPPING is active.")
# lsblk- (Should show
MAPPINGas a "crypt" device under parentDEV)
# ls /dev/mapper/MAPPING- (If this file is not there, the device is not opened)
Note: If rebooting is not an option, use the
cryptsetup luksOpen DEV MAPPINGcommand demonstrated in step 3 before continuing* -
Use
/dev/mapper/MAPPINGfor whatever purpose intended, e.g., put a filesystem on it, use it as the backing storage for an LVM volume group, etc.Examples:
# mkfs.ext4 /dev/mapper/MAPPING # mkfs.xfs /dev/mapper/sda3-unlocked # vgcreate SOME_NEW_VG /dev/mapper/sda3-unlocked # vgextend SOME_EXISTING_VG /dev/mapper/sda3-unlockedNote: Assuming success in step 4, there are no special considerations for storing a filesystem on top of
/dev/mapper/MAPPING-- simply put it in/etc/fstabas normal. Related: How to configure encrypted swap and How to extend a LUKS encrypted volume.*
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.