Systemd is the init system, collection of system management daemons and utilities that have become the new standard for managing services in Linux. Systemctl is a command-line tool to manage and control the systemd services. With systemctl, you can start, stop, list, enable and disable the systemd services from the command-line.
Systemd uses unit files based on purpose and resources. For instance, services have a unit file with .service extensions while device unit files have .device extensions. All unit files are stored at /etc/systemd/system/ and /lib/systemd/system. The files created at run time, boot time are stored in /run/systemd/system/.
In this guide, we'll show you how to manage services and units using Systemctl command on Ubuntu 20.04.
Requirements
- A system running on Ubuntu 20.04.
- A root password set in your server.
Install Systemctl
By default, systemctl comes preinstalled in all major operating systems.
You can check it with the following command:
# systemctl --version
You should see the installed version of systemctl in the following output:
systemd 245 (245.4-4ubuntu3)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid
To check the location of the systemd binaries and libraries, run the following command:
# whereis systemd
This is the output that you should get:
systemd: /usr/bin/systemd /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
And then, you must run the following command:
# whereis systemctl
This is the output that you should see:
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz
To check out whether systemd is running or not, use the following command:
# ps -ef | grep systemd
Below, the output that you should get:
root 259 1 0 10:34 ? 00:00:01 /lib/systemd/systemd-journald
root 284 1 0 10:34 ? 00:00:00 /lib/systemd/systemd-udevd
systemd+ 290 1 0 10:35 ? 00:00:00 /lib/systemd/systemd-networkd
message+ 309 1 0 10:35 ? 00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root 320 1 0 10:35 ? 00:00:00 /lib/systemd/systemd-logind
systemd+ 356 1 0 10:35 ? 00:00:00 /lib/systemd/systemd-resolved
root 657 1 0 10:35 ? 00:00:00 /lib/systemd/systemd --user
List Systemd Units
You can use the systemctl command to list all units, active units and inactive units.
To list all units, run the following command:
# systemctl list-units --all
You should get the following output:
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point
dev-disk-by\x2did-scsi\x2d0QEMU_QEMU_HARDDISK_drive\x2dscsi0\x2d0\x2d0\x2d0.device loaded active plugged QEMU_HARDDISK
dev-disk-by\x2did-scsi\x2d0QEMU_QEMU_HARDDISK_drive\x2dscsi0\x2d0\x2d0\x2d0\x2dpart1.device loaded active plugged QEMU_HARDDISK 1
dev-disk-by\x2dpartuuid-da29c858\x2d01.device loaded active plugged QEMU_HARDDISK 1
dev-disk-by\x2dpath-pci\x2d0000:00:05.0\x2dscsi\x2d0:0:0:0.device loaded active plugged QEMU_HARDDISK
dev-disk-by\x2dpath-pci\x2d0000:00:05.0\x2dscsi\x2d0:0:0:0\x2dpart1.device loaded active plugged QEMU_HARDDISK 1
dev-disk-by\x2duuid-29a0b164\x2d1ba1\x2d45a7\x2db23a\x2dcdb98f23edbc.device loaded active plugged QEMU_HARDDISK 1
dev-rfkill.device loaded active plugged /dev/rfkill
dev-sda.device loaded active plugged QEMU_HARDDISK
dev-sda1.device loaded active plugged QEMU_HARDDISK 1
To list all active units, run the following command:
# systemctl list-units
Below is the output that you should get:
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point
sys-devices-pci0000:00-0000:00:03.0-virtio0-net-eth0.device loaded active plugged Virtio network device
sys-devices-pci0000:00-0000:00:04.0-virtio1-net-eth1.device loaded active plugged Virtio network device
sys-devices-pci0000:00-0000:00:05.0-virtio2-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged QEMU_HARDDISK 1
sys-devices-pci0000:00-0000:00:05.0-virtio2-host2-target2:0:0-2:0:0:0-block-sda.device loaded active plugged QEMU_HARDDISK
sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS1
sys-devices-platform-serial8250-tty-ttyS10.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS10
sys-devices-platform-serial8250-tty-ttyS11.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS11
To list all inactive units, run the following command:
# systemctl list-units --all --state=inactive
This is the output that you should see:
UNIT LOAD ACTIVE SUB DESCRIPTION >
proc-sys-fs-binfmt_misc.mount loaded inactive dead Arbitrary Executable File Forma>
● tmp.mount not-found inactive dead tmp.mount >
ntp-systemd-netif.path loaded inactive dead ntp-systemd-netif.path >
acpid.service loaded inactive dead ACPI event daemon >
apt-daily-upgrade.service loaded inactive dead Daily apt upgrade and clean act>
apt-daily.service loaded inactive dead Daily apt download activities >
● auditd.service not-found inactive dead auditd.service >
● connman.service not-found inactive dead connman.service >
● console-screen.service not-found inactive dead console-
screen.service >
List Systemd Services
To list all services, run the following command:
# systemctl list-units --type=service
Below the output that you should get:
UNIT LOAD ACTIVE SUB DESCRIPTION
apache2.service loaded active running The Apache HTTP Server
console-setup.service loaded active exited Set console font and keymap
cron.service loaded active running Regular background program processing daemon
csm.service loaded active exited Cloud Service Manager
dbus.service loaded active running D-Bus System Message Bus
getty@tty1.service loaded active running Getty on tty1
grub-common.service loaded active exited LSB: Record successful boot for GRUB
To list all running services, run the following command:
# systemctl list-units --type=service --state=running
You should see the following output:
UNIT LOAD ACTIVE SUB DESCRIPTION
apache2.service loaded active running The Apache HTTP Server
cron.service loaded active running Regular background program processing daemon
dbus.service loaded active running D-Bus System Message Bus
getty@tty1.service loaded active running Getty on tty1
networkd-dispatcher.service loaded active running Dispatcher daemon for systemd-networkd
ntp.service loaded active running Network Time Service
rsyslog.service loaded active running System Logging Service
serial-getty@ttyS0.service loaded active running Serial Getty on ttyS0
ssh.service loaded active running OpenBSD Secure Shell server
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-networkd.service loaded active running Network Service
systemd-resolved.service loaded active running Network Name Resolution
systemd-udevd.service loaded active running udev Kernel Device Manager
user@0.service loaded active running User Manager for UID 0
To list all inactive services, run the following command:
# systemctl list-units --type=service --state=inactive
See below the output that you should get:
UNIT LOAD ACTIVE SUB DESCRIPTION
acpid.service loaded inactive dead ACPI event daemon
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
● connman.service not-found inactive dead connman.service
● console-screen.service not-found inactive dead console-screen.service
Check a Service Status
You can also check the status of a specific service using the systemctl command.
For example, run the following command to check the status of the Apache service:
# systemctl status apache2
This is the output that you should get:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-07-21 11:27:53 UTC; 2min 57s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 1986 (apache2)
Tasks: 55 (limit: 2353)
Memory: 5.3M
CGroup: /system.slice/apache2.service
├─1986 /usr/sbin/apache2 -k start
├─1988 /usr/sbin/apache2 -k start
└─1989 /usr/sbin/apache2 -k start
You can also use the following commands to check whether the specified service is in active, enabled or failed status:
# systemctl is-active apache2
# systemctl is-enabled apache2
# systemctl is-failed apache2
Start and Stop a Service
You can easily start any service using the systemctl command.
To start the Nginx service, run the following command:
# systemctl start nginx
To stop the Nginx service, run the following command:
# systemctl stop nginx
If you want to restart or reload the service, run the following command:
# systemctl restart nginx
# systemctl reload nginx
Enable and Disable a Service
When you start any service using the systemctl command, it will not start automaticaly after the system reboot. You'll need to enable that service to start at system reboot.
To enable the Nginx service, run the following command:
# systemctl enable nginx
To disable the service to start at system reboot, run the following command:
# systemctl disable nginx
Mask and Unmask a Service
Masking a service prevents the service from being started manually or automatically.
To mask the MySQL service, run the following command:
# systemctl mask mysql
To unmask the MySQL service, run the following command:
# systemctl unmask mysql
If you want to kill all processes and child processes of the Apache service, run the following command:
# systemctl kill apache2
Display Properties of a Service
To display the properties of the Apache service, run the following command:
# systemctl show apache2
To display the content of the Apache service file, run the following command:
# systemctl cat apache2
Below, the output that you should get:
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=https://httpd.apache.org/docs/2.4/
[Service]
Type=forking
Environment=APACHE_STARTED_BY_SYSTEMD=true
ExecStart=/usr/sbin/apachectl start
ExecStop=/usr/sbin/apachectl stop
ExecReload=/usr/sbin/apachectl graceful
PrivateTmp=true
Restart=on-abort
[Install]
WantedBy=multi-user.target
In order to display dependencies of the Apache service, run the following command:
# systemctl list-dependencies apache2
This is the output that you should get:
apache2.service
● ├─-.mount
● ├─system.slice
● └─sysinit.target
● ├─dev-hugepages.mount
● ├─dev-mqueue.mount
● ├─keyboard-setup.service
● ├─kmod-static-nodes.service
● ├─proc-sys-fs-binfmt_misc.automount
● ├─setvtrgb.service
● ├─sys-fs-fuse-connections.mount
● ├─sys-kernel-config.mount
● ├─sys-kernel-debug.mount
● ├─sys-kernel-tracing.mount
● ├─systemd-ask-password-console.path
● ├─systemd-binfmt.service
● ├─systemd-boot-system-token.service
● ├─systemd-hwdb-update.service
Conclusion
We hope you have gotten acquinted with how to use the systemctl to interact and control your systemd services.
You can also use systemctl to troubleshoot the configuration related issues.