Systemctl Cheatsheet
Basic Service Management
sudo systemctl start <service-name>
sudo systemctl stop <service-name>
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>
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
sudo systemctl enable <service-name>
sudo systemctl disable <service-name>
sudo systemctl enable --now <service-name>
sudo systemctl disable --now <service-name>
- Mask service (prevent starting)
sudo systemctl mask <service-name>
sudo systemctl unmask <service-name>
Service Information
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
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
sudo systemctl reboot
sudo systemctl poweroff
sudo systemctl suspend
sudo systemctl hibernate
sudo systemctl rescue
sudo systemctl emergency
systemctl get-default
sudo systemctl set-default <target>
Target Management
systemctl list-unit-files --type=target
sudo systemctl isolate <target>
# 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
journalctl -u <service-name>
journalctl -u <service-name> -f
journalctl -u <service-name> -b
journalctl -u <service-name> --since "1 hour ago"
journalctl -u <service-name> -p err
journalctl -b
journalctl -k
sudo journalctl --vacuum-time=2weeks
sudo journalctl --vacuum-size=100M
Unit File Management
systemctl cat <service-name>
sudo systemctl edit <service-name>
sudo systemctl edit --full <service-name>
- Reload systemd configuration
sudo systemctl daemon-reload
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)
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
sudo systemctl enable mytask.timer
sudo systemctl start mytask.timer
Environment and Variables
systemctl show-environment
sudo systemctl set-environment VAR=value
- Unset environment variable
sudo systemctl unset-environment VAR
sudo systemctl import-environment
Advanced Operations
sudo systemctl kill <service-name>
- Kill with specific signal
sudo systemctl kill -s SIGUSR1 <service-name>
systemctl show <service-name>
systemctl show <service-name> -p MainPID
systemctl monitor
- Verify service configuration
systemd-analyze verify <service-name>
Troubleshooting
systemctl status
systemctl --failed
systemd-analyze
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
sudo systemctl restart nginx
sudo systemctl restart apache2
sudo systemctl reload nginx
sudo systemctl start mysql
sudo systemctl start postgresql
sudo systemctl status redis
sudo systemctl restart ssh
sudo systemctl status network
sudo systemctl restart systemd-resolved
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
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
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
systemctl show <service-name> -p Conflicts
- Check service requirements
systemctl show <service-name> -p Requires