Systemctl is the command-line interface for managing systemd, the init system that controls services, processes, and system states on modern Linux distributions. It's your go-to tool for starting, stopping, and monitoring everything that runs on your server. Here's how it works.
If you've ever needed to restart a web server, check if a database is running, or configure a service to start automatically at boot, you've probably encountered systemctl. It's one of those commands that shows up constantly in Linux tutorials, yet many people use it without fully understanding what's happening behind the scenes.
Systemctl is the primary command-line tool for interacting with systemd, the init system and service manager that's become standard on most modern Linux distributions. Whether you're managing a production server or just tinkering with a VPS, understanding systemctl will make your life significantly easier.
Understanding systemd and its role
Before diving into systemctl itself, it helps to understand what systemd actually does. When your Linux system boots up, the kernel loads first, then immediately hands control over to an init system. This init system is responsible for starting all the services and processes that make your system usable.
For decades, Linux distributions used various init systems like SysV init and Upstart. These worked, but they had limitations, particularly around parallel service startup and dependency management. Systemd was designed to address these shortcomings while providing a unified approach to system and service management.
Systemd doesn't just start services; it monitors them, manages their dependencies, handles logging through journald, and even manages network configuration, timers, and mount points. It's a complete system management suite, and systemctl is your primary interface for controlling all of it.
The shift to systemd was controversial in the Linux community. Some appreciated the consistency and features it brought, while others felt it violated Unix philosophy by doing too many things. Regardless of where you stand on that debate, systemd has become the de facto standard on distributions like Ubuntu, Debian, Fedora, RHEL, and their derivatives.
What systemctl actually does
Systemctl is essentially a control panel for systemd. Every time you run a systemctl command, you're sending instructions to the systemd daemon, which then carries out the requested action.
The most common use case is managing services. A service in systemd terminology is any long-running process that the system needs to track, like a web server, database, or background application. Each service has a unit file that defines how it should start, what dependencies it has, and how systemd should handle it if it crashes.
When you run systemctl start nginx, systemctl communicates with systemd to read nginx's unit file, resolve any dependencies, and start the service. Similarly, systemctl stop nginx tells systemd to gracefully shut down the nginx process. The systemctl status nginx command queries systemd for information about the service's current state, recent logs, and process details.
Beyond basic service control, systemctl also manages the system state itself. You can use it to reboot, power off, or suspend your system. It handles enabling or disabling services so they start automatically at boot. It can reload service configurations without restarting them, mask services to prevent them from being started, and even isolate specific targets to change your system's operating mode.
How to use systemctl for common tasks
The systemctl command follows a consistent pattern: systemctl [command] [unit]. The command tells systemd what action to perform, and the unit specifies what to act upon. Most of the time, you'll be working with service units, which typically end in .service, though systemctl is smart enough to assume this extension if you don't provide it.
Starting and stopping services
Starting a service is straightforward:
sudo systemctl start apache2
This launches the Apache web server immediately. The service will run until you stop it or the system reboots. To stop a running service:
sudo systemctl stop apache2
Systemd attempts a graceful shutdown, giving the service time to finish current operations and clean up. If you need to restart a service, perhaps after changing its configuration:
sudo systemctl restart apache2
This stops the service completely and then starts it again. For services that support reloading their configuration without a full restart, you can use:
sudo systemctl reload apache2
Not all services support reload, so if this fails, you'll need to restart instead. There's also a hybrid command that reloads if possible and restarts if not:
sudo systemctl reload-or-restart apache2
Enabling services at boot
Starting a service only runs it until the next reboot. To make a service start automatically whenever the system boots:
sudo systemctl enable nginx
This creates symbolic links in systemd's configuration that tell it to start nginx during the boot sequence. The service won't start immediately when you enable it; it just configures future boot behavior. To both enable and start a service in one command:
sudo systemctl enable--now nginx
To prevent a service from starting at boot:
sudo systemctl disable nginx
Checking service status
One of systemctl's most useful features is its detailed status output:
systemctl status sshd
This shows whether the service is active, when it started, its process ID, memory usage, and recent log entries. The color coding makes it easy to see at a glance if something's wrong; green means active and running, red indicates a problem.
You can also check if a service is active or enabled without the full status output:
The failed services output is particularly valuable for troubleshooting. When something isn't working right, checking for failed services should be one of your first steps.
Viewing logs with journalctl
While not technically part of systemctl, journalctl is systemd's logging tool and works hand-in-hand with service management. To view logs for a specific service:
sudo journalctl -u nginx
This shows all log entries for nginx since the last boot. To follow logs in real-time, similar to tail -f:
sudo journalctl -u nginx -f
You can also view logs from previous boots, filter by time range, or show only entries since the last boot.
Understanding unit files
Every service systemctl manages has a corresponding unit file that defines how it should behave. These files typically live in /lib/systemd/system/ for distribution-provided services or /etc/systemd/system/ for custom or modified services.
The [Unit] section contains metadata and dependencies. After=network.target means this service should start after the network is up. The [Service] section defines what command to run and how to handle it. The [Install] section determines when the service should start during boot.
You can view a service's unit file:
systemctl cat nginx
If you need to modify a service's configuration, don't edit the main unit file directly. Instead, create an override:
sudo systemctl edit nginx
This creates a drop-in file in /etc/systemd/system/nginx.service.d/ that overrides specific settings without touching the original. After editing unit files or creating new ones, reload systemd's configuration:
sudo systemctl daemon-reload
Advanced systemctl features
Beyond basic service management, systemctl offers several advanced capabilities. You can mask a service to completely prevent it from starting, even if another service tries to activate it:
sudo systemctl mask apache2
This is stronger than just disabling; it creates a symlink to /dev/null, effectively making the service impossible to start. To undo this:
sudo systemctl unmask apache2
Systemd targets are similar to runlevels in older init systems. They represent different system states. You can switch targets to change your system's operating mode:
sudo systemctl isolate multi-user.target
This switches to multi-user mode without a graphical interface. To see your current target:
systemctl get-default
For system power management, systemctl provides commands that replace older tools:
These commands properly notify all services and ensure clean shutdowns.
Troubleshooting common issues
When services fail to start or behave unexpectedly, systemctl provides several diagnostic tools. Start by checking the service status and reading the error messages carefully. Failed services often show exactly what went wrong in their status output.
If the status output doesn't provide enough information, dig into the full logs:
sudo journalctl -u servicename -n100
This shows the last 100 log entries for that service. Look for error messages, permission issues, or configuration problems.
Dependency issues can prevent services from starting. Check what dependencies a service requires:
systemctl list-dependencies nginx
This shows a tree of everything that service needs to run. If a dependency is failed or missing, the service can't start.
Permission problems are common, especially with custom services. Make sure the service's user account has appropriate permissions for any files or directories it needs to access. You can see what user a service runs as in its unit file or status output.
For services that crash or fail repeatedly, check if they're set to restart automatically. You can modify the restart behavior by editing the unit file and adding or changing the Restart= directive in the [Service] section.
When to use systemctl vs other tools
While systemctl is the standard tool for service management on systemd-based distributions, it's not the only option. Older distributions or those using different init systems require different commands.
On SysV init systems, you'd use service command or invoke init scripts directly in /etc/init.d/. On systems using Upstart, you'd use the initctl command. However, most modern distributions have standardized on systemd, so systemctl is what you'll need to know.
Some applications provide their own management tools that sit on top of systemctl. For example, Apache has apachectl, and various control panels provide graphical interfaces. These tools often just wrap systemctl commands in a more user-friendly interface.
For container-based deployments, Docker and Kubernetes have their own service management paradigms. However, the underlying host system still uses systemd and systemctl. If you're working with containers, you might find our article on what is Docker and Docker Compose useful.
Security considerations
Systemctl requires root privileges for most operations, which means you'll need sudo access. This is intentional; managing services affects system-wide behavior and needs appropriate permissions.
When creating custom unit files, be careful about what commands you're telling systemd to run. Unit files that execute arbitrary code with root privileges represent a security risk if not properly controlled. Always validate the contents of unit files, especially if you're copying them from online sources.
Systemd includes security features like sandboxing and privilege restriction. Modern unit files often include directives like PrivateTmp=yes, ProtectSystem=strict, or NoNewPrivileges=yes to limit what a service can do even if it's compromised. If you're concerned about server security, our guide on how to secure SSH on a Linux server covers additional hardening steps.
Log files managed by journald can contain sensitive information. Be mindful of who has access to view logs and consider implementing log rotation and retention policies appropriate for your environment.
Frequently asked questions about systemctl
How do I create a custom systemd service?
Create a unit file in /etc/systemd/system/ with a .service extension. Include [Unit], [Service], and [Install] sections defining your service's behavior. Run sudo systemctl daemon-reload to make systemd aware of the new file, then enable and start it as needed.
What's the difference between systemctl start and systemctl enable?
Start launches the service immediately but doesn't affect boot behavior. Enable configures the service to start automatically at boot but doesn't start it right now. Use enable --now to do both at once.
Why does systemctl need sudo?
Managing services affects the entire system and requires elevated privileges. Without root access, users could interfere with system services, potentially causing instability or security issues.
How do I see all failed services?
Run systemctl list-units --state=failed to show all units that have failed. This includes services, mounts, and other unit types. Add --type=service to filter to just services.
Can I use systemctl on non-systemd distributions?
No, systemctl is specifically for systemd-based systems. Distributions using SysV init, Upstart, or OpenRC use different service management commands. Most major modern distributions have adopted systemd, though.
What does systemctl daemon-reload do?
It tells systemd to reload its configuration files from disk. You need to run this after creating or modifying unit files so systemd becomes aware of the changes. It doesn't restart any services.
Conclusion
Systemctl is your main interface for controlling services, managing boot behavior, and monitoring system health on modern Linux distributions. Whether you're running a web server, database, or custom application, knowing how to start, stop, enable, and troubleshoot services with systemctl is fundamental to Linux system administration.
The command's consistent syntax and detailed output make it approachable for beginners while offering the depth and flexibility that experienced administrators need. Once you understand the basic patterns, managing even complex multi-service deployments becomes straightforward.
Thanks for reading! If you're looking for infrastructure to run your Linux servers, xTom provides enterprise-grade dedicated servers and colocation services, while V.PS offers scalable, production-ready NVMe-powered VPS hosting perfect for any workload. We also offer IP transit, shared hosting, and a full range of IT services to support your infrastructure needs.
Ready to discuss your infrastructure needs? Contact our team to explore the right solution for your projects.