Open In App

How to Manage System Services in Linux | systemctl Command

Last Updated : 11 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 for managing services. In this article, we will explore the intricacies of systemctl and how it can be used to control and monitor system services.

What is Systemd?

Systemd is like the traffic manager for your Linux system. It’s a modern tool that organizes and supervises how different parts of your system start and run. Imagine it as a smarter, faster way to handle tasks compared to older methods. Instead of doing things one after the other, systemd can get multiple tasks going at the same time, making your computer start up quicker and respond faster. It uses something called “units” to keep everything in order – like services, sockets, and devices – helping your system run smoothly.

What is the Linux Systemctl Command?

Now, imagine you have a remote control for your Linux system – that’s what `systemctl` is. It’s like your boss for telling the system what to do. With this tool, you can start, stop, or restart services (think of them like programs running in the background). Want something to start automatically when your computer turns on? `systemctl` helps with that too. It’s also good for checking how things are going – like looking at logs or figuring out what other things a service needs to work properly. So, if you’re in charge of a Linux system, `systemctl` is your go-to tool for keeping things organized and running well.

Syntax of `systemctl` command in Unix

systemctl [command] [service]


[command] = The action we want to perform (start, stop, enable, disable, etc.)

[service] = The name of the service we want to perform the action on.

How to Manage System Services in Linux

1) Starting and Stopping Services

Starting Services

Syntax:

systemctl start [service]


Example:

If we want to start our SSH service.

systemctl start sshd


Stopping Services

Syntax:

systemctl stop [service]


Example:

If we want to stop our SSH service.

systemctl stop sshd


2) Enabling and Disabling Service

Enabling Services

Syntax:

systemctl enable [service]


Example:

If we want to enable our firewall service.

systemctl enable firewalld


Disabling Services

Syntax:

systemctl disable [service]


Example:

If we want to disable our firewall service.

systemctl disable firewalld


3) Viewing the Staus of Services

Viewing Status of Services

Syntax:

systemctl status [service]


Example:

If we want to see the status of our firewall service.

systemctl status firewalld


4) Restarting and Reloading Services

Restarting Services

Syntax:

systemctl restart [service]


Example:

If we want to restart our SSH service.

systemctl restart sshd


Reloading Services

Syntax:

systemctl reload [service]


Example:

If we want to reload our Apache service.

systemctl reload httpd


5) Masking and Unmasking Services

Masking Services

Syntax:

systemctl mask [service]


Example:

If we want to mask our MySQL service.

systemctl mask mysqld


Masking a service prevents it from being started or enabled, even if it is required by other services.

Unmasking Services

Syntax:

systemctl unmask [service]


Example:

If we want to unmask our MySQL service.

systemctl unmask mysqld


Unmasking a service allows it to be started or enabled again.

6) Changing the Default Target

Syntax:

systemctl set-default [target]


Example:

If we want to change the default target to graphical.target (which will start the graphical user interface).

systemctl set-default graphical.target


The default target determines which system services are started when the system boots up.

7) Listing Unit Files

Syntax:

systemctl list-unit-files


This command lists all the unit files available on the system, including both enabled and disabled unit files.
 

8) Masking and Unmasking Unit Files

Masking Unit Files

Syntax:

systemctl mask [unit-file]


Example:

If we want to mask the SSH unit file.

systemctl mask sshd.service


Masking a unit file prevents it from being started or enabled, even if it is required by other services.

Unmasking Unit Files

Syntax:

systemctl unmask [unit-file]


Example:

If we want to unmask the SSH unit file.

systemctl unmask sshd.service


Unmasking a unit file allows it to be started or enabled again.

Options available in `systemctl` command in Unix

Options                                                      Description Syntax
–version This option displays the version number of the systemctl command.
systemctl --version


–help This option displays the help manual for systemctl, including a list of available options and commands
systemctl --help


–type The argument in this case should be comma-separated list of unit types such as service and socket.
systemctl --type=service


–all This option lists all available units, including those that are inactive.
systemctl --all


–failed This option lists all units that have failed.
 
systemctl --failed


–user talk to service manager of calling user, instead of system.
systemctl --user


