Secure boot key management is the practice of controlling the cryptographic keys that a computer's firmware uses to decide which software is allowed to run before the operating system loads. Doing it properly means you own your platform keys (PK), key exchange keys (KEK), and the signature databases (db, dbx) rather than leaving them in whatever state the manufacturer shipped. You can set this up entirely from a Linux system with the tools that ship in most distributions, but you should not start until you understand one thing: replacing the factory keys, or enrolling your own, can leave a machine that refuses to boot. Back up first, always, and keep a recovery path open.

The procedures below use the standard UEFI variables and the widely available efitools and sbsigntools packages. Menu names and variable paths differ slightly by firmware vendor, motherboard model, and tool version, so treat any exact label as a guide and look for the nearest equivalent.

What You'll Need

  • A UEFI-based x86-64 or ARM64 machine with Secure Boot support, and firmware that lets you enter Setup Mode or clear the factory keys.
  • A Linux system with efitools, sbsigntools, openssl, and efivarfs mounted (normally at /sys/firmware/efi/efivars).
  • A physical or live USB rescue medium you can boot without Secure Boot, in case you lock yourself out.
  • Your own keys: a platform key, a key exchange key, and a signing key for the bootloader and kernel.
  • Knowledge of how your bootloader is signed today, and whether your kernel is signed or you rely on a shim.

Step-by-Step

  1. Confirm Secure Boot state and mode. Run mokutil --sb-state to see whether Secure Boot is enabled, and check the firmware setup for a mode indicator. Firmware usually reports one of three states: Setup Mode (no PK enrolled, variables writable), User Mode (keys enrolled, variables locked), and sometimes an audit mode. You can only enroll keys while in Setup Mode. To get there, enter firmware setup and use the option to clear or delete the platform key — often labelled “Clear Secure Boot keys”, “Reset to Setup Mode”, or similar under a Security or Boot tab.
  2. Back up the current variables before touching anything. Save copies of PK, KEK, db, and dbx so you can restore a working state. Read them from efivarfs or dump them with your firmware's key export function if one exists. Keep the backups on a separate drive, not on the machine you are about to reconfigure.
  3. Generate your own keys. Create a self-signed certificate for each role using OpenSSL: one for the PK, one or more for KEK, and one for db. Use a strong key size and keep the private keys offline or on encrypted storage. A simple starting point is a single owner key pair, but separating the PK, KEK, and db signing keys limits the damage if one private key leaks.
  4. Convert the certificates to the format your firmware expects. UEFI expects DER-encoded certificates or EFI signature lists (.esl). Tools such as cert-to-efi-sig-list from efitools produce the signature list from a PEM certificate. Keep the original PEM files; you will need them to sign binaries.
  5. Enroll the keys in the correct order. Write KEK and db first, then the PK last. Enrolling the PK switches the machine from Setup Mode back to User Mode and locks the variables, so if you write the PK first you may be unable to add the others. Use efi-updatevar or your firmware's key management screen, which many boards expose as “Append” or “Enroll” options for each database.
  6. Sign your bootloader and kernel. Sign the EFI bootloader binary (for example the GRUB or systemd-boot EFI file) with sbsign, using your db key and certificate. If you boot kernels directly, sign the kernel image too. If you rely on a shim, sign the shim and use Machine Owner Key (MOK) enrollment for the kernel, so the chain of trust runs from firmware to shim to kernel.
  7. Enroll the MOK if you use shim. When shim is in the chain, boot into it and use the MOK Manager screen to enroll your public key, or use mokutil --import from a running system and complete the enrollment at the next reboot. This lets shim trust your signed kernel without modifying the firmware db.
  8. Re-enable Secure Boot and test. Return to firmware setup, switch Secure Boot back on, and boot. Verify the running system with mokutil --sb-state and by checking that your signed bootloader loaded. If the machine boots, run your normal workload to confirm nothing in early boot depends on unsigned code.
  9. Document and store the keys safely. Record which certificate signed which binary, store private keys on encrypted removable media or in a password manager's secure notes, and note the firmware settings you changed. Losing the private keys means you cannot sign future bootloader updates, so plan for rotation and keep an older valid key enrolled while you roll out a new one.

Common Mistakes

The single most common failure is enrolling the platform key before the other databases, which locks the variables and leaves an unbootable or incomplete configuration. Another frequent error is signing only the bootloader while forgetting that the kernel, initramfs, or EFI drivers also need to be trusted — an unsigned component breaks the chain and the boot stops. People also forget that firmware updates can reset or re-sign the key databases, so keys must be re-enrolled or re-approved after an update. Finally, storing private keys on the same disk as the machine means a compromise of the OS gives an attacker the ability to sign malicious boot code, which defeats the point of Secure Boot.

Tips

Keep the factory keys as a fallback: many firmware setups have a “Restore factory keys” option that brings back the vendor PK, KEK, and db. Read the boot log from a rescue boot (“Secure Boot violation” messages usually name the failing binary) before changing settings again. Use a signing workflow you can repeat, such as a script that signs the bootloader and kernel after every update, so a routine package upgrade does not silently replace a signed binary with an unsigned one. Rotate keys with overlap: enroll the new key alongside the old, sign with the new one, test, then remove the old entry from db. If you manage several machines, keep a single documented key hierarchy and never reuse the same private key across unrelated systems.

Troubleshooting

If the machine stops booting with a Secure Boot violation, boot the rescue medium with Secure Boot off and inspect which binary failed. The two usual fixes are signing that binary with your db key or enrolling its certificate in db. If you cannot enter Setup Mode at all, your firmware may not expose clearing the PK — in that case, check the vendor documentation for a Secure Boot key management menu, since some boards require a firmware password or a specific boot mode before the option appears. If variables refuse to write, confirm efivarfs is mounted read-write and that the machine really is in Setup Mode. If shim rejects your kernel, re-enroll the public key through MOK Manager and verify the kernel image is signed with the matching key. If the system boots but some driver or module fails to load, that component is unsigned and needs either signing or a supported alternative. And if a firmware update wiped your keys, re-enroll from your backups and re-sign only what changed.

Frequently Asked Questions

Do I have to replace the factory keys?

No. Many people only enroll a MOK to trust their own kernel while keeping the factory keys. Owning the full key hierarchy gives more control, but it also increases the risk if you lose the private keys.

What happens if I lose my private key?

You can no longer sign new bootloader or kernel binaries that the firmware will accept. Keep a backup, and enroll a spare key alongside the active one so you have a working fallback.

Can I disable Secure Boot temporarily to fix a problem?

Yes, on most machines Secure Boot can be turned off in firmware setup. Use that only for diagnosis and troubleshooting, then re-enable it and fix the trust chain properly.

Does Secure Boot protect against malware after boot?

No. It verifies the boot chain up to the firmware and bootloader, but it does not stop malicious code that runs once the OS is loaded. It is one layer, not a complete security product.

Why do some distributions need shim and others do not?

Shim exists because firmware trusts only keys in db. A distribution that cannot ship its key in the firmware db uses shim, which is signed by a trusted key and then verifies the distribution's signed kernel through MOK.