Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Explaining Runlevel in Linux
3.
Checking the Current Runlevel
3.1.
Using the runlevel Command
3.2.
Using the who Command
3.3.
Changing the Runlevel in Linux
4.
Setting the Default Runlevel
4.1.
Example for SysVinit (e.g., older versions of Debian):
4.2.
Example for systemd (e.g., newer versions of Ubuntu):
5.
How to Check the Runlevel of Your Linux System
5.1.
Using the runlevel Command
5.2.
Using the who Command
6.
How to Change the Runlevel in Linux
6.1.
Changing Runlevel Temporarily
6.2.
Changing Runlevel Permanently
6.2.1.
Example for SysVinit (Older Systems):
6.2.2.
Example for systemd (Newer Systems):
7.
Setting the Default Runlevel for Your Linux System
7.1.
For SysVinit Systems
7.2.
For systemd Systems
8.
Frequently Asked Questions
8.1.
What Happens If I Set an Incorrect Runlevel?
8.2.
Can I Create Custom Runlevels in Linux?
8.3.
Is It Safe to Change Runlevels While the System Is Running?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Runlevel in Linux

Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

Runlevels in Linux are a fundamental part of the system's initialization process and management. They define the state of the machine after booting and determine which services or processes should be running. Understanding runlevels is crucial for system administrators and users who need to manage Linux systems effectively.

Runlevel in Linux

Linux, unlike other operating systems, provides multiple states (runlevels) for different purposes, such as single-user mode, multi-user mode, and graphical mode. These runlevels are designed to give users control over the behavior of their system, especially during startup and shutdown.

Explaining Runlevel in Linux

Runlevel is a state of init, the parent of all processes, that defines what processes or services are running on the system. When a Linux system boots, it initializes different services and processes as specified in its runlevel configuration. Each runlevel has a specific purpose and set of services associated with it.

The standard Linux kernel supports seven different runlevels, numbered from 0 to 6. These runlevels can include modes for shutting down, single-user mode, multi-user mode with or without network services, and graphical mode. Here's a table of standard Linux runlevels:

Runlevel Mode Description
0 Halt Shuts down the system.
1 Single-user mode Used for maintenance or emergency repairs.
2 Multi-user mode without NFS Does not configure network file systems or network services.
3 Multi-user mode with Networking The standard runlevel for most Linux-based servers.
4 Undefined Reserved for custom configurations.
5 Graphical mode Starts the system normally with a graphical user interface.
6 Reboot Reboots the system.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Checking the Current Runlevel

To manage and troubleshoot a Linux system effectively, it's important to know how to check the current runlevel. This can be done using various commands, such as runlevel and who.

Using the runlevel Command

The runlevel command displays the current runlevel of your Linux system. It shows two characters: the previous runlevel and the current runlevel. If the system is freshly booted, the previous runlevel is represented as 'N'.

Example:

$ runlevel


Output

N 3


This output indicates that the system did not have a previous runlevel ('N') and is currently in runlevel 3, which is multi-user mode with networking.

Using the who Command

Another way to check the current runlevel is by using the who command with the -r flag, which stands for 'runlevel'.

Example:


$ who -r


Output       

  run-level 3  2021-04-03 15:22


This output shows that the system is running in runlevel 3 as of the given date and time.

Changing the Runlevel in Linux

Sometimes, you may need to change the runlevel of your Linux system, for example, for maintenance or to troubleshoot issues. This can be done using various methods.

Example:

To change to runlevel 1 (single-user mode), you can use the init command:

$ sudo init 1


This command will bring the system down to runlevel 1, where it operates in single-user mode.

Setting the Default Runlevel

The default runlevel is defined in a system configuration file, which varies depending on the Linux distribution. Commonly, this file is /etc/inittab in SysVinit systems or a symbolic link in systemd systems.

Example for SysVinit (e.g., older versions of Debian):

In /etc/inittab, find the line that starts with id: and set the default runlevel:

id:3:initdefault:


This sets the default runlevel to 3 (multi-user mode with networking).

Example for systemd (e.g., newer versions of Ubuntu):

With systemd, you use the systemctl command to set the default target:

$ sudo systemctl set-default multi-user.target


This sets the default runlevel to multi-user mode (equivalent to runlevel 3).

How to Check the Runlevel of Your Linux System

Using the runlevel Command

