by danduran on Development 8 min read, Comments: 0 (Add Your Comment!)

Master Your Linux Server With These Powerful Systemd Commands!

TL;DR:

In this in-depth guide, you’ll discover how to master systemd—effortlessly controlling services, fine-tuning startup behavior, making sense of powerful logs with journalctl, and maximizing uptime.

Master Your Linux Server With These Powerful Systemd Commands!

Systemd is an init system and service manager that’s widely used on modern Linux distributions, including Debian. It’s designed to handle the startup, running, and shutdown of your services and daemons in a more efficient and parallelized manner than older init systems. With systemd, you have a unified way to manage services, sockets, timers, and more.

The primary tools to interact with systemd are:
- systemctl: Used to control and query the state of systemd services and units.
- journalctl: Used to view and query the systemd journal logs, providing insights into what’s happening on your system behind the scenes.


Checking if Your System Uses Systemd

Before we dive into controlling and managing services, let’s confirm that your system is using systemd.

stat /sbin/init  

If systemd is in use, /sbin/init will be a symlink to systemd. If you see something like /sbin/init -> /lib/systemd/systemd, congratulations—your system is running systemd.


Getting Started with systemctl

The systemctl command is your go-to tool for managing systemd units. A unit is a resource that systemd knows how to operate on. Common unit types include:
- Service units (e.g., ssh.service, nginx.service)
- Socket units
- Target units
- Mount units
- Timer units

For most daily tasks, you’ll deal primarily with service units.


Listing Services

List all currently running services:

systemctl list-units --type=service  

This shows all active services currently started by systemd.

List all services, including inactive:

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

This command is more exhaustive, showing services that might be stopped, disabled, or not loaded.

List all running services only:

systemctl list-units --type=service --state=running  

If you’re curious about just the currently operational services, this command filters them down.

List failed services:

systemctl list-units --failed  

Useful for diagnosing problems—failed services often mean there’s an issue in configuration, permissions, or dependencies.


Checking Dependencies and Status

Show dependencies of a particular service:

systemctl list-dependencies sshd.service  

This displays all units that sshd.service depends on and those that depend on it. It helps you understand how services interconnect.

Check if a service is enabled:

systemctl is-enabled htg-example.service  

This will tell you if the service is set to start automatically at boot.


Starting, Stopping, and Managing Services

When managing services, systemctl provides a straightforward, intuitive set of subcommands:

Start a service:

sudo systemctl start <example>.service  

Replace <example>.service with your target service name, e.g., nginx.service.

Check status of a service:

sudo systemctl status <example>.service  

Shows whether the service is active, running, and provides recent logs and other helpful info.

Stop a service:

sudo systemctl stop <example>.service  

Use this to bring a running service to a halt.

Restart a service:

sudo systemctl restart <example>.service  

Applies changes by fully restarting the service. Good for after configuration edits.

Reload a service (if supported):

sudo systemctl reload <example>.service  

Some services support a reload operation to apply new configs without a full restart.


Enabling and Disabling Services at Boot

System services can start automatically on boot if you enable them:

Enable a service:

sudo systemctl enable <example>.service  

Ensures the service starts automatically on subsequent boots.

Enable and start immediately:

sudo systemctl enable --now <example>.service  

This is a handy shortcut that enables the service and starts it right away.

Disable a service:

sudo systemctl disable <example>.service  

Prevents the service from starting automatically on the next reboot, but does not stop a currently running instance.


Advanced Service Management

Reloading the Daemon:
When you edit or add new service unit files, you must reload systemd’s daemon to reflect the changes:

sudo systemctl daemon-reload  

Re-executing the systemd daemon:
If systemd has been updated or you need the systemd daemon itself to restart with new binaries while the system is running:

sudo systemctl daemon-reexec  

Masking and Unmasking Services:
Masking a service prevents it from starting manually or automatically:

sudo systemctl mask <example>.service  

To revert this action:

sudo systemctl unmask <example>.service  

Editing Unit Files Safely:
Instead of manually editing files in /lib/systemd/system, use systemctl edit to create overrides in /etc/systemd/system/:

sudo systemctl edit <example>.service  

This opens a temporary file where you can override settings without altering the original unit file.

Viewing a Unit File:

systemctl cat <example>.service  

This shows the unit file content and any drop-ins applied.


Working with Targets

Targets are groupings of units. For instance, multi-user.target is analogous to runlevel 3 in SysV init.
You can change the system’s run level by isolating a target:

sudo systemctl isolate multi-user.target  

This brings the system into a non-graphical multi-user environment.


Exploring System Logs with journalctl

