How to use the command 'systemctl' (with examples)
- Linux
- November 5, 2023
Systemctl is a command-line tool that is used to control the systemd system and service manager. It enables us to manage services, check the status of units, enable or disable units, mask or unmask units, and reload systemd to scan for new or changed units.
Use case 1: Show all running services
Code:
systemctl status
Motivation: It is important to know the status of running services on a system for troubleshooting or monitoring purposes. This command provides a comprehensive list of all currently running services, along with their status.
Explanation: The status
argument is used to display the status of all running services. When executed, it will provide detailed information about each service, including its current status (running, inactive, failed), the time it was last started/activated, and any error messages or logs.
Example Output:
● service1.service - Service 1
Loaded: loaded (/etc/systemd/system/service1.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-01-31 10:12:13 UTC; 2h 30min ago
Main PID: 12345 (service1)
Tasks: 1 (limit: 1152)
Memory: 10.0M
CGroup: /system.slice/service1.service
└─12345 /usr/bin/service1
● service2.service - Service 2
Loaded: loaded (/etc/systemd/system/service2.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:service2(8)
...
Use case 2: List failed units
Code:
systemctl --failed
Motivation: Identifying failed units is crucial for system administrators to investigate and resolve issues. By listing failed units, we can easily identify the problematic units and take appropriate actions.
Explanation: The --failed
option is used to list all failed units in systemd. Units can include services, sockets, timers, and more. When this command is executed, it will display a list of failed units along with their corresponding failure reason and status.
Example Output:
UNIT LOAD ACTIVE SUB DESCRIPTION
service3.service loaded failed failed Service 3
service4.timer loaded failed failed Timer for Service 4
sys-devices-virtual-net-tun0.device loaded failed failed /sys/devices/virtual/net/tun0
unit5.socket loaded failed failed Socket for Unit 5
...
Use case 3: Start/Stop/Restart/Reload a service
Code:
systemctl start|stop|restart|reload unit
Motivation: Start/Stop/Restart/Reload a service allows us to manage service operations dynamically. This is useful when deploying a new version of an application, troubleshooting issues, or implementing changes that require a service restart.
Explanation: The start
, stop
, restart
, and reload
arguments are used to control the state of a unit or service. The unit
represents the name of the specific unit or service that needs to be started, stopped, restarted, or reloaded.
Example Output:
$ systemctl restart nginx.service
Use case 4: Show the status of a unit
Code:
systemctl status unit
Motivation: Displaying the status of a unit provides insights into its current state, including whether it is running, inactive, or has encountered any errors or failures. This is helpful for troubleshooting issues and monitoring the health of the system.
Explanation: The status
argument is used to show the status of a specific unit. By providing the name of the unit as the argument, this command will display detailed information about the unit, including its current status, the time it was last started/activated, and any error messages or logs.
Example Output:
$ systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-01-31 10:12:13 UTC; 2h 30min ago
Docs: man:nginx(8)
Process: 12345 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 12345 (nginx)
Tasks: 1 (limit: 1152)
Memory: 10.0M
CGroup: /system.slice/nginx.service
└─12345 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
Use case 5: Enable/Disable a unit to be started on bootup
Code:
systemctl enable|disable unit
Motivation: Enabling a unit to start on bootup ensures that important services or units automatically start when the system is booted. Disabling a unit prevents it from starting on bootup, which may be required for non-essential services or units.
Explanation: The enable
and disable
arguments are used to manage whether a unit should be started on bootup or not. By providing the name of the unit as the argument, this command enables or disables the unit accordingly.
Example Output:
$ systemctl enable nginx.service
Use case 6: Mask/Unmask a unit to prevent enablement and manual activation
Code:
systemctl mask|unmask unit
Motivation: Masking a unit prevents it from being enabled or manually activated, effectively blocking its usage. Unmasking a unit reverses the masking, allowing it to be enabled or manually activated again.
Explanation: The mask
and unmask
arguments are used to prevent or allow the enablement and manual activation of a unit. When a unit is masked, it cannot be enabled or manually started. Unmasking a unit reverses this effect.
Example Output:
$ systemctl mask nginx.service
Use case 7: Reload systemd, scanning for new or changed units
Code:
systemctl daemon-reload
Motivation: After modifying or adding new unit files to /etc/systemd/system
, the changes should be loaded into systemd. Reloading systemd ensures that it scans for any new or changed units, making them available for activation or control.
Explanation: The daemon-reload
argument is used to reload systemd, causing it to scan for new or changed units. This command should be executed after any modification or addition of unit files to the /etc/systemd/system
directory.
Example Output:
$ systemctl daemon-reload
Use case 8: Check if a unit is enabled
Code:
systemctl is-enabled unit
Motivation: Knowing whether a unit is enabled provides information about its automatic start on bootup. This can be helpful for verifying the configuration of critical services or units.
Explanation: The is-enabled
argument is used to check whether a unit is enabled or not. By providing the name of the unit as the argument, this command will return enabled
or disabled
based on the unit’s current configuration.
Example Output:
$ systemctl is-enabled nginx.service
enabled
Conclusion:
The systemctl
command is a versatile tool for managing services and controlling the systemd system manager. It allows users to monitor service status, start/stop/restart services, enable/disable units to start on bootup, mask/unmask units for preventing enablement, reload systemd, and check if a unit is enabled. With the provided examples and explanations, users can effectively utilize systemctl
to manage their system services and units.