performance2024-09-20·12 min·267/348

Linux Boot Time Optimization: Faster Server Startup

Techniques to reduce Linux boot time, including systemd analysis, service optimization, and kernel parameter tuning for faster server startup.

Introduction

A Linux server that takes several minutes to boot is a problem, especially in auto-scaling environments or disaster recovery scenarios. Slow boot times are often caused by services starting sequentially, unnecessary modules loading, or misconfigured dependencies. This post covers how to analyze and optimize Linux boot time for faster startup.

Environment

The examples use Ubuntu 22.04 LTS with systemd, where the initial boot time was measured at 45 seconds with a target of under 15 seconds.

systemd-analyze
# Startup finished in 30.123s (kernel) + 14.876s (userspace) = 45.000s

Problem

The server takes 45 seconds to boot, which is too long for a web server that should be responsive quickly. Critical services like nginx and PostgreSQL are delayed by slower-starting services.

systemd-analyze blame | head -10
# 15.234s cloud-init.service
# 12.111s snapd.service
#  8.456s NetworkManager-wait-online.service
#  5.678s apt-daily.service
#  4.321s ufw.service

Analysis

systemd-analyze provides several tools to identify bottlenecks:

# Critical chain shows the longest dependency path
systemd-analyze critical-chain
# The time after the product or unit is most likely started.
# multi-user.target @14.876s
# └─nginx.service @14.234s +641ms
# └─postgresql.service @13.456s +778ms
# └─network-online.target @12.345s
#   └─NetworkManager-wait-online.service @8.456s +3.889s

# Plot SVG timeline
systemd-analyze plot > boot-timeline.svg

Solution

1. Disable unnecessary services

# List all enabled services
systemctl list-unit-files --state=enabled

# Disable services you do not need
sudo systemctl disable cloud-init.service
sudo systemctl disable snapd.service
sudo systemctl disable apt-daily.service
sudo systemctl disable ufw.service

2. Skip NetworkManager wait-online

If your applications can start before network is fully online:

sudo systemctl disable NetworkManager-wait-online.service

3. Parallel service startup

systemd starts services in parallel when possible. Ensure proper dependency configuration:

# Check dependencies
systemctl list-dependencies nginx.service

# Remove unnecessary dependencies
# In /etc/systemd/system/myapp.service
[Unit]
# Do NOT use Requires= for non-critical services
Wants=postgresql.service
After=postgresql.service

4. Reduce kernel boot parameters

# /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nofb nomodeset"

# Remove unnecessary kernel modules
sudo nano /etc/modules
# Only essential modules here
# Update GRUB
sudo update-grub

5. Use systemd-analyze to identify slow services

# Find services that take too long
systemd-analyze blame | head -20

# Check a specific service's startup time
systemd-analyze security nginx.service

6. Use readahead or systemd-readahead

# systemd-readahead collects boot data
sudo systemd-readahead analyze

# Replay collected data
sudo systemd-readahead replay

7. Optimize filesystem mounting

# /etc/fstab - use noatime for non-critical partitions
/dev/sda1 / ext4 defaults,noatime 0 1
/dev/sda2 /var ext4 defaults,noatime,nodiratime 0 2

# Use tmpfs for temporary directories
tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
tmpfs /var/log tmpfs defaults,noatime,size=1G 0 0

8. Reduce initramfs size

# Check initramfs size
ls -lh /boot/initrd.img-*
# -rw-r--r-- 1 root root 45M Jan 20 10:00 /boot/initrd.img-5.15.0-91-generic

# Edit initramfs modules
sudo nano /etc/initramfs-tools/modules
# Only essential modules

# Regenerate initramfs
sudo update-initramfs -u

9. Use faster systemd targets

# Boot to a minimal target first
sudo systemctl set-default multi-user.target

# For headless servers, do not start graphical.target
sudo systemctl mask graphical.target

10. Measure the improvement

# Before optimization
systemd-analyze
# Startup finished in 30.123s (kernel) + 14.876s (userspace) = 45.000s

# After optimization
systemd-analyze
# Startup finished in 8.234s (kernel) + 6.766s (userspace) = 15.000s

11. Advanced: systemd-analyze security

# Find services with poor security that slow boot
systemd-analyze security --no-pager | head -10
# UNIT                       EXPOSURE PREDIRITE
# cloud-init.service              9.6 UNSAFE
# snapd.service                   9.4 UNSAFE
# apt-daily.service               9.1 UNSAFE

12. Use systemd-pstore for faster hardware initialization

# Disable if not needed
sudo systemctl mask systemd-pstore.service

13. Optimize module loading

# Check which modules load at boot
lsmod

# Blacklist unnecessary modules
echo "blacklist nouveau" | sudo tee /etc/modprobe.d/blacklist-custom.conf
echo "blacklist snd" | sudo tee -a /etc/modprobe.d/blacklist-custom.conf

# Rebuild module dependencies
sudo depmod -a

14. Use systemd-firstboot

# Skip first-boot setup on new instances
sudo touch /etc/firstboot-done

Lessons Learned

Boot time optimization is an iterative process. Start with systemd-analyze blame to identify the biggest offenders, then disable unnecessary services and optimize dependencies. For cloud instances, the biggest gains often come from removing cloud-init and snapd if they are not needed. Always measure before and after changes. And remember that faster boot times improve disaster recovery and auto-scaling responsiveness.


This blog does not accept any external sponsorships, affiliate marketing, or ad revenue.