How to mask a systemd unit in Linux?
Last Updated :
02 Feb, 2024
Systemd is a system and service manager for Linux operating systems, providing a range of functionalities for managing system processes. In some cases, you may want to hide or disable a systemd unit to prevent it from starting automatically. This process is known as "masking" a systemd unit. In this article, we'll explore the steps to mask a systemd unit in Linux.
What is Systemd?
Systemd is a suite of system management daemons, libraries, and utilities designed to centralize the management of system services. It replaces the traditional System V init scripts and provides features like the parallel startup of system services, on-demand activation of daemons, and dependency-based service control.
Systemd Units
Systemd units are configuration files that describe how a service, device, mount point, or other aspects of the system should be managed by systemd. Units are defined with files having extensions like .service, .socket, .mount, etc. They are located in directories such as /etc/systemd/system/ or /usr/lib/systemd/system/.
Method 1: Masking a Systemd Unit
Masking a systemd unit means making it impossible to start the unit, either manually or during system boot. This is achieved by creating a symlink from the unit file to /dev/null. The following steps demonstrate how to mask a systemd unit:
Step 1: Identify the Unit
Determine the systemd unit you want to mask. It could be a service, socket, target, etc. You can list all units using the systemctl list-units command or focus on specific types using systemctl list-units --type=TYPE. Use the following command:
systemctl list-units --type=your_unit_type
This command lists all active units of the specified type (your_unit_type). You need to replace your_unit_type with the actual type of the systemd unit you want to mask, such as 'service', 'socket', 'device', etc.
For example:
systemctl list-units --type=service
Output:
Identify the service to maskThe command entered in the terminal listed all the active services in the system.
Step 2: Mask the Unit
Now, create a symbolic link to /dev/null in the systemd unit directory. This will effectively disable the unit. Use the following command:
sudo systemctl mask UNIT_NAME
This command creates a symbolic link '(symlink)' from the systemd unit file to '/dev/null', effectively disabling the unit. Replace your_unit_name with the actual name of the unit you want to mask.
For Example:
sudo systemctl mask apache2.service
Output:
Mask the serviceBy the command mentioned above we are masking apache2.service which has successfully created symlink.
Step 3: Verify the Masking:
Confirm that the unit is masked by checking its status with systemctl status. The output should indicate that the unit is "masked." Use the following command:
systemctl status UNIT_NAME
The output should show something like "Loaded: masked" if the masking was successful.
For example:
systemctl status apache2.service
Output:
Check Status and VerifyBy entering the above command we can clearly see int the output that the service has been masked successfully.
Step 4: (Optional) Unmasking the Unit:
Ensure that the unit is masked by checking its status.
sudo systemctl unmask UNIT_NAME
This command checks the status of the specified systemd unit (UNIT_NAME). It helps you verify that the unit is now inactive (dead) and masked. The output should indicate that the unit is no longer running.
For Example:
sudo systemctl unmask apache2.service
Output:
Unmasking serviceBy using the above command we are unmasking the apache2.service and output shows that unmasking has been done successfully.
Method 2: Disabling the unit
You can also achieve a similar effect by disabling the unit. Disabling a unit removes the symbolic links to the unit files, preventing it from being started.
sudo systemctl disable <unit_name>
Explanation:
- The 'systemctl disable' command removes symbolic links related to the specified unit, preventing it from being started.
- This is an alternative method to achieve a similar effect to masking.
For example:
sudo systemctl disable nginx.service
Output:
Disabling unitOutput shows disabled that the unit has been disabled successfully.
To re-enable the unit :
sudo systemctl enable <unit_name>
Above command re-enables the unit and for more clarity refer to the below example:
sudo systemctl enable nginx.service
Re-enabling unitOutput shows that the unit has been re-enabled.
Method 3: Renaming the Unit File:
sudo mv /etc/systemd/system/<unit_name> /etc/systemd/system/<unit_name>.backup
sudo systemctl daemon-reload
- This command uses the mv (move) command to rename the systemd unit file. Replace <unit_name> with the actual name of the systemd unit you want to disable.
- For example, if you are working with the nginx.service unit, it would be:
sudo mv /etc/systemd/system/nginx.service /etc/systemd/system/nginx.service.backup
Output:
RenamingBy appending ".backup" to the filename, you effectively make the unit file inaccessible to systemd. This prevents systemd from recognizing and managing the service.
Conclusion
Masking a systemd unit in Linux is a straightforward process that involves stopping the unit and creating a symbolic link to /dev/null. This prevents the unit from starting automatically during system boot or when manually invoked. Always exercise caution when masking units, as it may impact system functionality, and only mask units that you are certain should be disabled.
Similar Reads
Boot Process with Systemd in Linux
Systemd is a Linux system and service manager that is responsible for controlling the boot process of a Linux system. The systemd boot process consists of several stages, each of which performs a specific task that is necessary for the successful initialization of the system. Understanding Systemd :
7 min read
How To Automate Podman With Systemd Integration?
Podman is an innovative container management solution providing a secure and lightweight alternative to standard containers such as Docker. Libpod, a container lifecycle management library included in this repository, serves as the foundation for Podman. APIs for managing containers, pods, container
5 min read
How to Spoof SMS Message in Linux ?
In this article, we will show how to spoof SMS messages in Linux using two of the following tools:- fake-smsSocial Engineering Toolkit (SET)1.) Fake-sms It is a tool written in simple script to send SMS anonymously. Features:Send sms anonymouslyFast sms deliveryInternational sms sending available.On
2 min read
How to Restart a Service in Linux?
In Linux, managing system services is an essential task for maintaining the stability and performance of your system. Whether you're troubleshooting an application, applying configuration changes, or ensuring services run smoothly, knowing how to restart a service in Linux is crucial. With tools, li
3 min read
How to Manage System Services in Linux | systemctl Command
Linux operating systems are known for their robustness and versatility, and managing system services is a crucial aspect of maintaining a well-functioning system. With the advent of systemd, a system and service manager for Linux operating systems, the systemctl command has become an essential tool
8 min read
How to Read and Edit Systemd Logs using Journalctl in linux
In the realm of Linux system administration, managing logs is an indispensable task. System logs are crucial for understanding the health, performance, and troubleshooting of a system. Systemd, the init system widely adopted by modern Linux distributions, introduced a centralized logging system call
6 min read
How to List Running Processes in Linux | ps Command
As we all know Linux is a multitasking and multi-user system. So, it allows multiple processes to operate simultaneously without interfering with each other. Process is one of the important fundamental concepts of the Linux OS. A process is an executing instance of a program that carries out differe
9 min read
How to Mask Password in VBA Excel Input Box?
A combination of integers, strings of characters, or other symbols is used to verify the identity of a user during the authentication process so, that they can access the system. But a password should always remain in a hidden format to prevent it from shoulder spying. Shoulder spying is a practice
2 min read
How to Install Steghide Tool in Linux?
Here we will discuss how to use Steghide on Kali Linux to hide and remove hidden data within an image. Various Steganography tools are available, but the part that sets it apart is that it uses a variety of algorithms to encrypt data. In addition, Steghide supports to cover data behind any image (jp
7 min read
Systemd vs Init Cheatsheet for Linux
Systemd is the new init framework, beginning with Fedora and presently embraced in numerous circulations like RedHat, Suse, and Centos. All things considered, the vast majority of us have been utilizing conventional SysV init scripts typically living in/and so on/rc.d/init.d/. These contents conjure
3 min read