Managing services is an important part of administering any Linux system. Ubuntu uses systemd as its init system to control services on your system. Learning how to disable and enable services with systemd is a useful skill for any Ubuntu user or administrator.
Introduction to Systemd
Systemd has replaced Upstart as the default init system in Ubuntu 15.04 and newer versions. Some key things to know about systemd:
- It starts services and mounts based on unit files located in /lib/systemd/system/
- The main systemd command is
systemctl
which allows you to manage services - Unit files define things like the service description, dependencies, exec start/stop commands and more
Understanding systemd units is important for controlling services. Let‘s take a look at how they work.
Systemd Unit Files
The configuration files that define systemd units live under /lib/systemd/system. There are several types of unit files:
- Service – Defines how to start/stop a service
- Mount – Controls file system mounts
- Socket – For socket activation of services
- Target – Groups units via dependencies
The most common units you‘ll interact with are service units. Here‘s an example apache2.service unit file:
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/sbin/apachectl start
ExecReload=/usr/sbin/apachectl graceful
ExecStop=/usr/sbin/apachectl stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
This defines the apache2 service with start, reload and stop commands. It has dependencies under After=, and belongs to multi-user.target.
Now that you understand units, let‘s see how to manage services!
Disabling Services
To disable a service means stopping it from starting up automatically when the system boots. The service can still be manually started/stopped while the system is running.
Here are the main commands for disabling and enabling services:
# Disable service (won‘t start at boot)
sudo systemctl disable apache2
# Enable service (starts at boot)
sudo systemctl enable apache2
Let‘s disable apache2 as an example:
sudo systemctl disable apache2
Synchronizing state for apache2.service with sysvinit using update-rc.d...
Executing /usr/sbin/update-rc.d apache2 removesystemctl disable apache2.service
Removed /etc/systemd/system/multi-user.target.wants/apache2.service.
This removes the symlink to apache2.service from the multi-user.target.wants directory. Since the apache2 service is no longer wanted by multi-user.target, it will not start at boot.
You can confirm the disable status with:
sudo systemctl is-enabled apache2
disabled
To re-enable apache2 to auto-start on boot:
sudo systemctl enable apache2
Synchronizing state for apache2.service with sysvinit using update-rc.d...
Executing /usr/sbin/update-rc.d apache2 defaults systemctl enable apache2.service
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
This recreates the symlink that was removed by the disable command.
Now apache2 will once again start automatically on system boot. Confirm with:
sudo systemctl is-enabled apache2
enabled
The disable
and enable
commands are the proper systemd way to controlling services on boot.
Stopping/Starting Services
In addition to disabling services from auto-starting, you can manually stop and start them while the system is running.
This can be useful for temporarily stopping a problematic service, restarting a service to load new configs, or kicking off a service you disabled from boot.
# Stop a currently running service
sudo systemctl stop apache2
# Start a stopped service
sudo systemctl start apache2
# Restart service to reload config
sudo systemctl restart apache2
For example, to stop the apache2 service:
sudo systemctl stop apache2
This will terminate the apache2 process but keep it enabled to start at the next boot.
You can check that apache2 has stopped running with systemctl status
:
sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: e>
Active: inactive (dead) since Thu 2022-02-10 11:12:10 EST; 8s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 14916 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCES>
Feb 10 11:12:10 ubuntu systemd[1]: Stopping The Apache HTTP Server...
Feb 10 11:12:10 ubuntu systemd[1]: Stopped The Apache HTTP Server.
The Active: inactive (dead)
status shows apache2 is no longer running after being stopped.
To start apache2 while the system is running:
sudo systemctl start apache2
And you can confirm it‘s now in the active (running)
state:
sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: e>
Active: active (running) since Thu 2022-02-10 11:15:10 EST; 3s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 14916 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCES>
Feb 10 11:15:10 ubuntu systemd[1]: Started The Apache HTTP Server.
This shows how to manually control a service while the system is running.
Checking Service Status
In addition to systemctl status
you can use these commands to query service state:
# Is service enabled/disabled
systemctl is-enabled apache2
# Check if service is running
systemctl is-active apache2
# Check full service config status
systemctl status apache2 --full
For example to check if apache2 is currently active:
systemctl is-active apache2
active
systemctl is-active apache2
inactive
These status commands are useful for scripting service checks as well.
Conclusion
Learning systemd is essential for controlling services on modern Ubuntu servers. Key points covered include:
- Disabling services prevents automatic start on boot
- Stopping/starting controls services on a running system
- Unit files and targets define systemd services
- Commands like status, is-enabled, is-active check state
Now you know the basics of managing services with systemd on Ubuntu!