Remote unlocking of a LUKS-encrypted disk

Installation
We start by updating the distribution’s package repository
sudo apt-get updateAnd then we can install dropbear-initramfs
sudo apt-get install dropbear-initramfsWhile Dropbear is being installed, the following message may appear:
“dropbear: WARNING: Invalid authorized_keys file, SSH login to initramfs won’t work!”
We don’t need to worry about it, since that is exactly what we are going to set up next.
Installing public SSH keys for authentication
In the /etc/dropbear/initramfs folder we create a file called authorized_keys
sudo touch /etc/dropbear/initramfs/authorized_keysNow we have to add our public SSH key so that we can connect remotely and securely. This can be done in several ways, depending on how we are accessing the server (or workstation) to configure it.
In my case, I already have a user on the machine with the keys I want to add, so I just have to copy them over. Assuming my user name is “john” and that this user has administrator rights, I can run the following command:
sudo bash -c "cat /home/john/.ssh/authorized_keys >> /etc/dropbear/initramfs/authorized_keys"Additional settings to make the service more secure
Now we should make some additional settings to improve the security of our SSH server. To do this we add some options to the Dropbear configuration file. This file is in the same folder where we created the authorized_keys file and is called dropbear.conf. In it we uncomment the “DROPBEAR_OPTIONS=” line.
Edit the file with whichever editor you like that is available. In this example I will use vim:
vim /etc/dropbear/initramfs/dropbear.confNow we add the following options to that line so that it looks like this:
DROPBEAR_OPTIONS="-I 120 -j -k -p 2222 -s -c cryptroot-unlock"The options added are passed to Dropbear. They change the SSH server’s behaviour as follows:
-I 120 : If the connection is idle for the specified time (in seconds), it is closed
-j : Disables local port forwarding
-k : Disables remote port forwarding
-p : Sets the port the service listens on, if we want it to be a port other than 22
-s : Disables password logins. This option is very important! Don’t forget to add the public keys mentioned earlier, otherwise there will be no way to access the service remotely.
-c : Forces the given command to be run. With the option we set, we run the command that lets us enter the key needed to unlock the encrypted disk.
IPv4 address configuration
If we want to set a static IPv4 address instead of the one assigned by DHCP, we need to make the following changes.
Edit the file /etc/initramfs-tools/initramfs.conf and add the following line:
IP=xxx.xxx.xxx.xxx::xxx.xxx.xxx.xxx:xxx.xxx.xxx.xxx:myhostnameThe xxx.xxx.xxx.xxx values must be replaced with the corresponding addresses, as follows:
<ipv4 address>::<ipv4 gateway>:<netmask>
Now we save the settings and make them take effect:
sudo update-initramfs -uOn Ubuntu Server 22.04 you should also run:
sudo update-grubThese instructions were tested on Ubuntu Server 22.04 and on Pop_OS! 22.04.