Edited 2 weeks ago by ExtremeHow Editorial Team
FedoraSystemdServicesManagementCommand LineTerminalSoftwareSystem AdministrationConfigurationComputers
This content is available in 7 different language
Managing services on modern Linux distributions is important for system administrators and any user who needs to maintain control over the services running on a machine. Fedora, a popular Linux distribution, uses systemd as its system and service manager. This article explains how services can be effectively managed using systemd in Fedora. We'll cover topics such as starting, stopping, enabling, disabling, and checking the status of services, as well as creating custom service units. Whether you're new to Fedora or Linux systems in general, or you're an experienced user who needs a refresher, this comprehensive guide is useful for everyone.
Systemd is a tool that provides a system and service manager designed to replace traditional Unix System V
(sysv
) and BSD init
. It provides on-demand start of daemons, service monitoring, snapshot support, and more. Nicer relies on services. The core of systemd is managed through systemctl
command, which is responsible for checking and controlling the state of the systemd system and service manager.
Before we dive into practical usage of systemd, it's helpful to understand its main components:
Service units are the most common type of unit in systemd. These units contain settings that define what the service is, how it should be started or stopped, what it depends on, and other specific behavior.
A service unit file is usually located in /etc/systemd/system
or /usr/lib/systemd/system
and has the following syntax:
[Unit]
Description=My Sample Service
[Service]
ExecStart=/usr/bin/my-service
ExecStop=/usr/bin/my-service-stop
Restart=on-failure
[Install]
WantedBy=multi-user.target
Each service file typically contains the following sections:
systemctl
command is used to monitor and control systemd. Below are common tasks and how you can perform them using systemctl
:
To check the status of a service, use:
systemctl status my-service.service
This command will provide information about the service status, recent logs, and more.
You can use the following to start the service:
systemctl start my-service.service
Starting a service manually doesn't mean it will start automatically upon boot.
The following command is used to stop a service:
systemctl stop my-service.service
This command stops the service until manually restarted or rebooted.
To restart a service, use:
systemctl restart my-service.service
This operation stops the service (if it is running) and then starts it again.
To start a service automatically at system boot, enable it as follows:
systemctl enable my-service.service
Conversely, to disable a service so that it does not start automatically, use:
systemctl disable my-service.service
Enabling a service creates a symbolic link from the service's unit file to the system's boot-up configuration.
If you modify a unit file or configuration in the systemd tree, it is often necessary to reload systemd:
systemctl daemon-reload
This command informs systemd to scan for new or modified unit files.
Creating a custom service allows you to run your own applications as managed services. Here is a simple step-by-step way to create it:
Open a text editor and create a new service unit file in the /etc/systemd/system/
directory:
sudo nano /etc/systemd/system/my-custom-service.service
Fill it with the required service definition:
[Unit]
Description=Custom Service Example
[Service]
ExecStart=/usr/bin/custom-script.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
Make sure the script or program being called in ExecStart
has the proper permissions:
chmod +x /usr/bin/custom-script.sh
After creating the new unit file, reload systemd:
systemctl daemon-reload
Now start and enable your custom service:
systemctl start my-custom-service.service
systemctl enable my-custom-service.service
Your custom service should now be managed the same as any other system service.
Target endpoints are configurations that allow you to abstract the concept of runlevels in traditional init systems. They help manage dependencies between services and other units, providing a way to group multiple services and units together.
To find out the available targets on your system, use:
systemctl list-units --type=target
To change the target or runlevel, use:
systemctl isolate multi-user.target
This command is equivalent to changing the runlevel to multi-user mode.
Systemd comes with an integrated system journaling tool known as journalctl
. This log management tool consolidates the logs of all services managed by systemd into a central repository.
To view the logs for a specific service, use:
journalctl -u my-service.service
When monitoring logs in real-time as they are generated, use the following flag:
journalctl -u my-service.service -f
There can sometimes be problems in managing services smoothly. Knowing how to troubleshoot can be invaluable:
journalctl
to identify specific error messages.systemctl list-dependencies
command to view the dependencies of a service.
Efficient management of services is fundamental to system administration in Fedora and other Linux distributions that use systemd. This guide has shown you how to manage various aspects of services using systemctl, from performing basic tasks like starting and stopping services to creating custom service units and handling logs with journalctl. With these skills, you can ensure that your systems run smoothly and efficiently, automate tasks, and handle system services effectively.
Remember that in the Linux world, any action you perform on a system may require root privileges. Make sure you have the proper permissions and always back up important files before making changes to critical system configurations. Understanding systemd and the tools it provides is invaluable for effective Linux service management.
If you find anything wrong with the article content, you can