How to Start, Stop and Restart Services in Linux Using systemctl CommandSecuring a service using systemd

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

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

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

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

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

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

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

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 -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

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>`.



Contact Us