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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.