As a Linux system administrator, having a deep understanding of systemctl is essential for effectively managing services in Ubuntu and other systemd-based distributions. systemctl is a powerful command-line tool that allows you to control the systemd init system, which handles service management on most modern Linux distributions.
In this comprehensive guide, we will explore the ins and outs of systemctl, including how to start, stop, enable, and check the status of services. Whether you are a systems engineer, DevOps practitioner, or Linux enthusiast, this guide will give you the systemctl mastery needed to control services with ease.
An Introduction to systemd and systemctl
Before diving into systemctl usage, it helps to understand what systemd is and how it works. systemd is a system and service manager for Linux operating systems that is widely used by modern Linux distributions like Ubuntu, RHEL, CentOS, etc.
Some key facts about systemd:
-
It is a replacement for the old SysV init system used by traditional Linux distributions.
-
For any given service, systemd uses unit files that define different aspects of how the service should run.
-
systemd handles dependencies between services automatically. This makes starting and stopping complex nested services easier.
-
systemctl is the command-line tool for talking to and managing systemd. It is the main interface system administrators use to control systemd.
With systemd being central to service management on Ubuntu systems, grasping systemctl is crucial for control and observability.
systemctl Basics and Service Commands
systemctl has a number of helpful subcommands for managing services such as:
start – starts a service
stop – stops a running service
restart – restarts a service
reload – reloads config without stopping service
enable – enables a service to start on boot
disable – disables starting a service on bootup
status – shows current service status
The basic syntax for systemctl when working with services is:
sudo systemctl [command] [unit-name.service]
For example, to start the ssh service:
sudo systemctl start ssh.service
Note that most unit files for system services end in .service. This signifies it is a service unit file rather than other unit file types.
Managing Services in Ubuntu with systemctl
Let‘s explore practical examples of controlling services with systemctl while covering some best practices along the way.
Starting and Stopping Services
Starting and stopping services temporarily is one of the most common systemctl uses.
To start a service:
sudo systemctl start <service-name>
For example to start apache:
sudo systemctl start apache2.service
To stop a running service:
sudo systemctl stop <service-name>
For example, to stop postgresql:
sudo systemctl stop postgresql.service
Keep in mind start/stop commands don‘t persist across reboots. The service will revert to however it is configured to run on boot.
Restarting and Reloading Services
Another common task is restarting or reloading service configurations without interrupting service availability where possible.
To restart a service:
sudo systemctl restart <service-name>
For example:
sudo systemctl restart networking.service
To reload a service‘s configuration without full restart:
sudo systemctl reload <service-name>
For example, reloading sshd_config changes:
sudo systemctl reload sshd.service
This allows applying config changes on the fly without disconnecting users as would happen during a full ssh restart.
Enabling Services to Start on Boot
When managing Ubuntu servers, you generally want services to start automatically on system boot rather than having to manually start them later.
To enable a service on boot:
sudo systemctl enable <service-name>
For example:
sudo systemctl enable nfs-server.service
This links the service unit file to systemd targets that start services after boot finishes.
Disabling Unneeded Services from Autostarting
Just as you enable services to run on boot, you can also disable them from automatic startup when not needed.
To disable automatic startup:
sudo systemctl disable <service-name>
For example, to prevent mysql from starting up with the system:
sudo systemctl disable mysql
This removes the symlinks that allow automatic service starts.
Checking Service Status
systemctl also allows checking status of both running and stopped services. This gives visibility into whether a service reached activation or perhaps failed to start.
To check status with systemctl:
systemctl status <service-name>
For example:
systemctl status bluetooth
This will print out detailed service status, exit codes, and logs. The output will indicate if the service successfully started and any errors.
Listing Loaded Units
In addition to querying individual services, you can use systemctl to output a list of all the currently loaded systemd units. This acts as an overview of everything systemd is managing.
To see all active systemd units:
systemctl list-units --type=service
This will output a table of all the loaded .service unit files including their current state. Other types of units can be listed by changing the --type filter.
Masking Units to Prevent Accidental Startup
Sometimes you want to ensure that a particular service or unit can never start accidentally or due to a systemctl wildcard.
To accomplish this, you can "mask" the unit from starting:
sudo systemctl mask <unit-name>
For example:
sudo systemctl mask bluetooth.service
This will prevent bluetooth.service from ever being started by systemctl. If you did try to start it manually, it would print a warning that the unit has been masked.
Get Unit Files to Debug Issues
When debugging issues with failing services, it helps to directly access the unit definition file itself.
To cat out a unit file definition from systemd‘s catalog:
systemctl cat <unit-name>
For example, to view sshd.service unit contents:
systemctl cat sshd.service
This allows you to inspect runtime options, environment variables, execution start up commands, and more as defined for the service.
Conclusion
Learning systemctl provides vast power when it comes to controlling Ubuntu services. It transforms how you interact and observe the processes running on your Linux servers.
From starting and stopping services during maintenance to enabling persistent boot behavior, systemctl gives you fine grained access to manage systemd-driven services. As you grow into senior sysadmin roles, having systemctl skills will serve you well in keeping Ubuntu and other production Linux infrastructure humming smoothly.