journalctl allows you to read the systemd journal, which contains logs from the kernel, systemd, and services. Using journalctl, you can quickly pinpoint issues or monitor activity over time.

View all logs:

journalctl  

This command outputs a massive list of system logs since the journal’s creation. Press q to quit.

Follow logs in real-time:

journalctl -f  

Similar to tail -f /var/log/syslog, this command shows you new log entries as they appear.


Filtering Logs by Time and Unit

Filtering by service unit:

journalctl -u <example>.service  

This shows all logs related to a particular service, making debugging service issues much easier.

Filtering by time (since a specific date/time):

journalctl --since "2024-12-10 08:00:00"  

Returns logs only after 8:00 AM on December 10, 2024. Adjust as needed.

Filtering until a specific time:

journalctl --until "2024-12-10 10:00:00"  

Returns logs recorded up to (but not after) 10:00 AM on December 10, 2024.

Combining since and until:

journalctl --since "2024-12-10 08:00:00" --until "2024-12-10 09:00:00"  

Perfect for examining a narrow one-hour window.

Filtering by Service and Time Together:

journalctl -u <example>.service --since "08:00:00" --until "09:00:00"  

This zeroes in on logs for <example>.service within that specific hour.

Relative Time Filters:

journalctl --since "1 hour ago"  

Shows logs from the last hour, helping in real-time troubleshooting.


Viewing Logs from Past Boots

journalctl differentiates logs by boot sessions, allowing you to view logs from previous reboots.

List all boots:

journalctl --list-boots  

This shows a list of previous boots with their Boot IDs and timestamps. 0 represents the current boot, -1 the previous boot, etc.

View logs from a previous boot:

journalctl --boot -1  

Replace -1 with another negative number to go further back.

Combining boots with units:

journalctl --boot -1 -u <example>.service  

Displays logs for <example>.service from the previous boot only.


Additional journalctl Options

Filtering by Priority:

journalctl -p err -u <example>.service  

Shows only error-level logs or more severe, helping you find critical issues quickly.

Filtering Kernel Messages:

journalctl -k  

Focuses on kernel messages exclusively.

Jumping to the Current Boot:

journalctl -b  

Equivalent to --boot 0, it shows logs from the current boot.


Another Example Using Provided Command

You mentioned this specific command:

journalctl -S "08:00:00" -u <example>.service  

In most cases, you should use --since:

journalctl --since "08:00:00" -u <example>.service  

This will print all logs from <example>.service since 8:00 AM today. If you omit the date, journalctl assumes the current day.


All Commands Mentioned for Reference

# Check if systemd is present  
stat /sbin/init

# Systemd service management and queries  
systemctl list-units --type=service  
systemctl list-units --all --type=service  
systemctl list-units --type=service --state=running  
systemctl list-units --failed  
systemctl list-dependencies sshd.service  
systemctl is-enabled htg-example.service

# Starting, stopping, and managing services  
sudo systemctl start <example>.service  
sudo systemctl status <example>.service  
sudo systemctl stop <example>.service  
sudo systemctl restart <example>.service  
sudo systemctl reload <example>.service  
sudo systemctl enable <example>.service  
sudo systemctl enable --now <example>.service  
sudo systemctl disable <example>.service

# Advanced service management  
sudo systemctl daemon-reload  
sudo systemctl daemon-reexec  
sudo systemctl mask <example>.service  
sudo systemctl unmask <example>.service  
sudo systemctl edit <example>.service  
systemctl cat <example>.service

# Working with targets  
sudo systemctl isolate multi-user.target

# Journalctl usage  
journalctl  
journalctl -f  
journalctl -u <example>.service  
journalctl --since "2024-12-10 08:00:00"  
journalctl --until "2024-12-10 10:00:00"  
journalctl --since "08:00:00" --until "09:00:00" -u <example>.service  
journalctl --list-boots  
journalctl --boot -1  
journalctl --boot -1 -u <example>.service  
journalctl -p err -u <example>.service  
journalctl -k  
journalctl -b  

Summary

systemctl helps you:
- List units (running, all, failed).
- Start, stop, restart, enable, and disable services.
- Check the status and dependencies of services.
- Reload and re-execute the systemd daemon after changes.

journalctl helps you:
- View and filter logs by service unit, time range, and boot sessions.
- Diagnose issues, monitor service behavior, and search historical logs.
- Filter by priority or kernel messages for more targeted troubleshooting.

Knowing these commands will help you confidently take control of your system’s services and understand what’s happening under the hood through the logs. Whether you’re troubleshooting a failing service, analyzing performance, or simply curious about what’s going on, systemctl and journalctl are essential tools in your toolbox.

No comments yet. Be the first to comment!