Systemctl Cheatsheet


Basic Service Management

  • Start a service
sudo systemctl start <service-name>
  • Stop a service
sudo systemctl stop <service-name>
  • Restart a service
sudo systemctl restart <service-name>
  • Reload service configuration
sudo systemctl reload <service-name>
  • Restart or reload if running
sudo systemctl reload-or-restart <service-name>
  • Show service status
systemctl status <service-name>
  • Check if service is active
systemctl is-active <service-name>
  • Check if service is enabled
systemctl is-enabled <service-name>

Service Configuration

  • Enable service at boot
sudo systemctl enable <service-name>
  • Disable service at boot
sudo systemctl disable <service-name>
  • Enable and start service
sudo systemctl enable --now <service-name>
  • Disable and stop service
sudo systemctl disable --now <service-name>
  • Mask service (prevent starting)
sudo systemctl mask <service-name>
  • Unmask service
sudo systemctl unmask <service-name>

Service Information

  • List all services
systemctl list-units --type=service
  • List all enabled services
systemctl list-unit-files --type=service --state=enabled
  • List all disabled services
systemctl list-unit-files --type=service --state=disabled
  • List failed services
systemctl list-units --type=service --state=failed
  • List all running services
systemctl list-units --type=service --state=running
  • Show service dependencies
systemctl list-dependencies <service-name>
  • Show reverse dependencies
systemctl list-dependencies --reverse <service-name>

System State Management

  • Reboot system
sudo systemctl reboot
  • Shutdown system
sudo systemctl poweroff
  • Suspend system
sudo systemctl suspend
  • Hibernate system
sudo systemctl hibernate
  • Switch to rescue mode
sudo systemctl rescue
  • Switch to emergency mode
sudo systemctl emergency
  • Get default target
systemctl get-default
  • Set default target
sudo systemctl set-default <target>

Target Management

  • List available targets
systemctl list-unit-files --type=target
  • Switch to target
sudo systemctl isolate <target>
  • Common targets
# Multi-user (CLI)
sudo systemctl isolate multi-user.target

# Graphical (GUI)
sudo systemctl isolate graphical.target

# Rescue mode
sudo systemctl isolate rescue.target

Logs and Journaling

  • View service logs
journalctl -u <service-name>
  • Follow service logs
journalctl -u <service-name> -f
  • View logs since boot
journalctl -u <service-name> -b
  • View logs for last hour
journalctl -u <service-name> --since "1 hour ago"
  • View logs with priority
journalctl -u <service-name> -p err
  • View system boot logs
journalctl -b
  • View kernel messages
journalctl -k
  • Clear journal logs
sudo journalctl --vacuum-time=2weeks
sudo journalctl --vacuum-size=100M

Unit File Management

  • Show unit file content
systemctl cat <service-name>
  • Edit unit file
sudo systemctl edit <service-name>
  • Edit full unit file
sudo systemctl edit --full <service-name>
  • Reload systemd configuration
sudo systemctl daemon-reload
  • Reset failed services
sudo systemctl reset-failed
  • Reset specific failed service
sudo systemctl reset-failed <service-name>

Custom Service Creation

  • Example service unit file (/etc/systemd/system/myapp.service)
[Unit]
Description=My Application
After=network.target

[Service]
Type=simple
User=myuser
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/myapp
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
  • Create and enable custom service
sudo cp myapp.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable myapp.service
sudo systemctl start myapp.service

Timers (Systemd Cron)

  • List active timers
systemctl list-timers
  • Example timer unit file (mytask.timer)
[Unit]
Description=Run mytask every 5 minutes

[Timer]
OnCalendar=*:0/5
Persistent=true

[Install]
WantedBy=timers.target
  • Example service for timer (mytask.service)
[Unit]
Description=My scheduled task

[Service]
Type=oneshot
ExecStart=/usr/local/bin/mytask.sh
  • Enable and start timer
sudo systemctl enable mytask.timer
sudo systemctl start mytask.timer

Environment and Variables

  • Show service environment
systemctl show-environment
  • Set environment variable
sudo systemctl set-environment VAR=value
  • Unset environment variable
sudo systemctl unset-environment VAR
  • Import environment file
sudo systemctl import-environment

Advanced Operations

  • Kill service processes
sudo systemctl kill <service-name>
  • Kill with specific signal
sudo systemctl kill -s SIGUSR1 <service-name>
  • Show service properties
systemctl show <service-name>
  • Show specific property
systemctl show <service-name> -p MainPID
  • Monitor service changes
systemctl monitor
  • Verify service configuration
systemd-analyze verify <service-name>

Troubleshooting

  • Check system state
systemctl status
  • List failed units
systemctl --failed
  • Analyze boot time
systemd-analyze
  • Show critical chain
systemd-analyze critical-chain
  • Show service startup time
systemd-analyze blame
  • Check configuration issues
systemd-analyze verify
  • Check if system is running
systemctl is-system-running

Common Service Operations

  • Web servers
sudo systemctl restart nginx
sudo systemctl restart apache2
sudo systemctl reload nginx
  • Databases
sudo systemctl start mysql
sudo systemctl start postgresql
sudo systemctl status redis
  • System services
sudo systemctl restart ssh
sudo systemctl status network
sudo systemctl restart systemd-resolved
  • Check network service
systemctl status NetworkManager
systemctl status systemd-networkd

Useful Patterns

  • Restart service if config changed
sudo systemctl reload-or-restart nginx
  • Check service before restart
nginx -t && sudo systemctl reload nginx
  • Enable multiple services
sudo systemctl enable nginx mysql redis
  • Check multiple service status
systemctl status nginx mysql redis
  • Filter services by pattern
systemctl list-units --type=service | grep -i apache
  • Show services using port
ss -tulpn | grep :80
systemctl status $(systemctl list-units --type=service --state=running | grep -i apache | awk '{print $1}')

Service Dependencies

  • Show what services depend on target
systemctl list-dependencies --reverse multi-user.target
  • Show what target depends on
systemctl list-dependencies multi-user.target
  • Check service conflicts
systemctl show <service-name> -p Conflicts
  • Check service requirements
systemctl show <service-name> -p Requires