Systemd Cheatsheet - Linux Service Management Commands | Online Free DevTools by Hexmos

Master Linux service management with our Systemd cheatsheet. Learn essential systemctl and journalctl commands for controlling and monitoring services.

Systemd Cheatsheet

Systemd Cheatsheet

This cheatsheet provides essential commands for managing services and viewing logs using systemd, the init system used in many modern Linux distributions. Understanding these commands is crucial for system administrators and developers working with Linux environments.

systemctl: Service Management

The systemctl command is the primary tool for controlling the systemd system and service manager. It allows you to start, stop, restart, enable, disable, and check the status of services.

Checking Service Status

To view the current status of a service (e.g., Nginx), use the status command:

sudo systemctl status nginx

Restarting a Service

To gracefully restart a running service, use the restart command:

sudo systemctl restart nginx

Enabling and Disabling Services

To ensure a service starts automatically on boot:

sudo systemctl enable nginx

To prevent a service from starting automatically on boot:

sudo systemctl disable nginx

journalctl: Viewing Logs

The journalctl command is used to query and display messages from the systemd journal. This is where systemd collects logs from all services and the kernel.

Tailing Unit Logs

To continuously monitor the logs of a specific unit (service) in real-time, similar to tail -f:

sudo journalctl -fu nginx

The -f flag follows the log output, and the -u flag specifies the unit.

Viewing Recent Logs

To view the last 100 log entries for a unit and then exit (without continuous tailing):

sudo journalctl -fu nginx -n 100 --no-pager

The -n 100 option limits the output to the last 100 lines, and --no-pager prevents the output from being piped through a pager like less, displaying it directly.

Filtering Logs by Time

You can also filter logs by time using options like --since and --until:

sudo journalctl -u nginx --since "1 hour ago"
sudo journalctl -u nginx --since "2023-10-27 10:00:00" --until "2023-10-27 11:00:00"

Further Resources