To become proficient in Linux system management, it’s essential to master its commands. One command that plays a crucial role in controlling the systemd system and service manager is “systemctl”. In this guide, we’ll simplify the systemctl command, providing you with practical steps and insights to help you utilize its full potential and streamline your Linux administration tasks.
What is the systemctl Command in Linux
The systemctl command in Linux is a utility that manages and interacts with the systemd system and service manager. The systemd is a system initialization system and a service manager that has become the default on many Linux distributions. It’s used to initialize system components and manage services, replacing the older System V init scripts. Pros of the systemctl Command in Linux:
- Unified Management. Provides a centralized method for managing services and system states, simplifying tasks like starting, stopping, and restarting services.
- Parallel Initialization. Designed for faster boot times by starting services in parallel, reducing system startup duration.
- Clear Service Dependencies. Enables clear specification of dependencies, ensuring services start in the correct order and improving overall system stability.
- Consistent Logging. Paired with the journald logging system, it facilitates easy and organized retrieval of logs related to specific services.
- Dynamic Configuration and Service Resilience. Allows on-the-fly configuration changes without needing full restarts and can automatically restart failed services, enhancing system uptime and reliability.
In the upcoming section, we will explore how to use the system command in Linux.
Utilizing the systemctl Command in Linux
Here’s a step-by-step guide on using the systemctl
command in Linux, complete with descriptions and sample outputs.
1. Checking the Status of a Service. Before you work on any service, you might want to know its current status. Run the command below:
systemctl status service_name
Sample Output:
● sshd.service - OpenSSH Daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2023-10-10 08:00:00 UTC; 1h ago Main PID: 1234 (sshd) Tasks: 10 (limit: 4096) Memory: 10M CGroup: /system.slice/sshd.service ├─1234 /usr/sbin/sshd -D └─5678 sshd: user@pts/0
2. Starting a Service. If a service is not running, you can start it. Usually, no output is displayed if the command is successful. Run the command:
systemctl start service_name
3. Stopping a Service. You can stop a running service. No output is displayed once the command runs successfully.
systemctl stop service_name
4. Restarting a Service. If you made changes or just need to refresh a service, restart it.
systemctl restart service_name
5. Enabling a Service. This ensures the service starts on boot.
systemctl enable service_name
Sample Output:
Created symlink from /etc/systemd/system/multi-user.target.wants/service_name.service to /usr/lib/systemd/system/service_name.service.
6. Disabling a Service. This prevents the service from starting on boot.
systemctl disable service_name
Sample Output:
Removed symlink /etc/systemd/system/multi-user.target.wants/service_name.service.
7. Reloading Service Configuration. If a service’s config file has changed, you can reload it without restarting.
systemctl reload service_name
8. List All Active Services. View a list of all services and their statuses.
systemctl list-units --type=service
Sample Output:
UNIT LOAD ACTIVE SUB DESCRIPTION cron.service loaded active running Regular background program processing daemon sshd.service loaded active running OpenSSH Daemon ufw.service loaded active exited Uncomplicated firewall
9. Reboot System. Restart the entire system. Your system will begin the reboot process once you run the command:
systemctl reboot
10. Check Logs for a Service. View the logs related to a specific service.
journalctl -u service_name
Sample Output:
-- Logs begin at Tue 2023-10-10 07:00:00 UTC, end at Tue 2023-10-10 09:00:00 UTC. -- Oct 10 08:00:01 servername sshd[1234]: Server listening on 0.0.0.0 port 22.
Congratulations, you’ve now learned how to use the systemctl command in Linux. Please note that this guide covers the basics, but there’s much more to systemctl. As you become more familiar with it, you’ll discover it’s a versatile tool for managing your Linux system.
Conclusion
Looking to deploy Linux in the cloud? With Gcore Cloud, you can choose from Basic VM, Virtual Instances, or VPS/VDS suitable for Linux:
- Gcore Basic VM offers shared virtual machines from €3.2 per month
- Virtual Instances are virtual machines with a variety of configurations and an application marketplace
- Virtual Dedicated Servers provide outstanding speed of 200+ Mbps in 20+ global locations