The Wayback Machine - https://web.archive.org/web/20241129123113/https://www.geeksforgeeks.org/systemctl-in-unix/
Open In App

How to Manage System Services in Linux | systemctl Command

Last Updated : 04 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

How to Manage System Services in Linux | systemctl Command – FAQs

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.

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.

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.

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.

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!
 



Previous Article
Next Article

Similar Reads

How to Start, Stop and Restart Services in Linux Using systemctl Command
System services play a crucial role in the functioning of a Linux system, handling various tasks and processes in the background. systemctl is a powerful command-line tool that allows users to manage these services effectively. In this article, we will explore the basics of using systemctl to start, stop, restart, enable, disable and display status
9 min read
systemctl: command not found on ubuntu
In Ubuntu, while working with the 'systemctl' command, users typically manage system services, view their status, start or stop them, and enable or disable them at system startup. However, encountering the "systemctl: command not found" error suggests that the 'systemctl' utility is not available or accessible in the current environment. This issue
3 min read
Understanding Package Managers and systemctl
A package manager is a command-line or graphical tool used to automate the process of installing, updating, and removing software packages on a Linux system. Software packages are collections of files, including executables, libraries, configuration files, and documentation, that are bundled together for easy distribution and installation. Package
9 min read
How to Manage KVM Virtual Environment using Command Line Tools in Linux
KVM, short for Kernel-based Virtual Machine, is a complete virtualization solution for Linux running on x86 hardware (Intel VT or AMD-V). Since version 2.6.20, the mainline Linux kernel has featured KVM, a complete virtualization solution for Linux running on Intel 64 and AMD 64 hardware that is fast and stable for the majority of workloads. It con
8 min read
Manage Network Connections From the Linux Command Line with nmcli
Network connectivity is a critical aspect of modern computing, and Linux provides a robust command-line tool for managing network connections: nmcli. Network Manager Command-Line Interface (nmcli) is a powerful and versatile utility that allows users to control and configure network settings directly from the terminal. In this article, we will delv
5 min read
How to use Linux Cockpit to manage system performance
Cockpit is a web-based graphical management tool for Linux machines on your computer. Having Cockpit on your server allows you to manage events on the user interface of a browser. Our typical admin tasks include setting up your firewall, changing your network settings, managing storage, creating accounts, installing containers, updating software, a
5 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a Linux and macOS cat command that improved its precursor: the syntax highlighted file contents can be displayed with its help. Therefore, in the case of source code and configuration files, it is extremely helpful. It can highlight over 10 languages, like JavaScript, Java, Ruby, Python, Go, C, and JSON. Syntaxccat [OPTIONS] [FILE...]Basic
3 min read
Linux - Installing PIP to Manage Python Packages
Python is the most popular language for a decade, and we all know that python had a huge support of libraries that are used in different sectors of Software Engineering. Meanwhile, PIP also getting its fame along with python in Linux and developers community.PIP is a package manager for python in handling the software packages that are developed us
2 min read
Most Useful Commands to Manage Apache Web Server in Linux
Prerequisite: How do Web Servers work? Apache is one of the most widely used free, open-source Web Server applications in the world, mostly used in Unix-like operating systems but can also be used in windows. As a developer or system administrator, it will be very helpful for you to know about the Apache webserver. It has many notable features amon
3 min read
How to Manage Linux Containers using LXC
LXC is a technology for creating light weighted containers on your system. This technology is able to run on more than one Linux host. It is just like its counterparts i.e the other hypervisors like KVM and Xen. Unlike complete virtualization, containerization does not provide the user with complete isolation but the amount of load that is exerted
4 min read
How To Use IPRoute2 Tools to Manage Network Configuration on a Linux
In computer networking, the iproute2 command line utility tool is a versatile and powerful tool for users. it provides beginner-friendly functionality to manage and configure the network. In this article, we are going to discuss each and every part of the iproute2 tool and how to implement it in our system. By the end of this article, you'll have s
4 min read
How to Manage Debian Packages in Linux?
Debian, one of the oldest and most respected Linux distributions, is renowned for its stability, security, and robust package management system. Managing Debian packages effectively is crucial for maintaining a healthy and well-functioning Linux system. In this comprehensive guide, we will explore every aspect of Debian package management, from ins
9 min read
How to Manage Packages in Red Hat-based Linux?
Package management tools like dnf (Dandified YUM) or yum (Yellowdog Updater Modified) are used to manage packages on Red Hat-based Linux systems. These programs support the installation, removal, and upgrading of software packages on your computer. Typical package management tasks include the following: Table of Content DNF Package Manager:YUM(Yell
4 min read
How to Manage Directories in Linux?
Directories in Linux or any operating system help to organize the data and files, which is then easier to find and manage. In this article, you will learn everything you will need to manage directories in Linux. We will try to cover every topic from creating, and copying to deleting the directories and many more. We will explore various topics, inc
6 min read
Finding System Boot-up Performance Statistics in Linux Using systemd-analyze Command
If your Linux system is taking so much time to boot, and you want to know which process is taking so much time to boot then you are on the right page. We are going to see how to get a complete overview of system boot-up time and which service is taking more time to initialize. We are going to use the systemd-analyze command and subcommands of syste
2 min read
How to Mount File System in Linux | mount Command
All files in a Linux filesystem are arranged in form of a big tree rooted at '/'.These files can be spread out on various devices based on your partition table, initially your parent directory is mounted(i.e attached) to this tree at '/', others can be mounted manually using GUI interface(if available) or using mount command. mount command is used
6 min read
Unmount File System in Linux Using the umount Command
Unmounting a file system is a vital procedure for securely deleting storage devices or rendering system resources accessible. A vital instrument in this procedure is the "umount" command, which enables users to softly unmount mounted devices or partitions. Before beginning the unmounting process, this step ensures data integrity and protects any po
7 min read
Reboot Linux System Command with Examples
Rebooting a Linux system is a fundamental administrative task necessary for applying updates, troubleshooting, or system maintenance. Various commands are available to reboot a Linux system, each with specific options and use cases. This guide explores essential reboot commands such as `reboot`, `shutdown`, and `init`, illustrating their usage with
11 min read
How to Monitor System Activity in Linux | top Command
top command is used to show the Linux processes. It provides a dynamic real-time view of the running system. Usually, this command shows the summary information of the system and the list of processes or threads which are currently managed by the Linux Kernel. As soon as you will run this command it will open an interactive command mode where the t
10 min read
SAR command in Linux to monitor system performance
sar (System Activity Report) It can be used to monitor Linux system's resources like CPU usage, Memory utilization, I/O devices consumption, Network monitoring, Disk usage, process and thread allocation, battery performance, Plug and play devices, Processor performance, file system and more. Linux system Monitoring and analyzing aids in understandi
6 min read
Exodus - Copy Linux Binaries From One Linux System
Exodus is an easy-to-implement program, that is used for bundling dependencies, and also helps to effectively copy Linux ELF binaries from one to another machine securely, and this is very handy when you don't have root access, also if there are issues in the availability of the packages in the Linux distribution, Commonly Server oriented distribut
3 min read
How To Setup an Iptables Firewall to Enable Remote Access to Services in Linux
A system administrator can modify the IP packet filter rules of the Linux kernel firewall, which are implemented as various Netfilter modules, using the user-space utility application iptables. The filters are arranged in several tables, each of which has a set of rules on how to handle packets of network data. Using a set of programmable table rul
5 min read
How to Control Systemd Services on Remote Linux Server
Linux, SysV, and LSB init scripts are compatible with Systemd, a system and service manager. Aggressive parallelization capabilities are offered by Systemd, which also offers on-demand daemon starting and uses Linux cgroups to keep track of processes. Systemd also supports system snapshotting and restore, maintains mount and automount points, and i
2 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax
4 min read
anacron command in linux with examples
anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j
3 min read
SED command in Linux | Set 2
We have discussed some of the SED command options in Sed Command in Linux/Unix with examples SED is used for finding, filtering, text substitution, replacement and text manipulations like insertion, deletion search, etc. It's a one of the powerful utilities offered by Linux/Unix systems. We can use sed with regular expressions. I hope atleast you h
8 min read
enable and disable command in Linux
Enables and disables are the built-in shell commands. enable command is used to start the printers or classes whereas the disable command is used to stop the printers or classes. Syntax For enable Command: enable [-a] [-dnps] [-f filename][name ...] Syntax For disable Command: disable [-c] [-W] [ -r [ reason ] ] printer Options: -a : List all built
1 min read
three90RightbarBannerImg