systemctl: Linux services cheatsheet

A list of useful commands to manage linux services with systemctl.

  • Provide detailed information about the specified service, including its current state:
$ systemctl status your-service-name
  • To start / stop a service:
$ systemctl start your-service-name
$ systemctl stop your-service-name
  • To enable a service to start automatically at boot time:
$ systemctl enable your-service-name
  • To disable a service from starting automatically at boot time:
$ systemctl disable your-service-name
  • List all enabled (i.e., set to start at boot) service units on a system:
$ systemctl list-unit-files --type=service --state=enabled

UNIT FILE              STATE  
autovt@.service        enabled
getty@.service         enabled
kdump.service          enabled
nis-domainname.service enabled
sshd.service           enabled

5 unit files listed.
  • Lists service units that are both enabled and currently running on the system:
$ systemctl list-units --type=service --state=enabled,running

UNIT                     LOAD   ACTIVE SUB     DESCRIPTION               
dbus.service             loaded active running D-Bus System Message Bus  
sshd.service             loaded active running OpenSSH server daemon     
systemd-journald.service loaded active running Journal Service           
systemd-udevd.service    loaded active running udev Kernel Device Manager

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

4 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
  • Provides a list of names of all failed systemd units on the system:
$ systemctl --quiet --no-pager --failed --all --full --no-legend | awk '{print $1}'
  • Provides detailed information about the configuration and status of a service:
$ systemctl show sshd.service

Type=notify
Restart=on-failure
NotifyAccess=main
RestartUSec=42s
TimeoutStartUSec=1min 30s
TimeoutStopUSec=1min 30s
...
  • Check Status of Multiple Services:
$ systemctl status sshd.service httpd.service
  • List Failed Units:
$ systemctl --quiet --no-pager --failed
  • Check Dependencies of a Service:
$ systemctl list-dependencies sshd.service
  • Restart All Failed Services:
$ systemctl list-units --state=failed --no-pager --quiet | awk '{print $1}' | xargs systemctl restart
  • Show All Units Sorted by Startup Time:
$ systemctl list-units --all --no-pager --no-legend --sort=start
  • Monitor Journal in Real-time for a Service:
$ journalctl -fu sshd.service

Service file configuration example

Basic example of a systemd service file that is located in /etc/systemd/system/ with the name ‘example-service.service‘. This example is for a fictional service called ‘example-service‘.

# /etc/systemd/system/example-service.service

[Unit]
Description=Example Service
After=network.target

[Service]
ExecStart=/usr/bin/example-service
Restart=always
User=nobody

[Install]
WantedBy=default.target
  • Description: A human-readable description of the service.
  • After: Specifies that the service should start after network.target has been reached.
  • ExecStart: The path to the executable or command to start the service.
  • Restart: Indicates that the service should always be restarted if it exits.
  • User: The user under which the service will run.
  • WantedBy: Indicates the default target that should enable the service.

Reload systemd:

After creating or modifying a service file, you need to reload the systemd configuration to recognize the changes.

$ systemctl daemon-reload
Compartir:

This article was written by RoberMB

💻OS, ☁️Cloud, 🛡️Cybersecurity, ✈️Traveling #Linux, #Ansible, #AWS, #VMware, #Docker 🏴‍☠️ CEH v10, CPHE 🏴‍☠️ ... Always learning, always enjoying.

Leave a Reply

Your email address will not be published. Required fields are marked *