Script to unlock an Ubuntu or Debian encrypted root filesystem via ssh.
To unlock the target system (after initial setup; see below):
- Run the following from your workstation:
unlock-cryptroot target.example.com
- Enter the password to your ssh key (if there is one).
- Enter the drive encryption password(s).
For additional options and default file locations, run:
unlock-cryptroot --help
Tested on:
- Ubuntu 18.04 (Bionic)
- Ubuntu 16.04 (Xenial)
- Ubuntu 14.04 (Trusty)
Relevant bug reports:
- Edit
/etc/crypttab
and add theinitramfs
option to each device you want to be able to remotely unlock during boot. Seeman 5 crypttab
for details. (Theinitramfs
option is not necessary for the root device or any resume devices, but it doesn't hurt.) Update your initramfs after making any changes (sudo update-initramfs -u
). - Install the dropbear ssh server into the initramfs:
sudo apt-get install dropbear-initramfs
- If you wish to use a non-default IP address or network device,
set the
ip=
kernel boot parameter:- Edit
/etc/default/grub
- Add your
ip=
parameter to theGRUB_CMDLINE_LINUX
variable - Save your changes
- Run
sudo update-grub
to install the changes
- Edit
- Prepare keys for public key authentication:
- Generate an ssh key pair for logging in to the initramfs:
sudo sh -c '(umask 0077 && mkdir -p /etc/initramfs-tools/root/.ssh)' sudo ssh-keygen -t rsa -b 4096 -o -a 100 \ -f /etc/initramfs-tools/root/.ssh/id_rsa
- Add the public key to the initramfs's
authorized_keys
:sudo cp /etc/initramfs-tools/root/.ssh/id_rsa.pub \ /etc/initramfs-tools/root/.ssh/authorized_keys
- Update the initramfs:
sudo update-initramfs -u
- Generate an ssh key pair for logging in to the initramfs:
- At boot, Ubuntu's initramfs will create a Netplan config file for
your network interface. This config file overrides the default
network configuration for that interface. If you do not want this
override, install an initramfs script that deletes the Netplan
config file after your system is unlocked. See the example
etc_*
file.
- Edit
/etc/crypttab
and add theinitramfs
option to each device you want to be able to remotely unlock during boot. Seeman 5 crypttab
for details. (Theinitramfs
option is not necessary for the root device or any resume devices, but it doesn't hurt.) Update your initramfs after making any changes (sudo update-initramfs -u
). - Install the dropbear ssh server into the initramfs:
sudo apt-get install dropbear-initramfs
- If you wish to use a non-default IP address or network device,
set the
ip=
kernel boot parameter:- Edit
/etc/default/grub
- Add your
ip=
parameter to theGRUB_CMDLINE_LINUX
variable - Save your changes
- Run
sudo update-grub
to install the changes
- Edit
- Prepare keys for public key authentication:
- Generate an ssh key pair for logging in to the initramfs:
sudo sh -c '(umask 0077 && mkdir -p /etc/initramfs-tools/root/.ssh)' sudo ssh-keygen -t rsa -b 4096 -o -a 100 \ -f /etc/initramfs-tools/root/.ssh/id_rsa
- Add the public key to the initramfs's
authorized_keys
:sudo cp /etc/initramfs-tools/root/.ssh/id_rsa.pub \ /etc/initramfs-tools/root/.ssh/authorized_keys
- Update the initramfs:
sudo update-initramfs -u
- Generate an ssh key pair for logging in to the initramfs:
- If you have one or more non-root non-resume partitions that you
want to be able to remotely unlock:
- Run
blkid
to get the UUID of each such partition. - Edit
/etc/initramfs-tools/conf.d/resume
and add a newRESUME=UUID=<uuid>
line for each UUID at the top of the file. The lastRESUME=
line must refer to your resume device. The result should look like this:RESUME=UUID=<uuid of non-root non-resume device #1> RESUME=UUID=<uuid of non-root non-resume device #2> RESUME=UUID=<uuid of resume device>
- Update the initramfs:
sudo update-initramfs -u
initramfs
crypttab option added in Ubuntu 16.04 (Xenial). - Run
- Install dropbear into the initramfs:
sudo apt-get install dropbear
- Set the
ip=
kernel boot parameter:- Edit
/etc/default/grub
- Add your
ip=
parameter to theGRUB_CMDLINE_LINUX
variable - Save your changes
- Run
sudo update-grub
to install the changes
- Edit
- The kernel
ip=
parameter conflicts with the system's normal networking configuration, so you must set up a script to deconfigure the interface after the drive is unlocked but before the normal networking configuration is applied. See the exampleetc_*
file.
- Copy the ssh private key for the target machine's initramfs to
the machine that will be doing the remote unlocking:
T=target.example.com # change as necessary scp root@"$T":/etc/initramfs-tools/root/.ssh/id_rsa \ ~/.ssh/id_rsa.initramfs_"$T"