Salta ai contenuti

Systemd

systemd is the system and service manager for modern Linux distributions. Control services, units, and system startup.

Service Management

CommandDescription
systemctl start [service]Start a service
systemctl stop [service]Stop a service
systemctl restart [service]Restart a service
systemctl reload [service]Reload service configuration
systemctl reload-or-restart [service]Reload or restart if reload unsupported
systemctl status [service]Show service status
systemctl is-active [service]Check if service is running
systemctl is-enabled [service]Check if service starts on boot
systemctl enable [service]Enable service on boot
systemctl disable [service]Disable service on boot
systemctl enable --now [service]Enable and start service
systemctl disable --now [service]Disable and stop service

Listing Units & Services

CommandDescription
systemctl list-unitsList all loaded units
systemctl list-units --allList all units (loaded and not)
systemctl list-units --type=serviceList only service units
systemctl list-units --state=runningList running units
systemctl list-units --state=failedList failed units
systemctl list-unit-filesList all unit files and their status
systemctl list-unit-files --type=serviceList all service files
systemctl get-defaultShow default target (runlevel)
systemctl list-dependenciesShow unit dependencies

Checking Status

# Show service status with full details
systemctl status nginx

# View service logs
journalctl -u nginx

# Follow service logs
journalctl -u nginx -f

# View logs from last boot
journalctl -u nginx --since today

# View logs with timestamp
journalctl -u nginx --no-pager

# Show detailed service information
systemctl show nginx

# Check if service failed
systemctl is-failed nginx

# List failed services
systemctl list-units --state=failed

Service Units

Basic Service Operations

# Start service
systemctl start apache2

# Stop service immediately
systemctl stop apache2

# Graceful restart (stop and start)
systemctl restart apache2

# Reload configuration without stopping
systemctl reload apache2

# Check current status
systemctl status apache2

# View service file
systemctl cat apache2

# Edit service file
systemctl edit apache2

# Edit service file with full content
systemctl edit --full apache2

Enable/Disable Services

# Enable service to start on boot
systemctl enable nginx

# Disable service from starting on boot
systemctl disable nginx

# Enable and start immediately
systemctl enable --now nginx

# Disable and stop immediately
systemctl disable --now nginx

# Check if enabled
systemctl is-enabled nginx

# Undo any recent changes
systemctl revert nginx

Targets (Runlevels)

TargetOld RunlevelDescription
multi-user.target3Multi-user mode (no GUI)
graphical.target5Graphical mode with login
rescue.target1Single-user rescue mode
emergency.targetSEmergency shell
poweroff.target0Power off
reboot.target6Reboot

Managing Targets

# Get current default target
systemctl get-default

# Set default target
sudo systemctl set-default multi-user.target
sudo systemctl set-default graphical.target

# List available targets
systemctl list-units --type=target

# Switch to target
sudo systemctl isolate rescue.target
sudo systemctl isolate multi-user.target
sudo systemctl isolate graphical.target

# Reboot
sudo systemctl reboot

# Power off
sudo systemctl poweroff

# Suspend
sudo systemctl suspend

# Hibernate
sudo systemctl hibernate

# Hybrid sleep (suspend to RAM + disk)
sudo systemctl hybrid-sleep

Timers (Scheduled Tasks)

# List all timers
systemctl list-timers

# List all timers including inactive
systemctl list-timers --all

# Show specific timer status
systemctl status apt-daily.timer

# Start timer
systemctl start apt-daily.timer

# Stop timer
systemctl stop apt-daily.timer

# Enable timer on boot
systemctl enable apt-daily.timer

# View timer details
systemctl cat apt-daily.timer

# View associated service
systemctl cat apt-daily.service

Sockets

# List all sockets
systemctl list-sockets

# Show socket status
systemctl status ssh.socket

# Start socket
systemctl start ssh.socket

# Stop socket
systemctl stop ssh.socket

# Enable socket on boot
systemctl enable ssh.socket

# View socket configuration
systemctl cat ssh.socket

Logging with journalctl

# Show all journal entries
journalctl

# Show entries from specific service
journalctl -u nginx

# Follow logs (like tail -f)
journalctl -f

# Follow service logs
journalctl -u nginx -f

# Show last N lines
journalctl -n 50

# Show logs from last boot
journalctl -b

# Show logs from previous boot
journalctl -b -1

# Show logs from specific time
journalctl --since "2024-01-15 10:00:00"

# Show logs from today
journalctl --since today

# Show logs between times
journalctl --since "2024-01-15" --until "2024-01-16"

# Show logs from specific user
journalctl _UID=1000

# Show logs from priority (err, warning, notice, info, debug)
journalctl -p err

# Show logs with pager
journalctl

# Show logs without pager
journalctl --no-pager

# Show logs in JSON format
journalctl -o json

# Show logs in short format
journalctl -o short

Creating Custom Service Units

Basic Service File

# /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target

[Service]
Type=simple
User=myapp
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/app
Restart=on-failure
RestartSec=10s

[Install]
WantedBy=multi-user.target

Service with Multiple Commands

# /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target postgresql.service

[Service]
Type=notify
User=myapp
WorkingDirectory=/opt/myapp
Environment="PORT=8080"
ExecStartPre=/opt/myapp/bin/migrate
ExecStart=/opt/myapp/bin/start
ExecReload=/opt/myapp/bin/reload
ExecStop=/opt/myapp/bin/stop
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

Dependency Management

# Show unit dependencies
systemctl list-dependencies nginx

# Show dependencies recursively
systemctl list-dependencies --all

# Show only targets
systemctl list-dependencies --type=target

Troubleshooting

Check Service Issues

# View service status
systemctl status apache2

# Check for errors
journalctl -u apache2 -n 20

# Show full service details
systemctl show apache2

# Verify service file
systemctl cat apache2

# Test service configuration
/usr/sbin/apache2ctl configtest  # Apache example

# Reload daemon after editing unit files
sudo systemctl daemon-reload

# Restart service after changes
sudo systemctl restart apache2

Failed Services

# List failed units
systemctl list-units --state=failed

# Show failed service status
systemctl status [failed-service]

# View failed service logs
journalctl -u [failed-service] -n 50

# Reset failed state
systemctl reset-failed [service]

# Reset all failed services
systemctl reset-failed

Useful Aliases

# Add to .bashrc or .zshrc
alias sc='sudo systemctl'
alias scl='systemctl list-units'
alias scls='systemctl list-units --type=service'
alias scs='systemctl status'
alias scr='sudo systemctl restart'
alias scrs='sudo systemctl restart'
alias sce='sudo systemctl enable'
alias scd='sudo systemctl disable'
alias scst='sudo systemctl start'
alias scsp='sudo systemctl stop'
alias jl='journalctl'
alias jlu='journalctl -u'
alias jlf='journalctl -f'

Best Practices

  • Always use absolute paths in ExecStart
  • Set User and Group for security
  • Use Restart=on-failure for reliability
  • Add meaningful Description
  • Document dependencies with After/Before
  • Use Type=simple for most services
  • Set reasonable timeouts
  • Monitor logs with journalctl
  • Use reload instead of restart when possible
  • Validate unit files after changes

Resources

  • man systemd - systemd system manager
  • man systemctl - systemd control utility
  • man systemd.unit - Unit configuration
  • man systemd.service - Service unit type
  • man journalctl - Journal control

Last updated: 2026-03-30