In Linux systems, service management and system status control are daily tasks for system administrators. The systemctl command is the control tool for the Systemd system, used to manage system services and system status. Today, let’s learn about this super useful command—systemctl.1. What is the systemctl Command?The systemctl command is used to manage system services and system status. It can help you start, stop, enable, disable services, and manage the system’s run levels.Before diving deeper into the systemctl command, it is recommended to check its official documentation to understand all available options and usages. Enter the following command in the terminal:
man systemctl
2. Usage of the systemctl CommandBasic command format:
systemctl [options] [action] [service_name]
Common options: -f: force the operation. -H: enable remote control. -M: specify the container. -n: specify the number of displayed items. -t: specify the output type. -a: show all services. -l: show detailed information. -h: show help information. –failed: show failed services. –all: show all services, including static services. –state=status: show services with the specified state. Common actions: start: start the service. stop: stop the service. restart: restart the service. reload: reload the service configuration. enable: enable the service to start automatically at system boot. disable: disable the service from starting automatically at system boot. status: check the service status. list-units: list all running services. list-unit-files: list all services and their startup status. list-dependencies: list the dependencies of the service. poweroff: shut down the system. reboot: restart the system. halt: stop the system. suspend: suspend the system. hibernate: put the system into hibernation.3. Example OperationsExample 1: Start a Service
sudo systemctl start nginx # This command will start the nginx service.
Example 2: Stop a Service
sudo systemctl stop nginx # This command will stop the nginx service.
Example 3: Restart a Service
sudo systemctl restart nginx # This command will restart the nginx service.
Example 4: Reload Service Configuration
sudo systemctl reload nginx # This command will reload the configuration of the nginx service.
Example 5: Enable a Service
sudo systemctl enable nginx # This command will enable the nginx service to start automatically at system boot.
Example 6: Disable a Service
sudo systemctl disable nginx # This command will disable the nginx service from starting automatically at system boot.
Example 7: Check Service Status
sudo systemctl status nginx # This command will check the current status of the nginx service.
Example 8: List All Running Services
systemctl list-units --type=service # This command will list all running services.
Example 9: List All Services and Their Startup Status
systemctl list-unit-files --type=service # This command will list all services and their startup status.
Example 10: List Service Dependencies
systemctl list-dependencies nginx # This command will list the dependencies of the nginx service.
4. Practical TipsScenario 1: Manage Service Start and StopYou can use the following commands to manage service start and stop:
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
Scenario 2: Set Service to Start on BootYou can use the following command to set the service to start on boot:
sudo systemctl enable nginx
Scenario 3: Check Service StatusYou can use the following command to check the service status:
systemctl status nginx
Scenario 4: Manage System StatusYou can use the following commands to manage system status:
systemctl reboot
systemctl poweroff
systemctl suspend
5. NotesPermission Requirements:Most systemctl commands require administrator privileges, so you need to prepend sudo when using them.Service Name:Ensure you use the correct service name. If you are unsure of the service name, you can use systemctl list-unit-files –type=service to view all services.Remote Management:You can use the -H option for remote management of the system, for example, systemctl -H user@remote_host start service.Output Format:You can use the -l option to display detailed information or the -n option to specify the number of items displayed.6. Interactive SectionQuestion 1:How do you use the systemctl command to start a service?Question 2:When using the systemctl command, how do you check the current status of a service?If you already know the answers, or if you have other questions while using the systemctl command, feel free to leave a comment for discussion! Let’s communicate and improve together.