Linux init Command

Linux 命令大全Linux Command Encyclopedia


What is the init Command

initIt is one of the most important processes in the Linux system. It is the first user-level process (PID=1) created by the kernel after system boot, responsible for starting and managing all other processes in the system.

Key Features

  • Process Management: Serves as the parent process of all processes
  • Runlevel Control: Manages different runtime states of the system
  • Service Management: Starts and stops system services
  • System Initialization: Executes startup scripts and configuration

Basic Syntax of the init Command

init [运行级别]

Runlevel Explanation

The Linux system defines 7 standard runlevels:

Runlevel Description Typical Use
0 Shutdown System shutdown state
1 Single-user mode System maintenance / root privileges
2 Multi-user mode (without NFS) Basic multi-user mode
3 Full multi-user mode Standard command-line interface
4 Reserved User-defined
5 Graphical interface mode Multi-user mode with GUI
6 Reboot System reboot

Practical Applications of the init Command

1. View Current Runlevel

Example

who -r
# or
runlevel

2. Switch Runlevel

Example

init 3  # Switch to multi-user command-line mode
init 5  # Switch to graphical interface mode
init 0  # Shutdown
init 6  # Reboot the system

3. Emergency Handling

Example

init 1  # Enter single-user maintenance mode (requires root privileges)

The Evolution of init in Modern Linux Systems

1. System V init

Traditional initialization system, uses the /etc/inittab configuration file

Example

# Example of a typical inittab entry
id:3:initdefault:  # Default runlevel
si::sysinit:/etc/rc.d/rc.sysinit  # System initialization script

2. Upstart (Used in Early Ubuntu Versions)

Event-driven init system

3. systemd (Modern Mainstream Distributions)

Example

systemctl isolate multi-user.target  # Equivalent to init 3
systemctl isolate graphical.target  # Equivalent to init 5

Hands-on Practice

Exercise 1: Runlevel Switching Experiment

Switch from graphical interface to command-line mode

init 3

Observe service changes, then switch back to graphical interface

init 5

Exercise 2: System Maintenance Mode Experience

Enter single-user mode

init 1

Perform file system check

fsck /dev/sda1

Notes

  1. Permission requirements: The init command usually requires root privileges
  2. Use with caution: Directly using init 0 or init 6 may cause loss of unsaved data
  3. System differences: Different distributions may have different init implementations
  4. Service impact: Switching runlevels will stop/start related services

FAQ

Q: Why doesn't my system have an /etc/inittab file?
A: Systems using systemd no longer need inittab; the configuration method has changed

Q: How to set the default runlevel?
For systemd systems:

systemctl set-default multi-user.target  # 相当于运行级别3

Q: What is the difference between the init and shutdown commands?
Both ultimately shut down the system, but shutdown provides more options (such as delayed shutdown, broadcast notifications, etc.)

Linux 命令大全Linux Command Encyclopedia

Other Extensions