init Command: Tutorial & Examples

Managing run levels and system states in Linux

The init command is the first process started by the Linux Kernel during system boot. It is responsible for initializing the system, managing runlevels, and controlling the startup and shutdown of services. Understanding how to use init is essential for system administrators who manage the boot process, troubleshoot system states, or require precise control over system modes.

History And Evolution Of init

The init command has its origins in the early Unix operating systems, where it served as the first process (PID 1) to execute after the kernel finished booting. Traditionally, init is part of the SysVinit system, which organizes system startup and shutdown through runlevels defined in the /etc/inittab file. Over time, alternative init systems such as Upstart and systemd have emerged, offering more advanced features and parallel service startup. However, SysVinit and its init command remain relevant in many Linux distributions, especially for embedded systems and legacy environments.

How init Command Works

The init process is the parent of all other processes on the system. Upon starting, it reads the configuration from /etc/inittab to determine the default runlevel and the corresponding scripts or programs to execute. Runlevels define the state of the machine, such as single-user mode, multi-user mode, or system halt.

When the system changes runlevels—whether at boot, shutdown, or manually invoked—init executes the appropriate scripts to start or stop services accordingly. It monitors processes it spawns and can restart them if configured to do so.

Administrators can request runlevel changes by invoking the init command with a runlevel number, causing it to transition the system to the specified state.

What init Is Used For

  • Controlling system runlevels and modes.
  • Starting and stopping system services during boot and shutdown.
  • Switching system states for maintenance or troubleshooting.
  • Bringing the system into single-user mode for recovery.
  • Halting or rebooting the system safely.

Understanding init is crucial for managing the system lifecycle, especially in environments where graphical interfaces are absent or minimal.

Common init Runlevels And Parameters

The init command accepts a single argument specifying the target runlevel. The standard runlevels are:

  • 0: Halt the system (shutdown)
  • 1: Single-user mode (rescue mode, minimal services)
  • 2: Multi-user mode without networking (varies by distribution)
  • 3: Full multi-user mode with networking (text mode)
  • 4: Undefined/unused (customizable)
  • 5: Multi-user mode with graphical interface (if applicable)
  • 6: Reboot the system

Note that the exact meaning of runlevels 2-5 may vary among distributions.

How To Use init Command With Examples

Here are common examples demonstrating the usage of init:

init 0

Initiates system shutdown and powers off.

init 6

Initiates system reboot.

init 1

Switches to single-user mode for maintenance.

Example: Switching to runlevel 3 (multi-user text mode):

init 3

This changes the system to full multi-user mode with networking.

Example output is typically not displayed because init changes the system state. However, you will observe system behavior such as processes stopping, services starting, or system powering off.

Technical Background And Configuration

The behavior of init is governed by the /etc/inittab file. This file contains entries specifying what actions to perform for each runlevel. A typical inittab entry looks like:

id:3:initdefault:

This line sets the default runlevel to 3.

Other entries define scripts or programs to run at specific runlevels. For example:

rc:2345:wait:/etc/rc.d/rc

Here, the script /etc/rc.d/rc is executed when entering runlevels 2, 3, 4, or 5.

Administrators can customize the inittab to modify system behavior during state transitions. After editing, a reload of init is typically required for changes to take effect.

Common Errors And Troubleshooting

  • Incorrect runlevel number: Using a runlevel not defined in inittab will result in an error.
  • Failure to reach desired runlevel: This often occurs if scripts linked to the runlevel fail. Checking logs in /var/log can help identify issues.
  • System hangs during runlevel change: Can indicate a problematic init script or hardware issue.
  • Permission denied errors: Running init requires superuser privileges; use sudo init <runlevel> if necessary.

For troubleshooting, switching to single-user mode (init 1) allows repair of configuration files or removal of problematic services.

Potential Problems And Pitfalls

  • Using init improperly can cause system instability or data loss, especially if runlevels are switched abruptly.
  • Many modern Linux systems use systemd as the init system; invoking init directly may have limited effect or be overridden.
  • Editing /etc/inittab incorrectly can prevent the system from booting properly.
  • Some distributions do not fully support all traditional runlevels.

Alternatives And Related Commands

  • systemctl: Used on systemd-based systems for managing system states and services.
  • telinit: A symlink or wrapper to init for changing runlevels.
  • shutdown: Command to safely halt or reboot the system.
  • runlevel: Displays the current and previous runlevels.
  • service: Starts or stops individual services, often used alongside init.

Understanding these commands is important as many modern systems have moved away from traditional init.

Tips And Best Practices

  • Always back up /etc/inittab before making changes.
  • Use single-user mode (init 1) for maintenance tasks requiring minimal services.
  • Prefer shutdown or reboot commands for safe system power management.
  • Verify your distribution's init system before using init commands.
  • When scripting, use telinit for runlevel changes to ensure compatibility.

Scripting And Automation With init

You can automate system runlevel changes in scripts for maintenance or deployment. For example, a script to reboot the system after a task:

#!/bin/bash
# Perform maintenance task
echo "Starting maintenance..."
# ... task commands ...
echo "Rebooting now."
/sbin/init 6

Ensure scripts run with appropriate privileges.

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA