The Wayback Machine - https://web.archive.org/web/20241127233533/https://www.geeksforgeeks.org/start-stop-restart-services-using-systemctl-in-linux/
Open In App

How to Start, Stop and Restart Services in Linux Using systemctl Command

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

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 of services in a Linux environment.

What is systemctl?

Before diving into service management, it’s essential to understand the basics systemctl. This command is used to control the systemd system and service manager, which is a central component in modern Linux distributions.

systemctl [command] [unit]

Here,

  • command: Action to be performed (e.g., start, stop, restart, enable, disable).
  • unit: The service or unit to be affected.

Systemctl is a controller or utility of Systemd (an init system with a set of programs executed in the background), with auxiliary in manage services, These commands are executed in mode root if you aren’t mode rooting the system, requesting the password of root.

What is a service?

A service in computing is a background process that performs specific tasks and functions without user intervention. Services often start automatically during system boot and run continuously to provide functionalities such as web serving, file sharing, or system monitoring. They are managed by the operating system and can be started, stopped, or restarted as needed. Services help ensure essential operations are consistently available. Examples include web servers like Apache, database servers like MySQL, and system logging services.

What is a daemon?

A daemon is a background process that runs continuously on a Linux or Unix system. It performs tasks without direct user interaction, often starting automatically at boot. Common examples include web servers, database servers, and system monitoring tools. Daemons usually have names ending with “d”, like httpd or sshd. They help manage system functions and services efficiently.

What is the init system?

The init system is the first process that runs when a Linux system boots up and is responsible for initializing the system. It manages system services and processes, handling their start, stop, and restart. Common init systems include SysVinit, Upstart, and systemd. The init system ensures that all necessary services are running correctly and manages dependencies between them. It continues to run in the background to monitor and manage system processes throughout the system’s operation.

Units in systemd

In systemd, units are the basic objects that systemd manages and controls. They represent system resources such as services, timers, devices, and mount points. Each unit is defined by a configuration file with a specific suffix, like .service for services or .timer for timers. Units can be started, stopped, enabled, or disabled using systemctl commands. They are essential for defining how the system boots and manages various processes and resources.

.service units in systemd

Systemd .service units are configuration files used to manage services on Linux systems. They define how and when services should be started, stopped, and restarted. Each .service file contains details like the service’s name, description, and the command to run. These files are typically located in /etc/systemd/system or /lib/systemd/system. Managing them involves commands like systemctl start, systemctl stop, and systemctl enable.

How to find all the systemd units in the system?

To list available systems units or to List all Services in Linux we use the following command:

systemctl list-unit-files --type service -all

all the systemd

How to Start a System Service in Linux

Syntax:

sudo systemctl start service.service

The command start serves for starting (activate) one or more units specified on the command line.

Example:

sudo systemctl start mariadb
systemctl start

Command Start

How to Stop a System Service in Linux

Syntax:

sudo systemctl stop service.service

The command stop serves for stopping the service or (deactivate) one or more units specified on the command line.

Example:

sudo systemctl stop mariadb
systemctl stop

Command Stop and Status

How to Display Status of a System Service in linux

Syntax:

sudo systemctl status service.service

The command status serves to check the status of the service. Show terse runtime status information about one or more units, followed by the most recent log data from the journal. If no units are specified, show system status.

Example:

sudo systemctl status mariadb
systemctl status

Command Status

How to restart a System Service in Linux

Syntax:

sudo systemctl restart service.service

The command restart serves for restarting the service in execution. Stop and then start one or more units specified on the command line. If the units are not running yet, they will be started.

Example:

sudo systemctl restart mariadb
systemctl restart

Command Restart

How to Enable a System Service in Linux

Syntax:

sudo systemctl enable name_service.service

The enable command serves for executing the service since the initialization if consists of one or more units or unit instances. This will create a set of symlinks, as encoded in the [Install] sections of the indicated unit files. the system manager configuration is reloaded (in a way equivalent to daemon-reload), in order to ensure the changes are taken into account immediately. 

Example

sudo systemctl enable mariadb

systemctl enable

Command Enable

Command Status

How to Disable a System Service in Linux

Syntax:

sudo systemctl disable name_service.service

The disable command serves for withdrawing the service since the initialization of one or more units. This removes all symlinks to the unit files backing the specified units from the unit configuration directory and hence undoes any changes made by enabling or link.

Example:

sudo systemctl disable mariadb

systemctl disable

Command Disable

Command Status

Shutdown or reboot the system using systemctl

Reboot the System: It instructs systemd to restart the entire system. All services are stopped, and the system goes through a complete shutdown process before restarting.

sudo systemctl reboot

Shut Down the System: It tells systemd to power off the machine. All services and processes are stopped, and the system is safely powered down.

sudo systemctl shutdown

journalctl

journalctl is a command used in Linux systems to query and display logs from the systemd journal, which is a system service that collects and stores logging data. Here’s a simple guide on how to use journalctl:

journalctl

journalctl -n 10 is used to display the last 10 entries (lines) from the systemd journal. Here’s a breakdown of what each part of the command does:

  • journalctl: Invokes the journalctl command, which is used to query and display logs from the systemd journal.
  • -n 10: Specifies the number of lines to display. In this case, -n 10 instructs journalctl to show the last 10 entries from the journal.
journalctl -n 10

journalctl -n 10

Securing a service using systemd

Securing a service using systemd is an essential practice to safeguard your system against potential vulnerabilities and unauthorized access. Here’s a simplified guide on how to accomplish this:

1. Understand Service Unit Files

  • Service Configuration: Each service in systemd is managed by a unit file, typically located in /etc/systemd/system/. These files define how the service operates, including its start-up behavior, resource limits, and security settings.

2. Restrict Service Permissions

  • User and Group Permissions: Specify the user and group under which the service runs. Avoid using the root user whenever possible to minimize the impact of security breaches.

3. Utilize systemd Security Features

  • PrivateTmp: Enable private /tmp and /var/tmp directories for the service to prevent information leakage between different services.
  • ProtectSystem and ProtectHome: Set these options to restrict access to system directories and user home directories, respectively, to enhance system security.
  • ReadOnlyPaths and ReadWritePaths: Specify which directories the service can read from or write to. Limiting write access can prevent unintended modifications to critical files.

4. Limit Resource Usage

  • Limit CPU and Memory: Use CPUQuota and MemoryLimit options to restrict the service’s CPU and memory usage, preventing resource exhaustion attacks.

5. Enable Network Controls

  • Restrict Network Access: Utilize RestrictAddressFamilies, IPAddressAllow, and IPAddressDeny options to control network access for the service, limiting its exposure to potential threats.

6. Set File System Permissions

  • File System Permissions: Ensure that the service’s files and directories have appropriate permissions (chmod) and ownership (chown) to prevent unauthorized access.

7. Regularly Update and Monitor

  • Keep System Updated: Regularly update your system and service software to patch security vulnerabilities and ensure that your security measures remain effective.
  • Monitor Logs: Monitor systemd journal logs (journalctl) for any suspicious activities or security-related events, and take appropriate action if necessary.

Conclusion

We discussed systemctl which is a vital command-line tool for managing system services in Linux, playing a crucial role in tasks such as starting, stopping, restarting, enabling, and disabling services. This article provided a comprehensive guide to systemctl, covering its syntax and key commands. From listing available systemd units to enabling or disabling automatic startup, each command contributes to effective service management. The article also addressed common questions, offering practical solutions for tasks such as checking service status, starting, stopping, and restarting services, and enabling or disabling automatic startup. Overall, understanding and mastering systemctl commands are essential for Linux administrators to ensure the stability and optimal performance of their systems.

Systemctl – FAQs

What is a Linux service?

A Linux service is a background process or application that runs independently of user interaction, providing specific functionality or performing system tasks. It is managed by the init system, typically systemd, and can be started, stopped, and managed using commands like systemctl.

What is the difference between Systemctl and service?

systemctl is a command-line tool used to manage systemd services, while service is a symbolic link to systemctl on many systems, providing compatibility with older init scripts.

How to check service status in Linux?

Use `systemctl status <service_name>` to see if the service is running, active (recently stopped), or inactive (never started). This reveals important information like memory usage and active connections.

How do I start, stop, restart a service?

For starting, use :

sudo systemctl start <service_name>

For stopping, use :

sudo systemctl stop <service_name>

To restart, use :

sudo systemctl restart <service_name>

Note : Remember to replace <service_name> with the actual service name.

How do I make a service start automatically at boot?

Use sudo `systemctl enable <service_name>` to set the service to start automatically when the system boots.

To disable automatic startup, use `sudo systemctl disable <service_name>`.



Previous Article
Next Article

