Skip to content
abdallahmekky

CHEAT SHEET

systemd cheat sheet

Services, logs, boot targets and root password recovery on RHEL. The daily tools of a Linux administrator.

All cheat sheets

Services

systemctl status sshd              # state, PID and the latest log linessystemctl start sshd               # start nowsystemctl stop sshd                # stop nowsystemctl restart sshd             # stop, then startsystemctl reload sshd              # re-read the config without stoppingsystemctl enable sshd              # start at bootsystemctl enable --now sshd        # enable and start togethersystemctl disable --now sshd       # disable and stop togethersystemctl mask sshd                # make it impossible to startsystemctl unmask sshd              # undo masksystemctl is-active sshd           # active or inactivesystemctl is-enabled sshd          # enabled or disabled

Listing and unit files

systemctl list-units --type=service          # loaded servicessystemctl list-units --failed                # units that failedsystemctl list-unit-files --state=enabled    # what starts at bootsystemctl list-dependencies sshd             # what a unit needssystemctl cat sshd                           # print the unit filesystemctl edit sshd                          # create a drop-in overridesystemctl daemon-reload                      # after changing unit files

journalctl

journalctl -u sshd                 # logs of one unitjournalctl -u sshd -f              # follow new linesjournalctl -b                      # this boot onlyjournalctl -b -1                   # the previous boot (needs a persistent journal)journalctl -p err                  # errors and worsejournalctl --since "1 hour ago"    # a time windowjournalctl --since today           # since midnightjournalctl -k                      # kernel messagesjournalctl --disk-usage            # space used by the journal

With the default Storage=auto, journald keeps logs only in memory unless /var/log/journal exists. Check with ls /var/log/journal. To make sure logs survive reboots:

mkdir -p /var/log/journalsystemctl restart systemd-journald

Boot targets

TargetWhat you get
graphical.targetMulti-user with a graphical login
multi-user.targetText mode, networking, no GUI
rescue.targetSingle-user shell, basic services
emergency.targetMinimal shell, root file system read-only
systemctl get-default                       # show the default targetsystemctl set-default multi-user.target     # text mode at bootsystemctl set-default graphical.target      # graphical login at bootsystemctl isolate rescue.target             # switch right nowsystemctl reboot                            # restartsystemctl poweroff                          # shut down

Root password recovery

At the GRUB menu press e, add rd.break to the end of the line that starts with linux, then press Ctrl+x. You land in the initramfs shell:

mount -o remount,rw /sysroot     # make the real root writablechroot /sysroot                  # step into itpasswd root                      # set the new passwordtouch /.autorelabel              # SELinux relabels on the next bootexit                             # leave the chrootexit                             # continue booting
Kernel parameterEffect
rd.breakStop in the initramfs, before the root file system is mounted for real
systemd.unit=rescue.targetBoot into rescue mode
systemd.unit=emergency.targetBoot into emergency mode
enforcing=0Boot SELinux in permissive mode once

Timers and boot time

systemctl list-timers              # scheduled timerssystemctl enable --now fstrim.timer    # enable a timersystemd-analyze                    # how long the boot tooksystemd-analyze blame              # slowest units first

A minimal service unit

# /etc/systemd/system/example.service[Unit]Description=Example serviceAfter=network.target[Service]ExecStart=/usr/local/bin/exampleRestart=on-failureUser=appuser[Install]WantedBy=multi-user.target

After creating or editing it: systemctl daemon-reload, then systemctl enable --now example.

More cheat sheets

Questions

How do I start a service now and at every boot?

Use systemctl enable --now service. It enables the service for boot and starts it immediately.

How do I see the logs of one service?

Use journalctl -u service. Add -f to follow new lines, -b for the current boot, or --since "1 hour ago" to limit the time.

How do I change the default boot target?

Use systemctl set-default multi-user.target for text mode or graphical.target for a graphical login, then reboot.

How do I reset a forgotten root password on RHEL?

Add rd.break to the kernel line in GRUB, boot, remount /sysroot read-write, chroot into it, run passwd root, then touch /.autorelabel and reboot.