Init Systems

Exploring init systems including systemd and SysV init for service management and system initialization

Init systems are the first processes started by the Linux kernel during boot. They manage system initialization, service startup/shutdown, and process supervision throughout the system’s lifecycle. Modern Linux distributions primarily use systemd, though alternatives like SysV init and OpenRC exist.

Key Concepts

  • PID 1: The init system always runs as process ID 1
  • Service Management: Starting, stopping, and monitoring services
  • Dependency Management: Handling service dependencies and boot order
  • Target/Runlevel: Different system states (multi-user, graphical, rescue)
  • Unit Files: Configuration files defining services and their behavior
  • Socket Activation: Starting services on-demand when needed

Command Syntax

systemd (most common)

systemctl [command] [unit-name]

  • Primary tool for managing systemd services
  • Controls service states and system targets

SysV Init (legacy)

service [service-name] [action]

  • Traditional service management command
  • Still available on many systems for compatibility

Common Options

systemd commands

start - Start a service stop - Stop a service
restart - Restart a service reload - Reload service configuration enable - Enable service at boot disable - Disable service at boot status - Show service status list-units - List all units

Practical Examples

Example 1: Check systemd status

1
systemctl status

Shows overall system state and recent log entries

Example 2: Manage a service

1
2
3
4
5
6
7
8
# Start nginx service
sudo systemctl start nginx

# Check if it's running
systemctl status nginx

# Enable it to start at boot
sudo systemctl enable nginx

Basic service lifecycle management

Example 3: View all services

1
systemctl list-units --type=service

Lists all loaded service units and their states

Example 4: Change system target

1
2
3
4
5
# Switch to multi-user target (no GUI)
sudo systemctl isolate multi-user.target

# Switch to graphical target
sudo systemctl isolate graphical.target

Changes the current system state

Example 5: View service logs

1
2
3
4
5
# View logs for specific service
journalctl -u nginx

# Follow logs in real-time
journalctl -u nginx -f

Examine service-specific log entries

Use Cases

  • System Boot: Starting essential services during startup
  • Service Management: Daily administration of system services
  • Troubleshooting: Diagnosing boot issues and service failures
  • Automation: Scripting service management tasks
  • Resource Management: Controlling service resource usage
  • Security: Managing service permissions and isolation

journalctl - View systemd logs systemd-analyze - Analyze boot performance loginctl - Manage user sessions hostnamectl - Control system hostname timedatectl - Manage system time/date localectl - Configure system locale

Tips & Troubleshooting

Common Issues

  • Service won’t start: Check systemctl status service-name and journalctl -u service-name
  • Boot hangs: Use systemd-analyze critical-chain to find bottlenecks
  • Service conflicts: Review unit file dependencies

Performance Tips

  • Use systemd-analyze blame to identify slow-starting services
  • Disable unnecessary services with systemctl disable service-name
  • Consider socket activation for rarely-used services

Security Notes

  • Services run with minimal privileges by default in systemd
  • Use systemctl edit service-name to safely modify service configs
  • Regular audit of enabled services recommended

Legacy System Notes

  • On SysV systems, check /etc/init.d/ for service scripts
  • Runlevels 0-6 correspond to different system states
  • Use chkconfig or update-rc.d for SysV service management