Master Systemd: Your Complete Guide to Systemctl on Linux
Want to take control of your Linux server? Managing services is key, and systemd
with systemctl
is the new standard. This guide will show you how to use systemctl
to easily manage services, check their status, and configure them to start automatically. Let's dive in and learn how to use systemctl
for efficient Linux system management!
What is Systemd and Why Use Systemctl?
Systemd
is the init system that has become the standard for most Linux distributions. Think of it as the conductor of an orchestra, ensuring all necessary components start smoothly after the kernel boots. The systemctl
command is your management tool for systemd
, enabling you to control services, inspect system states, and tweak configurations. This guide provides everything you need to know to manage systemctl
services.
If you receive a bash: systemctl is not installed
error, your machine is likely using a different init system.
Core Systemctl Commands for Service Management
The primary function of any init system is to manage services and daemons. Here’s how to use systemctl
for common service management tasks.
-
Start a Service:
sudo systemctl start application.service
This command initiates the service, executing the instructions defined in its unit file.
-
Stop a Service:
sudo systemctl stop application.service
This halts a currently running service.
-
Restart a Service:
sudo systemctl restart application.service
This restarts a service, useful for applying configuration changes.
-
Reload a Service:
sudo systemctl reload application.service
This command tells a service to reload its configuration files without a full restart, if the service supports it.
-
Reload or Restart:
sudo systemctl reload-or-restart application.service
This attempts to reload the configuration. If that's not possible, it restarts the service.
Automate Startup: Systemctl Enable and Disable
Managing services in your current session is crucial, but you also need to control how services behave on system boot.
-
Enable a Service:
sudo systemctl enable application.service
This configures the service to start automatically at boot by creating a symbolic link to the appropriate autostart directory.
-
Disable a Service:
sudo systemctl disable application.service
This prevents the service from starting automatically at boot by removing the symbolic link.
Enabling a service does not start it immediately. Use start
and enable
together to start a service in the current session and ensure it starts at boot.
Monitor Your Services: Systemctl Status Checks
Keeping an eye on your services is essential for maintaining a healthy system.
-
Check Service Status:
systemctl status application.service
This provides detailed information about the service, including its state, process ID, and recent log entries.
-
Check if a Service is Active:
systemctl is-active application.service
This returns
active
orinactive
, indicating whether the service is currently running. -
Check if a Service is Enabled:
systemctl is-enabled application.service
This shows whether the service is configured to start at boot, outputting
enabled
ordisabled
. -
Check if a Service Failed:
systemctl is-failed application.service
This indicates if the service encountered an error during startup, returning
failed
if so.
System State Overview: Understanding Active Systemd Units
Gaining a broader perspective of your system's state involves listing and filtering units.
-
List Active Units:
systemctl list-units
This displays all currently active units, providing their name, load status, active state, sub-state, and description.
-
List All Units (Active and Inactive):
systemctl list-units --all
This includes all units
systemd
has loaded or attempted to load, regardless of their current state. -
Filter List by State:
systemctl list-units --all --state=inactive
Use
--state=
to filter by LOAD, ACTIVE, or SUB states. -
Filter List by Unit Type:
systemctl list-units --type=service
Focus on specific unit types like services with
--type=service
.
Discovering Available Units: Systemctl List-Unit-Files
To see every available unit file, even those not currently loaded, use list-unit-files
.
-
List All Unit Files:
systemctl list-unit-files
This shows all unit files present in the
systemd
paths, with their state (enabled
,disabled
,static
, ormasked
).
Delving Deeper: Unit Management with Systemctl
For more granular control, use these commands to inspect unit configurations and dependencies.
-
Display a Unit File:
systemctl cat atd.service
This shows the actual unit file loaded by
systemd
, useful for verifying configurations. -
Display Dependencies:
systemctl list-dependencies sshd.service
This shows the hierarchy of dependencies required to start the specified unit.
-
Check Unit Properties:
systemctl show sshd.service
This lists all low-level properties of a unit in a
key=value
format.
Mastering systemctl
puts you in command of your Linux system, ensuring smooth operation and efficient management of services. Use these commands to manage your server with ease! You'll be managing systemctl
services like a pro in no time!