Skip to content

Instantly share code, notes, and snippets.

@aalvesjr
Created February 12, 2016 21:23
Show Gist options
  • Select an option

  • Save aalvesjr/9b2cdc832fa9e5fffbb2 to your computer and use it in GitHub Desktop.

Select an option

Save aalvesjr/9b2cdc832fa9e5fffbb2 to your computer and use it in GitHub Desktop.
[LINUX] Using LUKS with keyfile and many passphrases

CryptSetup and LUKS

[Cryptsetup](https://gitlab.com/cryptsetup/cryptsetup) is utility used to conveniently setup disk encryption based on DMCrypt kernel module.

LUKS (Linux Unified Key Setup) is the standard for Linux hard disk encryption

Install

apt-get install cryptsetup

Creating one encrypted volume

Note

  • In example was used a external device "/dev/sdb" with one partition "/dev/sdb1" created with format "ext4"
  • The directory used in mount command was "/mnt/folder-crypt/"

Encrypt volume with 'cryptsetup' and formats a LUKS device

sudo cryptsetup luksFormat /dev/sdb1

In this moment you will inform the passphrase for the encrypted volume.

Opening the encrypted volume

At open the volume is necessary to assign a name for him

sudo cryptsetup luksOpen /dev/sdb1 secret

Format with ext4 (or any other) this volume, and it will be ready to be mounted

sudo mkfs.ext4 /dev/mapper/secret

sudo mount /dev/mapper/secret /mnt/folder-crypt

From this moment the folder "/mnt/folder-crypt/" is ready to be used

Closing the encrypted volume

First dismount the volume, after close the encrypter

sudo umount /mnt/folder-crypt 
sudo cryptsetup luksClose secret

Tips

Cryptsetup allow that a volume has 'until' 8 passwords (passphrases and/or keyfiles)

Checking how many passwords the LUKS volume has:

sudo cryptsetup luksDump /dev/sdb1

Will be listed 8 key slots (0 to 7)

LUKS header information for /dev/sdb1
...
Key Slot 0: ENABLED
Key Slot 1: DISABLED
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 this example, only a key "0" is being used

Using others passphrases

To add other passphrase in a slot "DISABLED"

sudo cryptsetup luksAddKey --key-slot 1 /dev/sdb1 

And inform the passphrase

Using one keyfile

Note In this example the file used was created with command:

dd if=/dev/urandom of=/path/to/keyfile bs=1024 count=4

sudo cryptsetup luksAddKey /dev/sdb1 /path/to/keyfile

And to open the encrypted volume with a keyfile

sudo cryptsetup --key-file=/path/to/keyfile luksOpen /dev/sdb1 secret

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment