I already asked once about LUKS unlocking of multiple HDDs in Linux: LUKS and multiple hard drives.

Now I would like to know how to secure store the keyfile used for the automatic unlock of the associated partitions.

My plan is (if possible):

  • Encrypt a small USB drive with LUKS that requires a passphrase

  • Unlock it at boot as the first drive by using the passphrase

  • Mount it to a given mount point, for instance /test (is this possible ?)

  • Now the keyfile can be safely read: /test/keyfile

  • Use the keyfile to unlock other drives without needing to ask password for them

  • LuksClose the USB drive in order to assure a certain degree of safety as soon as other drives have been unlocked

  • Automount /, /usr, /var and other mount-points as usual

Can this work? Basically I store the LUKS keyfile on a password-encrypted LUKS USB drive that only asks for passphrase once, while all other drives can be unlocked without further action. I'm not sure if there is some way to make the USB drive be unlocked first, then be mounted and only then the other drives try to access the keyfile. Furthermore in what concerns automation I suppose /etc/fstab and /etc/crypttab should be accessible BEFORE the other drives can be mounted, but this is not possible if the whole / file system is LUKS encrypted.

Unless there is the possibility of fully manually configure how LUKS works:

  • LuksOpen /dev/sdc1 usb_keyfile

  • mount /dev/mapper/usb_keyfile /keyfile (is this possible ?)

  • LuksOpen --keyfile /keyfile/key /dev/sda1 disk1

  • LuksOpen --keyfile /keyfile/key /dev/sdb1 disk2

  • LuksClose /dev/sdc1

Basically being able to run a shell script just after the required modules have been loaded and disable automatic LUKS password prompt.

Additionnal details

  • Distribution used: Gentoo GNU/Linux (amd64) or Debian GNU/Linux (amd64) because I'd like to apply this procedure to multiple installations
  • 1
    I don't see why this wouldn't be possible. It may be somewhat involved if you want an encrypted root partition, but it's not like there is any magic happening even before / gets mounted. One critical piece of information that's missing for your question to be properly answerable though is what distribution are you using?.
    – user
    Feb 15, 2013 at 10:29
  • @MichaelKjörling: updated my question. I don't think the solution is forcefully distribution-dependend since I could adapt it anyway. I'd like to know how (if possible) you'd implement such a solution (on Gentoo, Debian, Ubuntu or even RHEL/Centos). I could always try to adapt it to my distribution in a second time.
    – user51166
    Feb 15, 2013 at 10:36

1 Answer 1


Your approach looks good. Some remarks though:

  • If you want to encrypt rootfs, you'll need to use initrd (to have some minimal unencrypted system that will process the encrypted partitions).

    If the USB device is removable, both initrd and kernel can be stored on the USB to heighten tamper resistance (supposing you make sure the USB won't get into unauthorized hands) - which is usually why one encrypts rootfs. Once both kernel and initrd are on a removable media, you can ensure that nobody changes the kernel (or initrd) from the running system by simply removing the media.

    This is of course not an option if you want to have it inside of a server, but then again the question stands whether it makes sense to have such a device at all and not to use a small partition on one of the hard-drives. If for example all drives in the machine are in RAID, one would probably want to put rootfs on the USB as well. An interesting alternative to an internally connected USB flash device could be a CompactFlash card attached to ATA interface through an adapter, by the way.

    Some distributions offer prepared solutions for encrypted root, some don't - but mostly it's question of putting a couple of lines into initrd script before it tries to mount the "real" root (see for example man pages for pivot_root, usually in sections 2 (syscall) and 8 (bonary), if you're not familiar with the process).

  • remember to backup the keys and passphrases in case your USB drive dies. LUKS follows rather one-sided approach when it comes to damaging its header - once a single sector of its header (key-slot to be more precise) dies, you are unable to mount it. This is to make sure that erasing the header isn't effectively thwarted by block reallocation performed by the device itself (because that's what flash-memory based devices do a lot) - the key is spread over the whole key slot and one needs all of the data to reconstruct it - there is no redundancy. See Clemens Fruwirth's website for deeper discussion.

    That said, maybe a simple encrypted device on the USB would be enough (check section PLAIN MODE in man cryptsetup). Or a file encrypted with e.g. openssl enc. The former might actually be an option even for the encrypted partitions themselves.

  • 1
    Both initrd and kernel can reside wherever you want - they are only used by the bootloader and running system doesn't need those (except for updates of course). Please check the updated answer, whether the security POV is a bit clearer now.
    – peterph
    Feb 15, 2013 at 14:30
  • 1
    Ah, ok - no, it's not like this. LVM isn't necessary for this. First you create the RAID device with mdadm (MD is not part of LVM). After that you set up the encryption on the md-device (/dev/md/device-name). The downside is that the MD supeblock is not encrypted, but that's rarely an issue.
    – peterph
    Feb 17, 2013 at 17:42
  • 1
    Exactly. Actually, it's the kernel's md device driver that does it. mdadm is a system utility that instructs the kernel how to set up the virtual RAID devices. You can do that either manually (on each boot) or automatically. In the latter case, mdadm creates a superblock on the devices where the configuration of the RAID is stored 0 see --auto-detect option in man mdadm.
    – peterph
    Feb 18, 2013 at 15:56
  • 1
    Sure - the md is just a layer between the physical device (a) and the filesystem, pretty much in the same way the encryption and LVM is. It's just a question of the order in which you use them (my personal preference would probably be: physical device - md - lvm - encryption - filesystem).
    – peterph
    Feb 18, 2013 at 20:04
  • 1
    For copying the data from one RAID to another you probably want to use dd between two already set-up arrays instead of copying the whole device including the superblock, since that would be likley confusing mdadm.
    – peterph
    Feb 19, 2013 at 8:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .