Skip to content

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 nvme0n1 and 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:

PartitionTypeDescription
nvme0n1p1ESPVFAT, 1 GB, mounted on /boot
nvme0n1p2swapEncrypted with a random key
nvme0n1p3LUKSRoot /
nvme0n1p4LUKS/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:

bash
pacman -S gptfdisk
sgdisk --change-name=2:cryptswap /dev/nvme0n1

LUKS 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:

bash
cryptsetup luksFormat /dev/nvme0n1p3
cryptsetup open /dev/nvme0n1p3 root
mkfs.ext4 /dev/mapper/root

Then /home:

bash
cryptsetup luksFormat /dev/nvme0n1p4
cryptsetup open /dev/nvme0n1p4 home
mkfs.ext4 /dev/mapper/home

The boot partition:

bash
mkfs.fat -F32 /dev/nvme0n1p1

Then we can mount the file systems for the installation:

bash
mount /dev/mapper/root /mnt
mount --mkdir /dev/mapper/home /mnt/home
mount --mkdir /dev/nvme0n1p1 /mnt/boot

Check before going on:

bash
lsblk -o NAME,FSTYPE,MOUNTPOINTS

The 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:

bash
pacstrap -K /mnt base linux linux-firmware cryptsetup gptfdisk sbctl

Don'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: keyboard loads 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:

bash
blkid /dev/nvme0n1p3

Create 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=no removes 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:

bash
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.key

The key file is on the encrypted root, so it is protected by the main passphrase.

Swap ​

Erase any leftover plaintext swap signature:

bash
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 (fmask and dmask) to avoid the warnings from bootctl about random-seed being world-readable. So /boot is no longer readable without sudo.

We remount /boot to apply the new masks (the remount option does not re-evaluate them):

bash
umount /boot
mount /boot

UKI (Unified Kernel Image) and systemd-boot ​

Warning: The file /etc/vconsole.conf must 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:

bash
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:

bash
mkdir -p /boot/EFI/Linux
mkinitcpio -P
rm -f /boot/initramfs-linux*.img
bootctl install

Edit /boot/loader/loader.conf:

default arch-linux.efi
timeout 3
console-mode keep
editor no

Note: editor no prevents 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:

bash
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 s

Secure 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:

bash
sudo sbctl status

Create and enroll your own keys:

bash
sudo sbctl create-keys
sudo sbctl enroll-keys -m
sudo sbctl status        # Vendor Keys must mention microsoft

Note: The -m option at enrollment adds the Microsoft keys back, because some machines need them (firmware updates).

Signing ​

For now nothing is signed on the boot partition:

bash
sudo sbctl verify

We sign by hand for now:

bash
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.efi

Everything should be green:

bash
sudo sbctl verify        # green everywhere before going on

Note: The -s option saves the path in /var/lib/sbctl/files.json. Without it, there is no automatic re-signing.

Note: /EFI/BOOT/BOOTX64.EFI is the standard UEFI fallback path, used if the NVRAM is empty or reset.

Note: If vmlinuz-linux shows 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.

bash
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
EOF

It 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:

bash
sudo sbctl status

Maintenance ​

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/.

bash
sudo mkinitcpio -P && sudo sbctl sign-all && sudo sbctl verify

Changing the kernel command line ​

The command line is edited in the file /etc/kernel/cmdline.

Then:

bash
sudo mkinitcpio -P && sudo sbctl sign-all && sudo sbctl verify

If 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.