–force This option forces the service to start or stop, even if it has dependencies that are not yet started or stopped.
systemctl stop --force httpd.service


–no-block

This option starts or stops the service without blocking the shell, allowing the user to continue to use the shell while the service is starting or stopping.
systemctl start --no-block httpd.service


–state

This option is used to filter the output based on the specified unit state. You can specify one or more-unit states separated by commas, such as active, inactive, failed, and activating.

 For example: to list all the failed units.

systemctl list-units --state=failed


-r, –recursive

This option is used to show units of local containers as well.

 For example: to list all units including those in local containers.

systemctl list-units --recursive



–show-types

This option is used to show the types of sockets along with showing sockets. 

For example: to show the types of all sockets.

systemctl list-sockets --show-types


–job-mode=

This option controls how to deal with already queued jobs in case of queuing a new job. There are three available modes:

replace: replace already queued jobs with the new one.
fail: cancel the new job and return failure if there are already queued jobs.
isolate: queue the new job and isolate it from the other jobs.

For example: to use the replace mode.

systemctl isolate graphical.target --job-mode=replace


-i, –ignore-inhibitors

This option is used to ignore inhibitor locks when requesting a system shutdown or sleep state. 

For example: to ignore inhibitor locks when requesting a system shutdown.

systemctl poweroff --ignore-inhibitors


-q, –quiet

This option is used to suppress the printing of results of various commands and the hints about the truncated lines. 

For example: to reload the systemctl daemon without showing any output.

systemctl daemon-reload --quiet


–no-wall

This option is used to not send a wall message before power-off, halt, or reboot. 

For example: to halt the system without sending a wall message.

systemctl halt --no-wall


Frequently Asked Questions

1. What is the role of systemd in Linux, and how does it differ from traditional init systems?

systemd is a modern init system and service manager in Linux. Its role is to initialize and manage system processes and services during startup and runtime. Unlike traditional init systems, systemd operates asynchronously and in parallel, leading to faster boot times and improved system responsiveness.

2. How do you manage services in Linux?

There are two main ways to manage services in Linux:

  • Using systemctl: This is the most common and recommended method for managing services under systemd, the current system and service manager on most Linux distributions. With `systemctl`, you can start, stop, restart, check status, enable/disable, and perform other advanced actions on services.
    For example: To start the SSH service you would use `sudo systemctl start sshd.service`.
  • Using service scripts: Some older systems or specific services might still use traditional service scripts located in `/etc/init.d/`. These scripts often use commands like `start`, `stop`, and `restart` to control the service. However, using `systemctl` with systemd is generally considered better practice.

3. How do I check system service in Linux?

There are a few ways to check the status of a service in Linux:

  • Using `systemctl status <service_name>.service`: This is the most common way to check the status of a service managed by systemd. It will show you if the service is running, active, and any relevant information about its state and exit code.
  • Using `service <service_name> status`: This command works with older init systems or systemd managed services using service scripts.
  • Looking at the boot logs: You can view the boot logs with `journalctl -b` to see if the service started successfully or if there were any errors during startup.

4. How to run a service in Linux?

To run a service in Linux, you can use `systemctl start <service_name>.service` for systemd services or `service <service_name> start` for services managed by older init systems. However, remember that services are often started automatically at boot based on their configuration, so running them manually might not be necessary unless you’re troubleshooting or testing something.

5. How can I reboot my Linux system using systemctl?

The simplest way is with `sudo systemctl reboot`. Press Enter, enter your password when prompted, and your system will restart shortly.

Conclusion

In this article we will understand the use of `systemctl` command in Linux and How to manage system services in linux. By mastering systemctl, you gain full control over your system’s services, ensuring optimal performance and stability. Whether you’re a seasoned Linux admin or just starting, this powerful command is a valuable tool in your arsenal. So, dive in, explore its capabilities, and take command of your Linux system!

Remember, this is just a starting point. The world of systemctl is vast and offers many more possibilities. Explore the man page (man systemctl) and online resources for deeper dives and advanced usage. Feel free to ask any questions you may have, and happy systemd adventures!
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads