How to Change Runlevels (targets) in SystemD?


SystemD is a system and service manager for Linux operating systems. It manages the boot process, system services, and provides a centralized way to manage processes. One of the key features of SystemD is its use of runlevels (targets) to define the state of the system at startup.

Runlevels (targets) are a set of services and processes that are started or stopped depending on the current state of the system. Understanding how to change runlevels (targets) is an important skill for any Linux administrator.

Understanding Runlevels (Targets)

In the traditional Unix system, runlevels were used to determine which set of services would run on startup. SystemD replaces these traditional runlevels with more flexible and configurable targets. It is important to understand the concept of targets as they serve a similar purpose to the previous system's runlevel, but allow for greater flexibility and control over what services are running at any given time.

Explanation of Runlevels (Targets) and Their Purpose

SystemD targets are essentially a group of services that need to be started or stopped together. These groups can depend on each other, meaning that starting one target can also start another if it requires it. Targets can also be used to define which services should be started at boot time or which ones should not.

The purpose of targets is twofold −

  • To provide an organized way for the administrator to manage a group of related services as a single unit.

  • To provide a way for SystemD to read and store information about what needs to start up when the system boots up.

Overview of Different Runlevels (Targets) in SystemD

SystemD provides several built-in targets that cover different states in which your system can operate −

  • poweroff.target − This target is used when shutting down your system completely. All running processes and services will be gracefully terminated before your system powers off.

  • rescue.target − This target provides minimal functionality – only enough for an administrator or user to troubleshoot problems with their Linux installation. It does not include any network or graphical environment support, making it ideal for recovering after critical errors have occurred.

  • multi-user.target − this target is used when the system's multi-user environment has been started, providing full network and graphical support.

  • graphical.target − Similar to the multi-user target, this target is used to start the graphical environment. It starts all services required for the Graphical User Interface (GUI).

In addition to these built-in targets, administrators can also create custom targets that suit their needs. Custom targets are defined in SystemD unit files, which will be discussed in more detail later in this article. Now that we have a basic understanding of what runlevels (targets) are and how they function in SystemD, let's go over how to change them.

Changing Runlevels (Targets)

Identifying the Current Runlevel (Target)

Before changing the runlevel (target), it is essential to identify the current runlevel (target). To do this, open a terminal and execute the following command −

systemctl get-default  

This command will display the current default target.

For example, if you see "multi-user.target", it means your system is currently running at runlevel 3. Alternatively, you can use the following command to check all active targets −

systemctl list-units --type=target  

This will display all active targets and their states.

Selecting a New Runlevel (Target)

Once you have identified the current runlevel(target), you can choose a new one based on your requirements. For instance, if you want to switch from graphical mode to text mode, you should select "multi-user.target" as your new target.

You can view all available targets by executing the following command −

systemctl list-unit-files --type=target  

The output of this command shows all available targets with their descriptions. Choose carefully the target that suits your requirements

Switching to a New Runlevel(Target)

After selecting your new target, execute the following command substituting with your chosen target name −

sudo systemctl isolate  

This will stop all services and applications running under the previous run level(target)and start those defined for thie newly selected one. In case of any issues, refer to the troubleshooting section of this article.

Advanced Techniques for Changing Runlevels (Targets)

Using Systemctl Command Options to Change Targets Temporarily or Permanently

Systemctl is the primary command-line tool used for managing system services in SystemD. It provides a wide range of options that allow users to interact with and control different aspects of the system services. One of these options is the ability to change targets temporarily or permanently.

To change targets temporarily using systemctl, you can run the following command −

$ sudo systemctl isolate target_name.target  

For example, to switch from the graphical.target to multi-user.target temporarily, you can run −

$ sudo systemctl isolate multi-user.target  

This will immediately stop all services associated with graphical.target and start all services associated with multi-user.target. To change targets permanently using systemctl, you can run −

$ sudo systemctl set-default target_name.target  

For example, to set multi-user.target as the default target on boot permanently, you can run −

$ sudo systemctl set-default multi-user.target  

This will cause SystemD to switch to multi-user.target every time the system boots up.

Modifying Default Targets Using Symbolic Links

Symbolic links are a type of file that contains a reference to another file or directory. In SystemD, symbolic links are commonly used to modify default targets. To modify default targets using symbolic links, follow these steps −

  • Identify the current default target by running −

$ systemctl get-default 
  • Create a new symbolic link that points to the desired target by running −

$ sudo ln -sf /usr/lib/systemd/system/target_name .target /etc/systemd/system/default.target 

For example, if we want our default target to be graphical.target, we would run −

$ sudo ln -sf /usr/lib/systemd/system/graphical.target /etc/systemd/system/default.target  
  • Verify that the new default target is set by running −

$ systemctl get-default  

The above steps will modify the default target of the system. Every time the system boots up, it will start in the new default target.

Troubleshooting Common Issues with Changing Runlevels(Targets)

Permission Errors

When attempting to change runlevels (targets) in SystemD, one common issue that may arise is permission errors. These errors occur when the user does not have the necessary permissions to make changes to the target files.

To resolve this issue, it may be necessary to use the sudo command or switch to the root user account before attempting to change runlevels (targets). Additionally, ensure that all file permissions are set correctly for the target files.

Service Dependencies

Another common issue when changing runlevels (targets) in SystemD is service dependencies. These issues occur when a service requires other services or resources that are not available in the new runlevel (target).

To troubleshoot this issue, it may be necessary to identify which service is causing dependency issues and make changes accordingly. This may involve disabling or modifying certain services that are not essential for the system.

Incorrect Syntax

Incorrect syntax can also cause issues when attempting to change runlevels (targets) in SystemD. This can occur if there are typos or incorrect commands entered when using systemctl commands to change targets.

To troubleshoot this issue, double-check all command syntax and ensure that any options or arguments used with systemctl commands are correct. Additionally, consult documentation or online resources for proper command syntax if needed.

Conclusion

In this article, we have learned about SystemD and its runlevels (targets). We have explored the different runlevels (targets) in SystemD and the purpose of each.

We also learned about the step-by-step process of changing runlevels (targets) in SystemD and some advanced techniques for doing so. We discussed some common issues that may arise when changing runlevels (targets) in SystemD and how to troubleshoot them.

Updated on: 24-Aug-2023

441 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements