Linux System Administration

0 of 85 lessons complete (0%)

Starting up and Shutting Down

init and the inittab File

You don’t have access to this lesson

Please register or sign in to access the course content.

init is the first process started by the Linux kernel during booting and is the parent of all other processes. It is responsible for initializing the system and starting essential services and processes.

init

  • Role: init is responsible for setting up the environment in which other programs run, including starting and stopping system services and managing system run levels.
  • Run Levels: Traditional init systems use run levels to define the state of the machine, ranging from single-user mode to multi-user modes with or without networking.

inittab File

The inittab file is a configuration file used by the traditional SysV init system to define how the system should initialize and manage processes.

Init Runlevels in Unix-like Systems

Runlevels define the state of the system and determine which services and processes are active. The traditional SysV init system uses runlevels to manage different operating modes.

RunlevelDescriptionTypical UseExample
0HaltSystem shutdownsudo init 0 or sudo telinit 0
1Single-user modeMaintenance or repair (single-user)sudo init 1 or sudo telinit 1
2Multi-user mode without networkingMulti-user operations without networksudo init 2 or sudo telinit 2
3Multi-user mode with networkingMulti-user operations with networksudo init 3 or sudo telinit 3
4User-definableCustom configurations (usually unused)sudo init 4 or sudo telinit 4
5Multi-user mode with GUIMulti-user with graphical user interfacesudo init 5 or sudo telinit 5
6RebootSystem rebootsudo init 6 or sudo telinit 6

Use of init Command

  • Switching Runlevels: init is used to change the system’s runlevel, which dictates the state of system services and processes.
  • System Management: By switching runlevels, system administrators can perform tasks such as shutting down, rebooting, or transitioning between different operational states.
  • Maintenance: Single-user mode (runlevel 1) is often used for system maintenance and repair without interference from other users or processes.

The init system provides a fundamental way to control the state and configuration of a Unix-like system.

Example of a Complete inittab Configuration

# Default runlevel
id:3:initdefault:

# System initialization
si::sysinit:/etc/rc.d/rc.sysinit

# Runlevel 0 (halt)
l0:0:wait:/etc/rc.d/rc 0

# Runlevel 1 (single-user mode)
l1:1:wait:/etc/rc.d/rc 1

# Runlevel 2 (multi-user mode without networking)
l2:2:wait:/etc/rc.d/rc 2

# Runlevel 3 (multi-user mode with networking)
l3:3:wait:/etc/rc.d/rc 3

# Runlevel 4 (user-definable)
l4:4:wait:/etc/rc.d/rc 4

# Runlevel 5 (multi-user mode with GUI)
l5:5:wait:/etc/rc.d/rc 5

# Runlevel 6 (reboot)
l6:6:wait:/etc/rc.d/rc 6

Important Considerations

  • Editing inittab: Use a text editor with root permissions (e.g., sudo nano /etc/inittab or sudo vim /etc/inittab) to make changes.
  • Apply Changes: After modifying inittab, apply changes using telinit or init to re-read the configuration.
    • Example: sudo telinit q This command tells init to re-read the inittab file without rebooting the system.
  • Backup: Always back up the inittab file before making changes to avoid configuration issues.

The inittab file provides essential control over the system’s initialization process, runlevel management, and service startup.

In the traditional SysV init system, runlevel scripts are crucial for managing system initialization and state transitions. These scripts are located in various directories and are executed depending on the runlevel the system transitions to or from.

Runlevel Scripts Overview

  1. Location:
    • Scripts: Typically located in /etc/rc.d/, /etc/init.d/, or /etc/rcX.d/, where X is the runlevel.
  2. Script Directories:
    • /etc/init.d/: Contains the actual service scripts used to start, stop, and manage services.
    • /etc/rc.d/: Sometimes used to hold scripts that are executed during system startup and shutdown.
    • /etc/rcX.d/: Contains symbolic links to the scripts in /etc/init.d/, with names prefixed by S (start) or K (kill), indicating whether they should be started or stopped in that runlevel.

Runlevel Script Structure

  1. /etc/init.d/:
    • Purpose: Contains scripts to start and stop system services.
    • Example: /etc/init.d/sshd (manages the SSH daemon).
  2. /etc/rcX.d/:
    • Purpose: Contains symbolic links to scripts in /etc/init.d/ that define actions for a specific runlevel.
    • Example:
      • Start Links: S01sshd (starts the SSH daemon).
      • Stop Links: K01sshd (stops the SSH daemon).

Runlevel Script Execution

  • Runlevel 0 (Halt):
    • Scripts: K* scripts are executed to stop services.
    • Example: K01sshd stops the SSH daemon during shutdown.
  • Runlevel 1 (Single-User Mode):
    • Scripts: S* scripts are executed to start services, minimal set for maintenance.
    • Example: S10network might start network services if required.
  • Runlevel 2 (Multi-User Mode Without Networking):
    • Scripts: S* scripts start services for multi-user operations without networking.
    • Example: S20cron starts the cron daemon.
  • Runlevel 3 (Multi-User Mode With Networking):
    • Scripts: S* scripts start services with networking enabled.
    • Example: S30network starts network services.
  • Runlevel 4 (User-Definable):
    • Scripts: Custom configuration, can be used for special setups.
    • Example: Custom scripts for additional services.
  • Runlevel 5 (Multi-User Mode With GUI):
    • Scripts: S* scripts start services with a graphical user interface.
    • Example: S40xdm starts the X Display Manager.
  • Runlevel 6 (Reboot):
    • Scripts: K* scripts are executed to stop services before rebooting.
    • Example: K01sshd stops the SSH daemon.

Example of Managing Runlevel Scripts

  1. Create a New Service Script:
    • Create a script in /etc/init.d/: sudo nano /etc/init.d/myservice
    • Add executable permissions: sudo chmod +x /etc/init.d/myservice
  2. Create Symbolic Links in Runlevel Directories:
    • Link to /etc/rc3.d/ for runlevel 3: sudo ln -s /etc/init.d/myservice /etc/rc3.d/S90myservice
    • Link to /etc/rc0.d/ for runlevel 0: sudo ln -s /etc/init.d/myservice /etc/rc0.d/K10myservice
  3. Manage Services:
    • Start Service: sudo /etc/init.d/myservice start
    • Stop Service: sudo /etc/init.d/myservice stop

Summary

  • /etc/init.d/: Contains the service management scripts.
  • /etc/rcX.d/: Contains symbolic links to these scripts, specifying actions for each runlevel.
  • Runlevel Scripts: Define which services are started or stopped when changing runlevels.

These scripts ensure the system transitions smoothly between different operational states, maintaining proper service management across various runlevels.