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.
Runlevel | Description | Typical Use | Example |
---|---|---|---|
0 | Halt | System shutdown | sudo init 0 or sudo telinit 0 |
1 | Single-user mode | Maintenance or repair (single-user) | sudo init 1 or sudo telinit 1 |
2 | Multi-user mode without networking | Multi-user operations without network | sudo init 2 or sudo telinit 2 |
3 | Multi-user mode with networking | Multi-user operations with network | sudo init 3 or sudo telinit 3 |
4 | User-definable | Custom configurations (usually unused) | sudo init 4 or sudo telinit 4 |
5 | Multi-user mode with GUI | Multi-user with graphical user interface | sudo init 5 or sudo telinit 5 |
6 | Reboot | System reboot | sudo 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
orsudo vim /etc/inittab
) to make changes. - Apply Changes: After modifying
inittab
, apply changes usingtelinit
orinit
to re-read the configuration.- Example:
sudo telinit q
This command tellsinit
to re-read theinittab
file without rebooting the system.
- Example:
- 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
- Location:
- Scripts: Typically located in
/etc/rc.d/
,/etc/init.d/
, or/etc/rcX.d/
, whereX
is the runlevel.
- Scripts: Typically located in
- 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 byS
(start) orK
(kill), indicating whether they should be started or stopped in that runlevel.
Runlevel Script Structure
- /etc/init.d/:
- Purpose: Contains scripts to start and stop system services.
- Example:
/etc/init.d/sshd
(manages the SSH daemon).
- /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).
- Start Links:
- Purpose: Contains symbolic links to scripts in
Runlevel Script Execution
- Runlevel 0 (Halt):
- Scripts:
K*
scripts are executed to stop services. - Example:
K01sshd
stops the SSH daemon during shutdown.
- Scripts:
- 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.
- Scripts:
- Runlevel 2 (Multi-User Mode Without Networking):
- Scripts:
S*
scripts start services for multi-user operations without networking. - Example:
S20cron
starts the cron daemon.
- Scripts:
- Runlevel 3 (Multi-User Mode With Networking):
- Scripts:
S*
scripts start services with networking enabled. - Example:
S30network
starts network services.
- Scripts:
- 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.
- Scripts:
- Runlevel 6 (Reboot):
- Scripts:
K*
scripts are executed to stop services before rebooting. - Example:
K01sshd
stops the SSH daemon.
- Scripts:
Example of Managing Runlevel Scripts
- 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
- Create a script in
- 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
- Link to
- Manage Services:
- Start Service:
sudo /etc/init.d/myservice start
- Stop Service:
sudo /etc/init.d/myservice stop
- Start Service:
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.