How to check if the system is in single-user mode in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check the operating state of a Linux system, specifically focusing on how to determine if the system is in single-user mode. You will explore traditional runlevels using the runlevel and who -r commands, and also examine the default system target in modern systemd environments using systemctl get-default. This hands-on experience will provide you with practical skills for understanding and verifying the system's operational state.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux/BasicSystemCommandsGroup -.-> linux/help("Command Assistance") linux/FileandDirectoryManagementGroup -.-> linux/which("Command Locating") linux/SystemInformationandMonitoringGroup -.-> linux/service("Service Managing") linux/UserandGroupManagementGroup -.-> linux/whoami("User Identifying") subgraph Lab Skills linux/help -.-> lab-558810{{"How to check if the system is in single-user mode in Linux"}} linux/which -.-> lab-558810{{"How to check if the system is in single-user mode in Linux"}} linux/service -.-> lab-558810{{"How to check if the system is in single-user mode in Linux"}} linux/whoami -.-> lab-558810{{"How to check if the system is in single-user mode in Linux"}} end

Check runlevel with runlevel

In this step, we'll explore the concept of runlevels in Linux and how to check the current runlevel using the runlevel command.

Runlevels represent different operating states of a Linux system. Each runlevel defines which services are started and stopped. For example, one runlevel might be for a multi-user system with a graphical interface, while another might be for a single-user mode used for maintenance.

While modern Linux distributions often use systemd targets instead of traditional runlevels, understanding runlevels is still valuable as you may encounter them in older systems or documentation.

Let's check the current runlevel of your LabEx environment. Open the terminal if you haven't already.

Type the following command and press Enter:

runlevel

You should see output similar to this:

N 5

Let's break down the output:

  • The first character (N in this case) indicates the previous runlevel. N means there was no previous runlevel (the system just started).
  • The second character (5 in this case) indicates the current runlevel.

In traditional SysVinit systems, common runlevels include:

  • 0: Halt (shut down the system)
  • 1: Single-user mode (for maintenance)
  • 2: Multi-user mode, without networking
  • 3: Multi-user mode, with networking (text console)
  • 4: Not used/user-definable
  • 5: Multi-user mode, with networking and a graphical interface
  • 6: Reboot (restart the system)

Your LabEx environment is running in runlevel 5, which corresponds to a multi-user system with a graphical desktop environment.

Understanding runlevels helps you troubleshoot system startup issues and manage system services.

Click Continue to proceed to the next step.

Verify runlevel with who -r

In the previous step, we used the runlevel command to check the current runlevel. Another way to get similar information is by using the who command with the -r option.

The who command is typically used to show who is logged into the system. However, with the -r option, it displays the current runlevel.

Let's try it out. In your terminal, type the following command and press Enter:

who -r

You should see output similar to this:

           run-level 5  <DATE> <TIME>

This output confirms that the system is currently in runlevel 5. The date and time indicate when the system entered this runlevel.

Comparing the output of runlevel and who -r, you can see that both commands provide information about the current runlevel, although the format differs. who -r gives a more descriptive output including the date and time.

Using different commands to achieve similar results is common in Linux. It's helpful to know multiple ways to get the information you need, as some commands might be available on certain systems while others are not, or one command might provide more detailed information than another.

Practice using both runlevel and who -r to become comfortable with checking the system's operating state.

Click Continue to move on to the next step.

Inspect default target with systemctl get-default

As mentioned earlier, modern Linux distributions often use systemd instead of the traditional SysVinit system. systemd uses "targets" which are similar in concept to runlevels but offer more flexibility.

A target is a group of systemd units (like services, mount points, sockets, etc.) that the system should start or stop to reach a desired state. The default target is the state the system boots into.

We can use the systemctl command to interact with systemd. The get-default subcommand shows the default target.

Note: Due to the nature of the LabEx environment running in a Docker container, the systemctl command might behave differently or show limited information compared to a full operating system installation. However, we can still use it to understand the concept of default targets.

In your terminal, type the following command and press Enter:

systemctl get-default

You should see output similar to this:

graphical.target

This output indicates that the default target for this system is graphical.target. This target typically includes all the services needed for a multi-user system with a graphical desktop environment, which aligns with the runlevel 5 we saw earlier.

Other common targets include:

  • multi-user.target: Similar to runlevel 3, a multi-user system without a graphical interface.
  • rescue.target: A single-user mode for system recovery.
  • reboot.target: Used for rebooting the system.
  • poweroff.target: Used for shutting down the system.

While you might not be able to change the default target in this LabEx environment, understanding how to check it with systemctl get-default is a fundamental skill for managing systemd-based systems.

You have now learned three different ways to check the system's operating state or its default boot state: runlevel, who -r, and systemctl get-default.

Click Continue to complete this lab.

Summary

In this lab, we learned how to check the system's operating state in Linux. We began by exploring the concept of runlevels, which represent different system states, and used the runlevel command to determine the current runlevel. We understood the meaning of the output, including the previous and current runlevels, and reviewed the common runlevels in traditional SysVinit systems.

We then learned an alternative method to verify the runlevel using the who -r command, which provides similar information about the system's runlevel. Finally, we examined how modern Linux distributions, which often use systemd, manage system states through targets instead of traditional runlevels, and used the systemctl get-default command to identify the default target.