How to Enable/Disable Services on Ubuntu Systemd. (Update !!!)
Sometimes we require some services to start up automatically at boot up, such as web servers or ssh, and other times we need to terminate services that we no longer require and are eating CPU and RAM up.
What is Systemd and Why should you Care?
Systemd is a init system (first process started during booting) and a system manager providing a standard provides a standard process for controlling what programs run when a Linux system boots up. While systemd is compatible with SysV and Linux Standard Base (LSB) init scripts, systemd is meant to be a drop-in replacement for these older ways of getting a Linux system running. Systemd does more than starting the core programs running. It also starts a journal of system activity, manages services, automounts filesystems, logs events, set up a hostname, and other system tasks. That may sound good to you, but some developers hate it.
Manage Services
The main role of an init system is to set up all the components that need to be started once the kernel has been booted and manage services and daemons. It is itself a daemon process that continues running until the system is shut down.
For service management tasks, Systemd uses the concept of units, which can be services, sockets, mount points, devices, etc. Units are defined using text files in ini format. These files include information about the unit, its settings, and commands to execute. The unit file type is determined by the filename extensions. For ie. (.service), (.mount), (.device) and (.socket).
However, for most service management tasks, you may omit the .service suffix since Systemd is clever enough to recognize that you're presumably trying to operate on a service.
Mount points will automatically be translated into the appropriate .mount unit. For example, specifying /home is equivalent to home.mount. Similar to mount points, devices are automatically translated into the appropriate .device unit, therefore specifying /dev/sda2 is equivalent to dev-sda2.device.
Systemctl, on the other hand, is a command-line program for managing services and controlling Systemd. It is a component of the Systemd ecosystem, and it is installed by default on all systems.
List Current Units
We can use the list-units --type service --all
command to obtain a list of all active units that systemd is aware of.
$sudo systemctl list-units --type service --all
OUTPUT
UNIT LOAD ACTIVE SUB DESCRIPTION
apparmor.service loaded active exited Load AppArmor profiles
apport-autoreport.service loaded inactive dead Process error reports when automatic reporting is en
apport.service loaded active exited LSB: automatic crash report generation
apt-daily-upgrade.service loaded inactive dead Daily apt upgrade and clean activities
apt-daily.service loaded inactive dead Daily apt download activities
● auditd.service not-found inactive dead auditd.service
Here the output has the above columns:
- UNIT: The
systemd
unit name - LOAD: Whether the unit’s configuration has been parsed by
systemd
. The configuration of loaded units is kept in memory. - ACTIVE: A summary state about whether the unit is active or not. This is usually a fairly basic way to tell if the unit has started successfully or not.
- SUB: This is a lower-level state that indicates more detailed information about the unit. This often varies by unit type, state, and the actual method in which the unit runs.
- DESCRIPTION: A short textual description of what the unit is/does.
How to Enable and Disable Services in Systemd init
To start a service in systemd run the command as follow:
$ sudo systemctl start service-name
for instance; to start apache web service, run
$ sudo systemctl start apache2
to verify that the service is running:
$ sudo systemctl status apache2
OUTPUT
apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-06-09 09:24:14 UTC; 1 day 2h ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 3015 (apache2)
Tasks: 55 (limit: 2101)
CGroup: /system.slice/apache2.service
├─ 3015 /usr/sbin/apache2 -k start
├─98358 /usr/sbin/apache2 -k start
└─98359 /usr/sbin/apache2 -k start
Jun 09 09:24:03 WebServer systemd[1]: Starting The Apache HTTP Server...
Jun 09 09:24:14 WebServer systemd[1]: Started The Apache HTTP Server.
Jun 10 00:00:03 WebServer systemd[1]: Reloading The Apache HTTP Server.
Jun 10 00:00:04 WebServer systemd[1]: Reloaded The Apache HTTP Server.
Notice that the dot ("●") uses color on supported terminals to summarize the unit state at a glance.
White color indicates an "inactive" or "deactivating" state.
Red color indicates a "failed" or "error" state.
Green indicates an "active", "reloading" or "activating" state.
To Stop the Service
$ sudo systemctl stop apache2
Confirm that the service is not running
$ sudo systemctl stop apache2
OUTPUT
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Thu 2021-06-10 12:12:58 UTC; 2s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 315777 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
Main PID: 3015 (code=exited, status=0/SUCCESS)
Jun 09 09:24:03 WebServer systemd[1]: Starting The Apache HTTP Server...
Jun 09 09:24:14 WebServer systemd[1]: Started The Apache HTTP Server.
Jun 10 00:00:03 WebServer systemd[1]: Reloading The Apache HTTP Server.
Jun 10 00:00:04 WebServer systemd[1]: Reloaded The Apache HTTP Server.
Jun 10 12:12:57 WebServer systemd[1]: Stopping The Apache HTTP Server...
Jun 10 12:12:58 WebServer systemd[1]: apache2.service: Succeeded.
Jun 10 12:12:58 WebServer systemd[1]: Stopped The Apache HTTP Server.
Enable apache2 Service on bootup
$ sudo systemctl enable apache2
Disable apache2 Service on bootup
$ sudo systemctl disable apache2
Restart the Service
$ sudo systemctl restart apache2
Alternatively, if you also wish to enable and start the service at the same time you may execute
$ sudo systemctl enable -- now apache2
To Check whether the Service is currently configured to Start on the next bootup
$ sudo systemctl is-enabled apache2
OUPUT
enabled
To Check whether the Service is active
$ sudo systemctl is-active apache2
OUTPUT
active
How to remove Systemd Services completely (Bonus)
What if you installed a package, and later decide that you no longer need it.
How do you go about removing it completely?
First, stop the service
$ sudo systemctl stop service-name
Second, disable the service
$ sudo systemctl disable service-name
Removing the service in systemd
$ sudo rm /etc/systemd/system/service-name
$ sudo rm /etc/systemd/system/service-name/[related symlinks]
Reload systemd
$ sudo systemctl daemon-reload
Then, run reset-failed to reset any units from failed state
$ sudo systemctl reset-failed
You should be familiar with the systemctl command's fundamental features, which enable you to interface with and control your systemd instance.
While systemctl primarily manages the core systemd process, it also manages various components of the systemd ecosystem.
Also, other features, such as log management and user sessions, are handled by separate daemons and administration programs (journald/journalctl and logind/loginctl).
Taking the effort to become acquainted with these additional tools and daemons will make management easier and effective.