Introduction
The systemctl
command in Linux is essential for managing and controlling the system’s systemd
service manager, which handles the system’s booting and initialization process. Here’s a comprehensive look at how it’s used:
Please learn about how to use Linux commands first.
1. Starting and Stopping Services
- Start a Service: To activate a service, use:
sudo systemctl start <service_name>
# e.g
sudo systemctl start nginx
- Stop a Service: This command deactivates the service, see below
Note: Always check status of a process after stopping it
sudo systemctl stop <service_name>
# e.g
sudo systemctl stop nginx
# Check status of process after stopping it
sudo systemctl status nginx
2. Enabling and Disabling Services at Boot
- Enable: Ensures a service starts at boot.
sudo systemctl enable <service_name>
# e.g
sudo systemctl enable nginx
- Disable: Prevents the service from starting on boot.
sudo systemctl disable <service_name>
# e.g
sudo systemctl disable nginx
Pay attention to disabled in yellow text below (output of status subcommand)
3. Checking Service Status
- View the current status of any service to monitor its state, running or otherwise:
sudo systemctl status <service_name>
# e.g
sudo systemctl status nginx
4. Restarting and Reloading Services
- Restart: Restarts the service entirely:
sudo systemctl restart <service_name>
# e.g
sudo systemctl restart nginx
- Reload: Reloads configuration files without stopping the service:
sudo systemctl reload <service_name>
# e.g
sudo systemctl reload nginx
5. Viewing All Active Services with systemctl command
- List all active services to get an overview of running processes:
sudo systemctl list-units --type=service --state=active
6. Checking System Boot and Reboot Information
- Use
systemctl
to view boot-up or reboot details:systemctl reboot
- For shutting down:
systemctl poweroff
7. Target Units
Targets group units help control the system’s run-level. For instance, multi-user.target
enables multi-user mode:
sudo systemctl isolate multi-user.target
8. Masking and Unmasking Services
- Mask: Prevents a service from starting, even manually.
sudo systemctl mask <service_name>
# e.g
sudo systemctl mask nginx
- Unmask: Removes the mask, allowing the service to start again.
sudo systemctl unmask <service_name>
# e.g
sudo systemctl unmask nginx
9. Setting Default Target
Configure the default run-level target (such as graphical.target
for GUI or multi-user.target
for CLI-only mode):
sudo systemctl set-default <target_name>
10. Analyzing System Boot Performance
systemd-analyze
is an auxiliary tool to assess startup times and diagnose delays:
systemd-analyze blame
Conclusion
The systemctl
command is an indispensable tool in Linux for managing services, checking statuses, setting run levels, and analyzing boot performance. With systemctl
, administrators can exercise fine-grained control over system processes and ensure services operate as expected across system sessions.