runlevel

systemLinux/Unix
The runlevel command is one of the most frequently used commands in Linux/Unix-like operating systems. runlevel Display the current and previous system runlevel

Quick Reference

Command Name:

runlevel

Category:

system

Platform:

Linux/Unix

Basic Usage:

runlevel [options] [arguments]

Common Use Cases

    Syntax

    runlevel [options]

    Options

    Option Description
    --help Display help message and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the runlevel command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    runlevel
    Display the current runlevel of the system.
    # Advanced Examples Advanced
    who -r
    Alternative way to check the runlevel.
    runlevel < /var/run/utmp Explicitly specify the utmp file to read from. runlevel | awk '{print $2}' Extract only the current runlevel number. if [ "$(runlevel | cut -d ' ' -f 2)" = "3" ]; then echo "System is in multi-user mode without GUI" fi Check if the system is running in runlevel 3. previous_level=$(runlevel | awk '{print $1}') current_level=$(runlevel | awk '{print $2}') echo "System changed from runlevel $previous_level to $current_level" Extract and display both previous and current runlevels. init=$(cat /proc/1/comm) if [ "$init" = "systemd" ]; then echo "Using systemd, runlevel is legacy concept" systemctl get-default else runlevel fi Check if system uses systemd and show appropriate information. case $(runlevel | cut -d ' ' -f 2) in 0) echo "System is halting" ;; 1) echo "System is in single-user mode" ;; 3) echo "System is in multi-user mode" ;; 5) echo "System is in graphical mode" ;; 6) echo "System is rebooting" ;; *) echo "Unknown runlevel" ;; esac Interpret the current runlevel with a descriptive message.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `runlevel` command is a traditional Unix/Linux utility that displays the current and previous system runlevel. Runlevels are a concept used in SysV init systems to define the state of the machine and what services should be operating. In classic Unix systems, runlevels are numbered from 0 to 6, with each number representing a different operating state: - **Runlevel 0**: System halt/shutdown - **Runlevel 1**: Single-user mode (also called maintenance mode or rescue mode) - **Runlevel 2**: Multi-user mode without network services - **Runlevel 3**: Full multi-user mode with networking (text-based console login) - **Runlevel 4**: Typically unused, but can be user-defined - **Runlevel 5**: Multi-user mode with networking and graphical login (X11) - **Runlevel 6**: System reboot The `runlevel` command reads the /var/run/utmp file (or /run/utmp on some systems) to determine the current and previous runlevels. It outputs this information in a simple format: first the previous runlevel (or "N" if there was no previous runlevel since boot), followed by the current runlevel. It's important to note that with the widespread adoption of systemd as the init system in most modern Linux distributions, the concept of runlevels has been largely replaced by "targets". While systemd maintains backward compatibility with the runlevel concept, it uses a more flexible target system underneath. For this reason, on systemd-based systems, you might want to use `systemctl get-default` or related commands for more accurate information about the system state. The runlevel mapping to systemd targets is approximately: - Runlevel 0 → poweroff.target - Runlevel 1 → rescue.target - Runlevel 2, 3, 4 → multi-user.target - Runlevel 5 → graphical.target - Runlevel 6 → reboot.target The `runlevel` command has few options because its function is straightforward. It's typically used in scripts that need to determine the current operating state of the system, particularly in init scripts that need to behave differently based on the runlevel. While `runlevel` is still available on most Linux distributions for backward compatibility, it's considered somewhat deprecated in favor of systemd commands on modern systems. Scripts designed for portability or that need to work across different init systems should consider checking for the init system type before relying exclusively on runlevel information.

    Related Commands

    These commands are frequently used alongside runlevel or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the runlevel command works in different scenarios.

    $ runlevel
    View All Commands