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.
Masking Systemd Units in Linux: FAQs
What is masking a systemd unit?
Masking a systemd unit essentially renders it inaccessible. This means the service can't be started manually, by other services, or even during boot. It's considered the "strongest" form of disabling a service in systemd, like a "level 3 off switch."
How do I mask a unit?
The command you need is systemctl mask <unit_name>. For example, to mask the "cups" service, you'd use systemctl mask cups.service. This creates a symbolic link pointing to /dev/null in the appropriate systemd directory, effectively disconnecting the service from the system.
When would I use masking instead of disabling?
There are a few situations where masking is preferable to disabling:
- Prevent accidental activation: Masking makes it virtually impossible to accidentally start the service, even through dependencies.
- Block unwanted boot services: Mashed services won't even attempt to start during boot, potentially improving boot time.
- More persistent than disabling: Disabling removes links for auto-starting at boot, but masked units remain unaffected by configuration changes.
How do I unmask a unit if needed?
Use the command systemctl unmask <unit_name>. This removes the symbolic link and restores the unit's normal operation.
Are there any risks or considerations with masking?
Masking a critical service or one required by another service can lead to system instability. Always double-check the unit name and understand its role before masking. Additionally, masked units won't receive system updates unless unmasked first.
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
How to Install 'Python-VPython' package on Linux?
VPython package in python makes it easy to construct passable 3D displays and animations, even for those with limited programming experience. Because it is based on Python3, it also has much to offer for experienced programmers and researchers. This package permits users to create objects such as sp
2 min read
How to Include NA in ifelse() Using R?
The if-else () function in R is a powerful and efficient way to apply conditional logic across vectors, matrices, or data frames. However, dealing with NA (missing values) in if-else () can be tricky. This article will explore how to handle and include NA values effectively when using if-else () in
3 min read
How to parse a URL into hostname and path in javascript ?
We can parse a URL(Uniform Resource Locator) to access its component and this can be achieved using predefined properties in JavaScript. For Example, the first example parses the URL of the current web page and the second example parses a predefined URL. Example 1:This example parses the URL of the
1 min read
How to Detect AJAX Request to Normal Request in Node.js ?
When developing web applications with Node.js, you often need to differentiate between AJAX (Asynchronous JavaScript and XML) requests and normal HTTP requests. AJAX requests are typically used to fetch data without reloading the entire page, while normal requests often result in full page loads. De
3 min read
How to Install Tor Browser on Kali Linux? [2024]
The Tor browser provides a simple yet powerful way to access the internet anonymously and bypass censorship. Setting it up on Kali Linux requires just a few easy terminal commands to add the Tor Project repository and install the Tor packages. Once installed, the browser can be launched through the
6 min read
How to Use Vite with Angular?
Vite is a modern build tool that dramatically enhances the development experience of front-end systems. Originally developed for Vue.js, Vite has gained a reputation for speed and performance. This article will explore how to integrate Vite and Angular using Viteâs fast development server and effect
2 min read
What is GitHub and How to Use It?
GitHub is a web-based platform that hosts Git repositories, providing developers with tools for version control and collaboration. Whether you are working on a small personal project or a large enterprise application, GitHub can streamline your workflow and enhance productivity. Table of Content Wha
12 min read
How to get text found between span in Selenium?
Selenium WebDriver enables you to interact with web elements on a page. To extract text from a <span> element, you need first to locate it using one of Seleniumâs locator strategies, such as by ID, class, or XPath. After locating the <span> element, you can retrieve its text content with
2 min read
How to Configure Quagga Routing Suite on Linux?
Quagga is a routing software suite that provides support for various routing protocols, including OSPF Open Shortest Path First), BGP (Border Gateway Protocol), and RIP (Routing Information Protocol). It is designed to run on Unix-like operating systems, such as Linux, and is often used in large net
5 min read
How to use the Dodge Tool in Photoshop?
Earlier to lighten some specific parts of the image, photographers in the darkroom used some specific techniques to enlighten areas of an image, that process is called dodging, Dodge tool in photoshop is used to do the same. The Dodge tool in Photoshop is used to lighten the area of the image withou
3 min read
How to Schedule Posts in Bulk Using Hootsuite [Free]
Bulk scheduling of social media postings may greatly improve the efficiency of your content management system and guarantee regular audience interaction without requiring continuous human participation. A handy tool for this work is Hootsuite, a well-known social media management software. Users may
9 min read
How to check for "undefined" value in JavaScript ?
In JavaScript, undefined is a primitive value that represents the absence of a value or the uninitialized state of a variable. It's typically used to denote the absence of a meaningful value, such as when a variable has been declared but not assigned a value. It can also indicate the absence of a re
3 min read
How to print on browser's console using PHP ?
The echo command is used in PHP to print any value to the HTML document. Use <script> tag inside echo command to print to the console. Syntax: echo "This is GeeksforGeeks Tutorial."; echo variable_name; Note: The echo command will print the HTML document. This feature can be used to put JavaSc
2 min read
How to Change a Django QueryDict to Python Dict
In Django, a QueryDict is a specialized dictionary that handles HTTP GET and POST parameters. In an HttpRequest object, the GET and POST attributes are instances of django.http.QueryDict. There are instances when we may need to convert the QueryDict into a regular Python dictionary dict() to perform
6 min read
How to add Python to Windows PATH?
Python is a great language! However, it doesnât come pre-installed with Windows. Hence we download it to interpret the Python code that we write. But wait, windows donât know where you have installed the Python so when trying to any Python code, you will get an error. We will be using Windows 10 and
2 min read
How to define the URL of the video file in HTML5?
In this article, we will learn how to define the URL of a video in HTML. The <video> tag is used to embed video files on a web page. This displays a video player along with the player controls depending on the browser. The source of the video can either be a singular one or it can be made to s
3 min read
Consistency vs. Availability in System Design
When it comes to the field of system design there are two fundamental Concepts often analyzed, they are Consistency and Availability. It is essential to comprehend these ideas to ensure the construction of dependable, inexpensive, and optimal systems. These two concepts are explained in detail in th
6 min read
Stimulate bouncing game using Pygame
In this article, we will learn to make a bouncing game using Pygame. Pygame is a set of Python modules designed for writing video games. It adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the Python language. It is fre
4 min read
Subzy - Subdomain takeover Vulnerability Checker Tool
Subzy is the tool that identifies or checks the subdomain takeover on the target domain or multiple subdomains. Subzy is the Golang language based-tool. Subdomain takeover tool, which works based on matching response fingerprints from can-i-take-over-xyz. This automated scanner can help you in bug b
4 min read