Similar Reads

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 for managing services. In this article, we will ex
8 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 determine the cause for a restart in Kali Linux?
We can figure out what caused the reboot through a few different methods. We'll go through those methods in this post and use accessible programs and logs on a Linux system to diagnose such issues. It's not uncommon to discover that a Linux system has restarted unexpectedly or for no apparent reason. Finding and fixing the core cause might help pre
4 min read
Restart Network on Kali Linux
Kali Linux, renowned for its powerful tools for penetration testing and digital forensics, often requires a stable network connection for its myriad of functions. However, there are times when the network connectivity might falter due to various reasons such as misconfiguration, hardware issues, or simply the need for a fresh start. In such cases,
4 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, like systemctl, restarting services is a straightfor
3 min read
Restart Network Service on RHEL 9
RHEL 9 (Red Hat Enterprise Linux 9) is a powerful and widely used operating system in enterprise environments, renowned for its stability, security, and performance. In networking, RHEL 9 offers robust tools and utilities for managing network configurations and services. However, there are occasions when it becomes necessary to restart network serv
4 min read
How to Restart Network on Ubuntu 20.04
In Linux systems like Ubuntu, network management is essential for ensuring seamless connectivity between devices and the internet. Restarting the network is often necessary to troubleshoot issues, apply new configurations, or reset network interfaces. On Ubuntu 20.04, several methods exist to restart the network, each offering a different level of
4 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
How to Enable and Start SSH on Kali Linux
Secure Shell (SSH) is a protocol that provides a secure way to access a remote computer. It allows you to remotely log in to a system and execute commands as if you were sitting at the console. In this guide, we will explain how to enable and start SSH on Kali Linux, a popular Linux distribution used for penetration testing and digital forensics. W
5 min read
How to Setup ClipBucket to Start Video Sharing Website in Linux
ClipBucket is a free program that helps to make a video-sharing website. It is like YouTube, but you can create your own version. ClipBucket is easy to set up and use. It lets people upload videos, watch them, and connect with each other. The program can change video types and make small pictures of videos. It uses tools that make videos play well
10 min read
How to Compress and Extract Files Using the tar Command on Linux
Tar archives are special files that bundle multiple files and directories into a single file, making it easier to store, share, and manage. While tar archives themselves are not compressed, they can be combined with various compression utilities to reduce their size. The tar command is highly versatile, allowing users to create, modify, and extract
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
Scientific Calculator Using awk Command in Linux
The awk command in Linux allows the user to use the numeric functions, variables, logical operators and string functions. Basically, it is a utility that allows the user to write small and effective programs in the form the statements. It is mostly used in pattern scanning and processing. Here, we are using the awk command to perform the following
2 min read
Connecting to the Internet Using Command Line in Linux
Many of the times you may use a Linux system that does not have a GUI after install and it needs an internet connection to set up a desktop environment, also you may use Linux servers without a GUI and you need to connect over a wireless network using the command line. Below you will see Steps to connect to a wireless network using the command line
3 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
Disk cloning in Linux using dd command
Everyone likes to have a copy of the disk drive to have a chance to recover. Even If you don't want a copy now - you will want when your first HDD will be broken. It is used to clone all of the data from the initial etalon dump disk to several hosts disks. It will save you a huge portion-time. But what is the action options to clone the disk - let'
6 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
Using the eval command in Linux to run variables as commands
In Linux, the eval command serves as a powerful tool, enabling users to execute commands stored within variables. This functionality proves particularly useful for scripting and automation, allowing dynamic generation and execution of commands during runtime. By employing, users can manipulate and run strings as if they were direct commands, offeri
4 min read
How to Schedule Tasks Using at Command in Linux
The at command in Linux is used to schedule one-time tasks to be executed at a specified time in the future. It allows users to submit a command or script for execution at a later time, offering a convenient way to automate tasks without the need for complex cron jobs. The scheduled jobs are managed by the atd (at daemon) service, which runs in the
5 min read
How to Fix Checksum Error Using fsck Command in Linux?
Checksum errors are a common issue encountered on Linux systems when files or filesystems become corrupt due to various reasons, such as hardware failures or improper shutdowns. These errors can lead to data loss and system instability. The `fsck` (file system check) command is a powerful utility in Linux that can help identify and repair these err
5 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
How to Create a Text File Using the Command Line in Linux
The Linux command line is a powerful tool that allows you to manage your system with precision and speed. One of the most common tasks is creating a text file, which can be achieved using several commands and methods. Here we will learn some quick ways to create a text file from the Linux Command Line or Terminal. Table of Content Using the 'touch'
6 min read
Uninstall Node.JS Using Linux Command Line
To uninstall Node.js from your Linux system, you can use the command line, and the process will vary depending on how you originally installed it. Whether you used a package manager like apt, installed Node.js using nvm (Node Version Manager), or manually installed it from a binary file, you can easily remove Node.js from your system by following t
3 min read
Extract Emails From a Text File Using Grep Command in Linux
When dealing with large text files containing various information, it's often necessary to extract specific data such as email addresses. While manual extraction is possible, it can be time-consuming and error-prone. This is where the powerful grep command in Linux comes to our rescue. In this article, we'll explore how to use grep to efficiently e
4 min read
How to Display and Set Date and Time in Linux | date Command
date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.You must be the super-user (root) to change the date and time.  Syntax of Date Command in Linux date [OPTION]... [+F
6 min read
How to Stop ffmpeg Remotely?
In the Linux environment, FFmpeg is a powerful multimedia processing tool that enables users to convert, record, and stream audio and video files. When working with FFmpeg remotely, it may become necessary to stop its processes under certain circumstances. To stop FFmpeg remotely, you typically employ methods such as sending signals or commands to
5 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