init Command in Linux



The init program in Linux is the first user-space program executed by the kernel during the boot process. It is responsible for initializing the system and managing processes.

The init program serves as the parent of all processes in the system. The kernel launches it, and it is responsible for starting all other processes. Additionally, init takes over any processes that lose their original parent process, ensuring they are properly cleaned up when they finish running.

Apart from initializing the system and managing system processes, init is also used as a command to change the system run levels.

Table of Contents

Here is a comprehensive guide to the options available with the init command −

Using init Program as Command

The init program can also be used as a command by users with root privileges to change run levels. The init command can vary in behavior between distributions since systemd is the current standard in many.

Since modern Linux distributions use systemd, the relevance of the init command has diminished.

Syntax of init Command

The syntax of the Linux init command is as follows −

init [options]

The [options] field is used to specify various run levels.

Linux Run Levels

Run levels are predefined states in Linux that determine which processes and services are active at a given time. Run levels provide a way to define the state of the system and the services that should be running. They help manage how the system starts up, runs, and shuts down.

Note that in modern Linux distributions using systemd, the concept of run levels has been largely replaced by targets. For example, multi-user.target corresponds to run level 3, and graphical.target corresponds to run level 5. In modern Linux distributions, the init command will change the corresponding target.

The run levels can be changed using the init command. It runs the corresponding script/target to switch the run level.

The various run levels that can be changed with the init command are listed below −

Run Level Mode Systemd Target Description
0 Halt poweroff.target It shuts down the system
1 Single User Mode rescue.target It is a maintenance mode for system recovery and troubleshooting, allowing only the root user to log in
2 Multi User Mode multi-user.target It allows multiple users to log in but does not start networking services
3 Multi User Mode with Networking multi-user.target It allows multiple users to log in and starts networking services, commonly used for servers
4 Undefined <custom target> It is unused or defined by the system administrator for specific purposes
5 X11 graphical.target It is multi-user mode with a graphical user interface (GUI), starting the X server for graphical environments
6 Reboot reboot.target It reboots the system safely, restarting all processes from the beginning

To check the current run level, use the following command −

who -r
init Command in Linux1

To check the current target, use the following command −

systemctl get-default
init Command in Linux2

All the targets can be listed, using the following command −

systemctl list-units --type=target
init Command in Linux3

To list all, including the inactive targets append --all in the above command.

Examples of init Command in Linux

This section demonstrates the usage of the init command in Linux to change the run level.

Shutting Down the System

To shut down the system using the init command, use the run level 0.

sudo init 0

Rebooting the System

To reboot the system, use the run level 6 with the init command −

sudo init 6

Enabling the Rescue Mode

To enable the rescue mode, use the run level 1. It is primarily used for troubleshooting. It is a text-only mode equivalent to a single-user mode.

sudo init 1

Enabling the Graphical Mode

To enable the graphical mode, use the run level 5. If the single-user mode is enabled, it can be used to switch to the graphical run level.

sudo init 5

Changing Run Levels using systemctl Command

The systemctl isolate command is used in Linux distributions that use the systemd init system to change the target.

sudo systemctl isolate <target>

For example, to enter the single user mode, use the systemctl command in the following way −

sudo systemctl isolate rescue.target

Conclusion

The init program in Linux is the first user-space program executed by the kernel, managing system initialization and serving as the parent for all processes. Although its relevance has decreased with the rise of systemd in modern distributions, the init command remains important for changing run levels that determine the state of system processes and services.

Understanding the init command, including its syntax and usage for changing system states like shutdown, reboot, and rescue mode, is essential for effective system administration.

Advertisements