2024-11-07
Before diving into systemctl
commands, it’s important to grasp the concept of systemd services. These are essentially processes or daemons that run in the background, providing essential system functionality. Examples include network management (networkd
), logging (journalctl
), and the display manager (gdm3
). Each service is defined by a unit file, typically located in /etc/systemd/system/
. These files specify how the service should be started, stopped, and managed.
systemctl
offers a rich set of commands to interact with systemd services. Here are some of the most frequently used:
To start a service, use the start
command:
sudo systemctl start sshd.service
This command starts the SSH daemon. The .service
suffix is essential. To stop a running service:
sudo systemctl stop sshd.service
Restarting a service terminates and restarts it:
sudo systemctl restart sshd.service
Reloading a service, on the other hand, reloads its configuration without restarting the process itself (useful for configuration changes):
sudo systemctl reload sshd.service
To check the status of a service, use the status
command:
sudo systemctl status sshd.service
This provides detailed information about the service’s current state, including whether it’s running, active, failed, or inactive. The output usually includes logs and other relevant details.
Enabling a service ensures it starts automatically during system boot:
sudo systemctl enable sshd.service
Disabling a service prevents it from starting at boot:
sudo systemctl disable sshd.service
You can check the enabled status of a service with:
sudo systemctl is-enabled sshd.service
This will output enabled
or disabled
.
To list all active services:
systemctl list-units --type=service
You can filter this list. For example to list only running services:
systemctl list-units --type=service --state=running
To list all available service units:
systemctl list-unit-files --type=service
Sometimes, you want to prevent a service from ever being started, even manually. This is done by masking:
sudo systemctl mask sshd.service
Unmasking is done similarly:
sudo systemctl unmask sshd.service
While we focused on .service
units, systemctl
manages other unit types, including:
.socket
: Network sockets.target
: Groups of units (e.g., graphical.target
for the graphical desktop).mount
: Mount points.automount
: Automounted file systems.swap
: Swap devices.device
: Hardware devicesThe commands remain the same, simply replace .service
with the appropriate suffix. For instance:
sudo systemctl start getty.target
--user
for User Servicessystemctl
also manages user services. These are services that run under a specific user’s context. Use the --user
option for these:
systemctl --user start my-user-service.service
Remember to create your user service files appropriately.
These examples provide a solid foundation for working with systemctl
. Mastering these commands is essential for any Linux administrator. Remember to always use sudo
when necessary to execute commands with root privileges. Further exploration of the man systemctl
page is highly recommended for advanced usage.