Skip to content

Useful systemd commands in daily work

About the article

This is a continuation of the systemd series. In this article, I'm focusing on practical commands that are useful in daily admin work.

Basic service management

Starting, stopping and restarting services

Starting a service

systemctl start name

Stopping a service

systemctl stop name

Restarting a service

systemctl restart name

Reloading configuration without restarting service

systemctl reload name

Restart only if service is active

systemctl try-restart name

Restart or start if service is not running

systemctl restart-or-start name

Checking service status

Basic service status

systemctl status name

Check if service is active

systemctl is-active name

Check if service is enabled (autostart)

systemctl is-enabled name

Check if service is failed

systemctl is-failed name

Status of all services

systemctl list-units --type=service --all

Configuring autostart

Enable service autostart

systemctl enable name

Enable and start service

systemctl enable --now name

Disable service autostart

systemctl disable name

Disable autostart and stop

systemctl disable --now name

Enable service only for current boot

systemctl enable --runtime name

System monitoring

System logs analysis

Display journal for specific service

journalctl -u name

Display from current boot

journalctl -b

Display from previous boot

journalctl -b -1

Continuous display of new log entries

journalctl -f

Display only errors and warnings

journalctl -p err..warning

Logs from last 24 hours

journalctl --since "24 hours ago"

Logs from specific date/time

journalctl --since "2023-05-01 12:00:00" --until "2023-05-02 12:00:00"

Show logs disk usage

journalctl --disk-usage

Clear logs older than X days

journalctl --vacuum-time=7d

Limit logs size to 500MB

journalctl --vacuum-size=500M

Advanced log filtering

Logs for specific PID

journalctl _PID=1234

Logs for specific user

journalctl _UID=1000

Logs for specific execution path

journalctl _EXE=/usr/bin/httpd

Logs for specific host in container environment

journalctl _HOSTNAME=container1

Export logs to JSON format

journalctl -u name -o json

Export logs to file

journalctl -u name --since today > name.log

Network services management

Network status of specific interface

networkctl status eth0

List all network interfaces

networkctl list

Restart network configuration (if active)

systemctl restart systemd-networkd

Network services diagnostics

networkctl lldp

Advanced network options

Managing DNS service

systemctl restart systemd-resolved

Check DNS configuration

resolvectl status

Clear DNS cache

resolvectl flush-caches

Check DNS cache statistics

resolvectl statistics

Service configuration management

Edit service configuration file

systemctl edit name

Create configuration override

systemctl edit --full name

Check service file location

systemctl show -p FragmentPath name

Reload all unit files

systemctl daemon-reload

Check unit properties

systemctl show name

Configuration preview

Display service file

systemctl cat name

Display service dependencies

systemctl list-dependencies name

Display service properties

systemctl show name

Display specific property

systemctl show -p MainPID name

Systemd time analysis

System boot time

systemd-analyze

Longest starting services

systemd-analyze blame

Boot path

systemd-analyze critical-chain

Generate boot time chart

systemd-analyze plot > boot-chart.svg

Analyze all service dependencies

systemctl list-dependencies --all

Container and virtual machine management

List all containers

machinectl list

Start shell in container

machinectl shell container_name

Start shell as specific user in container

machinectl shell user@container_name

Restart container

machinectl reboot container_name

Stop container

machinectl poweroff container_name

Container status

machinectl status container_name

Nspawn - lightweight containers

Start container with specific directory as rootfs

systemd-nspawn -D /path/to/rootfs

Start container from image

systemd-nspawn -i image.raw

Start container with network

systemd-nspawn --network-bridge=br0 -D /path/to/rootfs

Power management

System shutdown

systemctl poweroff

System restart

systemctl reboot

System suspend

systemctl suspend

Hibernation

systemctl hibernate

Hybrid mode (suspend-then-hibernate)

systemctl hybrid-sleep

Schedule shutdown

systemctl poweroff --force --force

Cancel scheduled shutdown

systemctl cancel

Scheduled tasks status

systemctl list-jobs

Resource management

Check memory limits

systemctl show -p MemoryLimit name

Check CPU limits

systemctl show -p CPUQuota name

Check I/O limits

systemctl show -p IOWeight name

Set memory limit for service

systemctl set-property name MemoryMax=1G

Set CPU priority for service

systemctl set-property name CPUWeight=800

Set TasksMax limit for service

systemctl set-property name TasksMax=100

Systemd automation

Generating dependencies

Automatically generate dependencies for service

systemctl add-wants target.name name

Remove automatic dependency

systemctl remove-wants target.name name

Automation commands

Cascade restart services (all dependent)

systemctl restart --all name

Force reload all services

systemctl daemon-reexec

Generate system status report

systemd-analyze dump > systemd_report.txt

Important configuration files location

Path Description
/etc/systemd/system/ Unit files defined by administrator
/run/systemd/system/ Unit files generated during runtime
/lib/systemd/system/ Unit files provided by system packages
/etc/systemd/system.conf Global systemd configuration
/etc/systemd/user.conf Global configuration for user sessions
/etc/systemd/journald.conf Logging system configuration
/etc/systemd/logind.conf Session management configuration
/etc/systemd/networkd.conf Network management configuration
/etc/systemd/resolved.conf DNS service configuration

Advanced tips

  • To check systemd configuration consistency: systemd-analyze verify name.service
  • clear old systemd cache: rm -rf /var/lib/systemd/catalog/database
  • check failed units: systemctl --failed --all
  • set default target: systemctl set-default multi-user.target
  • temporarily start with different target: systemctl isolate rescue.target

Tip

Create your own aliases for frequently used systemd commands in your .bashrc file:

alias scs='systemctl status'
alias scr='systemctl restart'
alias jf='journalctl -f'
alias jfu='journalctl -fu'

These are just some of the useful commands. Systemd is a rich tool that offers many functionalities.