Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Last active May 23, 2025 18:30
Show Gist options
  • Save gdamjan/ccdcda2c91119406a0f8d22f8b8f2c4a to your computer and use it in GitHub Desktop.
Save gdamjan/ccdcda2c91119406a0f8d22f8b8f2c4a to your computer and use it in GitHub Desktop.
Replace grub2 with systemd-boot on Ubuntu 22.04
  1. remove grub
apt purge --allow-remove-essential grub2-common grub-pc-bin grub-pc grub-gfxpayload-lists grub-efi-amd64-bin grub-efi-amd64-signed grub-common os-prober shim-signed libfreetype6 
apt-get autoremove --purge
rm -rf /boot/grub/
rm -rf /boot/efi/EFI/ubuntu
  1. make sure it's not installed back
    • apt-mark hold "grub*"
  2. install systemd-boot
apt install systemd-boot systemd-ukify
  1. create /etc/kernel/cmdline with the wanted kernel command line (check /proc/cmdline for inspiration)
  2. create /etc/kernel/postinst.d/zz-update-systemd-boot (see below, and make sure it's 0755 mode) and run it
  3. reinstall the kernel package so it triggers kernel-install & friends
  4. reboot
#!/bin/bash
#
# This is a simple kernel hook to populate the systemd-boot entries
# whenever kernels are added or removed.
#
# Our kernels.
KERNELS=()
FIND="find /boot -maxdepth 1 -name 'vmlinuz-*' -type f -print0 | sort -rz"
while IFS= read -r -u3 -d $'\0' LINE; do
KERNEL=$(basename "${LINE}")
KERNELS+=("${KERNEL:8}")
done 3< <(eval "${FIND}")
# There has to be at least one kernel.
if [ ${#KERNELS[@]} -lt 1 ]; then
echo -e "\e[2msystemd-boot\e[0m \e[1;31mNo kernels found.\e[0m"
exit 1
fi
LATEST="${KERNELS[@]:0:1}"
echo -e "\e[2msystemd-boot\e[0m \e[1;32m${LATEST}\e[0m"
objcopy \
--add-section .osrel=/etc/os-release --change-section-vma .osrel=0x20000 \
--add-section .cmdline=/etc/kernel/cmdline --change-section-vma .cmdline=0x30000 \
--add-section .linux=/boot/vmlinuz-${LATEST} --change-section-vma .linux=0x2000000 \
--add-section .initrd=/boot/initrd.img-${LATEST} --change-section-vma .initrd=0x3000000 \
/usr/lib/systemd/boot/efi/linuxx64.efi.stub \
/boot/efi/EFI/Linux/ubuntu.efi
# Success!
exit 0
@mangkoran
Copy link

@gdamjan Thank you that fixed it. Is libfreetype6 intended to be removed (based on your gist)?

@Novantric
Copy link

Novantric commented May 22, 2025

Thanks! Just installing the packages worked flawlessly on 25.04, and after that I was able to purge and hold grub2 without issue (aside from libfreetype6 as mentioned above).
Any way to get secure boot working?

@txhammer68
Copy link

txhammer68 commented May 23, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment