Set up disk encryption and Secure Boot on ArchLinux
The goal of this guide is to set up disk encryption (root partition, swap and /home), and to enable Secure Boot with personal keys.
Prerequisites:
- The system must boot in UEFI mode.
- Secure Boot must be disabled in your BIOS for now.
- ArchLinux will be the only system installed on this machine.
The following steps take place during the ArchLinux installation, following the official guide.
Partitioning
Note: We assume the SSD is named
nvme0n1and that all the partitions will be on it.
Creating the partitions
Note: This step happens at the partitioning stage of the official guide.
Make sure to create the partitions following this layout:
| Partition | Type | Description |
|---|---|---|
| nvme0n1p1 | ESP | VFAT, 1 GB, mounted on /boot |
| nvme0n1p2 | swap | Encrypted with a random key |
| nvme0n1p3 | LUKS | Root / |
| nvme0n1p4 | LUKS | /home |
Warning: The ESP is 1 GB to leave some headroom, since the main UKI is about 70 MB, and the fallback about 220 MB.
Name the swap partition:
pacman -S gptfdisk
sgdisk --change-name=2:cryptswap /dev/nvme0n1LUKS containers
Note: This step happens at the formatting stage of the official guide.
Instead of the standard formatting described in the official guide, we will create the LUKS containers for our partitions and format the encrypted partitions they hold.
First the root:
cryptsetup luksFormat /dev/nvme0n1p3
cryptsetup open /dev/nvme0n1p3 root
mkfs.ext4 /dev/mapper/rootThen /home:
cryptsetup luksFormat /dev/nvme0n1p4
cryptsetup open /dev/nvme0n1p4 home
mkfs.ext4 /dev/mapper/homeThe boot partition:
mkfs.fat -F32 /dev/nvme0n1p1Then we can mount the file systems for the installation:
mount /dev/mapper/root /mnt
mount --mkdir /dev/mapper/home /mnt/home
mount --mkdir /dev/nvme0n1p1 /mnt/bootCheck before going on:
lsblk -o NAME,FSTYPE,MOUNTPOINTSThe mount points must be /mnt, /mnt/home, /mnt/boot.
Installing the system
Note: This step happens at the installation stage of the official guide.
Your pacstrap command must contain at least the following:
pacstrap -K /mnt base linux linux-firmware cryptsetup gptfdisk sbctlDon't forget to add amd-ucode or intel-ucode depending on your CPU, as well as a text editor, and a network configuration manager.
Boot configuration
Here we will set up the opening of the encrypted file systems at boot.
mkinitcpio
Edit /etc/mkinitcpio.conf as follows:
HOOKS=(base systemd autodetect microcode modconf kms keyboard sd-vconsole block sd-encrypt filesystems fsck)
Note:
keyboardloads the modules needed to type the passphrase.
Kernel command line
We will now set up the kernel command line. Note the UUID of the root LUKS container:
blkid /dev/nvme0n1p3Create the file /etc/kernel/cmdline and put in it, on a single line:
rd.luks.name=<UUID-root-container>=root root=/dev/mapper/root rw rd.luks.options=password-echo=no
Note:
password-echo=noremoves the asterisks when typing the passphrase.
crypttab
Edit /etc/crypttab this way:
home UUID=<UUID-home-container> none luks
swap PARTLABEL=cryptswap /dev/urandom swap,cipher=aes-xts-plain64,size=512,sector-size=4096,discard
Note: The root is not listed because it is opened by the initramfs.
Automatic opening of the encrypted /home partition
We want to avoid typing a second passphrase to open /home.
systemd automatically looks for /etc/cryptsetup-keys.d/<name>.key when the password field is none in /etc/crypttab.
Set up a key to open /home using this mechanism:
mkdir -p /etc/cryptsetup-keys.d
dd if=/dev/urandom of=/etc/cryptsetup-keys.d/home.key bs=512 count=4
chmod 600 /etc/cryptsetup-keys.d/home.key
cryptsetup luksAddKey /dev/nvme0n1p4 /etc/cryptsetup-keys.d/home.keyThe key file is on the encrypted root, so it is protected by the main passphrase.
Swap
Erase any leftover plaintext swap signature:
wipefs -a /dev/nvme0n1p2
Warning: With a random key, hibernation is not possible (the key is lost at shutdown).
fstab
Using the blkid command, identify the partitions inside the encrypted containers (their device starts with /dev/mapper) for the root and /home.
Edit /etc/fstab:
UUID=<UUID-FS-root> / ext4 rw,relatime 0 1
UUID=<UUID-FS-home> /home ext4 rw,relatime 0 2
UUID=<UUID-ESP> /boot vfat rw,relatime,fmask=0137,dmask=0077,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 2
/dev/mapper/swap none swap defaults 0 0
Note: We apply restrictive masks on the ESP (
fmaskanddmask) to avoid the warnings frombootctlaboutrandom-seedbeing world-readable. So/bootis no longer readable withoutsudo.
We remount /boot to apply the new masks (the remount option does not re-evaluate them):
umount /boot
mount /bootUKI (Unified Kernel Image) and systemd-boot
Warning: The file
/etc/vconsole.confmust be created as described in the official guide before starting this step.
Edit /etc/mkinitcpio.d/linux.preset to define the boot presets as follows:
ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/vmlinuz-linux"
PRESETS=('default' 'fallback')
default_uki="/boot/EFI/Linux/arch-linux.efi"
default_options="--cmdline /etc/kernel/cmdline"
fallback_uki="/boot/EFI/Linux/arch-linux-fallback.efi"
fallback_options="-S autodetect --cmdline /etc/kernel/cmdline"Installing on the boot partition:
mkdir -p /boot/EFI/Linux
mkinitcpio -P
rm -f /boot/initramfs-linux*.img
bootctl installEdit /boot/loader/loader.conf:
default arch-linux.efi
timeout 3
console-mode keep
editor no
Note:
editor noprevents editing the command line at boot.
Finish the installation following the official guide, then reboot.
Checks
Before going on with Secure Boot, we will run a few checks once the system has booted:
bootctl status | head -5 # Current Boot Loader: systemd-boot
cat /proc/cmdline # Check that it is the right one
swapon --show # /dev/dm-..., never nvme0n1p2
systemd-analyze blame | head # no entry at ~90 sSecure Boot
Reboot the machine and enter the BIOS settings.
Look for the Secure Boot configuration page, use the option to clear its keys, and put it in Setup Mode.
Leave Secure Boot disabled for now. Save and reboot.
Creating and enrolling the keys
Check that Setup Mode is enabled, otherwise go back to the BIOS settings:
sudo sbctl statusCreate and enroll your own keys:
sudo sbctl create-keys
sudo sbctl enroll-keys -m
sudo sbctl status # Vendor Keys must mention microsoft
Note: The
-moption at enrollment adds the Microsoft keys back, because some machines need them (firmware updates).
Signing
For now nothing is signed on the boot partition:
sudo sbctl verifyWe sign by hand for now:
sudo sbctl sign -s /boot/EFI/systemd/systemd-bootx64.efi
sudo sbctl sign -s /boot/EFI/BOOT/BOOTX64.EFI
sudo sbctl sign -s /boot/EFI/Linux/arch-linux.efi
sudo sbctl sign -s /boot/EFI/Linux/arch-linux-fallback.efiEverything should be green:
sudo sbctl verify # green everywhere before going on
Note: The
-soption saves the path in/var/lib/sbctl/files.json. Without it, there is no automatic re-signing.
Note:
/EFI/BOOT/BOOTX64.EFIis the standard UEFI fallback path, used if the NVRAM is empty or reset.
Note: If
vmlinuz-linuxshows as unsigned, that is normal: it is never loaded directly by the firmware in a UKI setup.
Automatic signing
We will create a hook for systemd-boot so that it gets signed automatically during system updates. By default, the systemd package does not refresh the copy on the ESP.
sudo mkdir -p /etc/pacman.d/hooks
sudo tee /etc/pacman.d/hooks/95-systemd-boot.hook > /dev/null <<'EOF'
[Trigger]
Type = Package
Operation = Upgrade
Target = systemd
[Action]
Description = Updating systemd-boot
When = PostTransaction
Exec = /usr/bin/bootctl update
EOFIt runs before zz-sbctl.hook (alphabetical order), so the new copy is signed right after.
Enabling Secure Boot
Go to the BIOS settings and enable Secure Boot, save and reboot.
Check that it is enabled:
sudo sbctl statusMaintenance
Below are a few system maintenance operations.
Automatic signing for Secure Boot
The zz-sbctl.hook hook re-signs everything in the database at the end of a pacman transaction.
Linux kernel update: nothing to do.
Manual signing
Anything run by hand bypasses the hook. The same goes after bootctl install or bootctl update: they copy an unsigned binary back from /usr/lib/.
sudo mkinitcpio -P && sudo sbctl sign-all && sudo sbctl verifyChanging the kernel command line
The command line is edited in the file /etc/kernel/cmdline.
Then:
sudo mkinitcpio -P && sudo sbctl sign-all && sudo sbctl verifyIf you get stuck
sbctl never touches the firmware. The BIOS always keeps the option to restore the factory keys and to disable Secure Boot: no situation is irreversible.