The runlevel command in Linux is a straightforward, efficient way to determine the current runlevel of your system. This command is particularly useful for system administrators and users who need to verify the system's state before performing certain operations.

When you run the runlevel command, it outputs two characters. The first character represents the previous runlevel, and the second one is the current runlevel. If the system has been started freshly and hasn't changed runlevels since boot, the previous runlevel is denoted as 'N'.

Example and Explanation:

Suppose you run the command:

$ runlevel


You might get an output like:

N 5


Here, 'N' signifies that there hasn't been a previous runlevel since the system started, and '5' indicates that the current runlevel is 5, typically associated with a graphical user interface.

Using the who Command

The who command, often used to list users currently logged into the system, can also help determine the current runlevel. By using the -r flag, which stands for 'runlevel', you can get the current runlevel information.

Example and Explanation:

Executing the command:

$ who -r


Might produce an output similar to:

run-level 3  2023-11-17 08:35


In this case, the output indicates that the system is currently at runlevel 3, which is the standard mode for most Linux servers, and it shows the last time this runlevel was entered.

How to Change the Runlevel in Linux

Changing Runlevel Temporarily

To change the runlevel temporarily, you can use the telinit or init command. This change will last until the system is rebooted.

Example and Explanation:

For instance, to switch to runlevel 1 (single-user mode), you might use:

$ sudo init 1


This command will move the system to runlevel 1, allowing you to perform system maintenance tasks without interference from other users or network services.

Changing Runlevel Permanently

For a permanent change, you would modify the system's configuration file or use a system management daemon like systemd.

Example for SysVinit (Older Systems):

In SysVinit, the default runlevel is set in the /etc/inittab file. You would edit this file to change the line that looks something like:

id:3:initdefault:


Changing the number to the desired runlevel sets it as the default after booting.

Example for systemd (Newer Systems):

In systemd-based systems, you use the systemctl command to set the default target.

$ sudo systemctl set-default graphical.targe


This command sets the default runlevel to graphical mode (equivalent to runlevel 5).

Setting the Default Runlevel for Your Linux System

The default runlevel determines how your system boots up and what services it starts by default. Setting it correctly is crucial for ensuring your system behaves as expected after a reboot.

For SysVinit Systems

In older systems that use SysVinit, the /etc/inittab file is where the default runlevel is specified.

Example:

To set the default runlevel to 3, edit the /etc/inittab file to have the following line:

id:3:initdefault:

For systemd Systems

In systemd-based systems, the default runlevel is set by creating a symbolic link to the desired target.

Example:

To set the default runlevel to multi-user (runlevel 3), use:

$ sudo systemctl set-default multi-user.target


This command sets the default runlevel to be multi-user mode, which is ideal for servers and systems that don't require a graphical interface.

Frequently Asked Questions

What Happens If I Set an Incorrect Runlevel?

Setting an incorrect runlevel can lead to various issues, depending on what you set it to. For example, setting your system to runlevel 0 or 6 will shut down or reboot your system, respectively. Setting a server to runlevel 5, which starts the graphical interface, might lead to unnecessary resource consumption. Always ensure you understand the implications of each runlevel before making a change.

Can I Create Custom Runlevels in Linux?

Yes, in Linux distributions that use SysVinit, you can create custom runlevels. This is usually done by editing the runlevel scripts located in /etc/rc.d or /etc/init.d. However, with systemd, customizing targets (systemd's equivalent of runlevels) is more complex and involves creating and managing custom target units.

Is It Safe to Change Runlevels While the System Is Running?

Generally, it's safe to change runlevels while the system is running, but it depends on what services are currently in use and what runlevel you're switching to. For example, moving to a single-user mode (runlevel 1) will stop many services and may terminate active user sessions. Always ensure that critical services and data are secured before changing runlevels.

Conclusion

Runlevels in Linux are a core concept for understanding how a Linux system operates after booting. They define the operational state of the system, determining which processes and services are active. This article covered the standard runlevels supported by the Linux kernel, how to check the current runlevel using the runlevel and who commands, and methods to change the runlevel both temporarily and permanently. Understanding and correctly managing runlevels is essential for effective Linux system administration, especially for tasks related to system maintenance, performance tuning, and troubleshooting.

Recommended Reads:  features of linux operating system

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
host SSH Error in Linux
Next article
gzip Command in Linux
Live masterclass