Back to Blog
Lesson 40 of the Linux: Linux Command Line for Developers course
LinuxAugust 28, 20264 min read

Managing System Services with Systemd: A Linux Guide

Learn to manage Linux background services using systemd. Master systemctl to start, stop, and enable services, and use journalctl for effective log debugging.

linuxsystemdsystemctldevopsservices
From below of monitor of modern computer with opened files on blue screen

Previously in this course, we looked at monitoring disk usage to ensure our server stays healthy. Now, we shift our focus to the "brains" of the background: how to control the processes that keep your web server running continuously.

In modern Linux distributions, systemd acts as the service manager. It is responsible for starting your web server when the machine boots, restarting it if it crashes, and managing the logs it generates.

Understanding systemd and systemctl

systemd is the first process to run (PID 1) after the kernel boots. It manages "units," which are resources like services, mount points, or sockets. The primary tool you will use to interact with these units is systemctl.

Think of systemctl as your remote control for every background process on the machine. Whether you need to stop a misbehaving service, check if a process is running, or ensure your web server starts automatically after a reboot, systemctl handles it.

Common systemctl Commands

CommandAction
systemctl status <name>Check if a service is running and view recent logs
systemctl start <name>Immediately start a service
systemctl stop <name>Immediately stop a service
systemctl restart <name>Stop and start a service (useful after config changes)
systemctl enable <name>Configure a service to start on boot
systemctl disable <name>Prevent a service from starting on boot

Note: Most of these commands require root privileges, so you will often need to prefix them with sudo.

Worked Example: Managing a Web Service

In our ongoing project, we want to ensure our web server is not just running, but persistent. Let's assume you have an Nginx server installed.

  1. Check the status: Run sudo systemctl status nginx. You will see the current state (active/inactive), the process ID (PID), and the last few lines of log output.

  2. Stop the service: If you need to perform maintenance, stop it safely: sudo systemctl stop nginx

  3. Enable on boot: To ensure the server survives a reboot, you must "enable" it: sudo systemctl enable nginx

  4. Verify changes: Check the status again. It should now indicate that the service is "enabled."

Debugging with journalctl

When a service fails to start, the standard output often isn't enough. systemd captures everything in the system journal, which you access via journalctl.

Instead of manually digging through /var/log, you can filter logs by the unit name:

Bash
# View all logs for nginx
journalctl -u nginx

# Follow logs in real-time (useful for debugging a crash)
journalctl -u nginx -f

# View only the last 50 lines
journalctl -u nginx -n 50

As we discussed in network ports and services, knowing which process is listening on a port is only half the battle; knowing how to manage that process with systemd is what makes you a true operator.

Hands-on Exercise

For this lesson's project task, we are going to ensure our web server configuration is robust.

  1. Identify the name of your web server service (e.g., nginx or apache2).
  2. Run sudo systemctl status <your-service> and note the status.
  3. If it is not enabled, run sudo systemctl enable <your-service>.
  4. Run sudo systemctl restart <your-service>.
  5. Use journalctl -u <your-service> -n 20 to verify that the restart occurred without errors.

Common Pitfalls

  • Forgetting sudo: You will get an "Access Denied" error if you try to manage system-wide services as a standard user.
  • Confusing "Start" and "Enable": start only affects the current session. If you don't enable the service, it will vanish after a reboot.
  • Assuming logs are in files: While some apps still log to /var/log, journalctl is the source of truth for services managed by systemd. Always check there first.

FAQ

What is the difference between reload and restart? restart kills the process and starts it fresh. reload sends a signal to the process to re-read its configuration files without dropping active connections—use reload whenever possible to minimize downtime.

Can I create my own services? Yes! You can create .service files in /etc/systemd/system/. We will cover service definitions in a later advanced module, but for now, focus on controlling existing ones.

Recap

You now have the power to control your server's lifecycle. We've covered how to use systemctl to manipulate service states and journalctl to diagnose issues. These skills, combined with the introduction to cron jobs we learned earlier, provide the foundation for robust server management.

Up next: Security Auditing Basics — identifying open sockets and potential security risks.

Similar Posts