The systemctl command is a powerful utility used to interact with systemd, the init system and service manager on many Linux distributions. It allows you to manage services, system states, and various system resources.
systemctl provides control over systemd services, which includes starting, stopping, restarting, enabling, disabling, and checking the status of services. Additionally, it can be used to manage system states (e.g., reboot, shutdown) and inspect logs and configurations.
systemctl [OPTIONS] COMMAND [NAME...]Where:
- COMMAND is the action you want to perform (e.g., start, stop, status).
- NAME is the name of the service, target, or unit you want to manage (e.g.,
nginx.service,docker, ormulti-user.target).
-
Starting a Service:
- Starts a systemd service.
systemctl start <service-name>
- Example: To start the
apache2service:systemctl start apache2
-
Stopping a Service:
- Stops a running systemd service.
systemctl stop <service-name>
- Example: To stop the
apache2service:systemctl stop apache2
-
Restarting a Service:
- Restarts a service by stopping and then starting it again.
systemctl restart <service-name>
- Example: To restart the
apache2service:systemctl restart apache2
-
Reloading a Service:
- Reloads a service’s configuration without stopping it. Some services support reloading (like web servers).
systemctl reload <service-name>
- Example: To reload the
apache2service:systemctl reload apache2
-
Checking the Status of a Service:
- Displays the current status of a service, including whether it's running, its PID, and any recent log entries.
systemctl status <service-name>
- Example: To check the status of
apache2:systemctl status apache2
-
Enabling a Service to Start at Boot:
- Enables a service to start automatically when the system boots.
systemctl enable <service-name>
- Example: To enable
apache2to start at boot:systemctl enable apache2
-
Disabling a Service from Starting at Boot:
- Disables a service from automatically starting when the system boots.
systemctl disable <service-name>
- Example: To disable
apache2from starting at boot:systemctl disable apache2
-
Masking a Service:
- Prevents a service from being started, either manually or automatically, by creating a symbolic link to
/dev/null.
systemctl mask <service-name>
- Example: To mask the
apache2service:systemctl mask apache2
- Prevents a service from being started, either manually or automatically, by creating a symbolic link to
-
Unmasking a Service:
- Reverses the effect of
maskby removing the symbolic link.
systemctl unmask <service-name>
- Example: To unmask
apache2:systemctl unmask apache2
- Reverses the effect of
-
Viewing Logs for a Service:
- Displays the logs for a specific service managed by systemd.
journalctl -u <service-name>
- Example: To view the logs for
apache2:journalctl -u apache2
-
Viewing the System Logs:
- Displays the entire system journal, including messages from all services and system events.
journalctl
-
Rebooting the System:
- Reboots the entire system.
systemctl reboot
-
Shutting Down the System:
- Shuts down the system gracefully.
systemctl poweroff
-
Suspending the System:
- Suspends the system (puts it to sleep).
systemctl suspend -
Viewing All Active Units:
- Lists all active systemd units (services, sockets, etc.).
systemctl list-units
-
Viewing All Loaded Units:
- Lists all loaded systemd units, including inactive ones.
systemctl list-units --all
-
Checking Systemd Targets:
- Lists the systemd targets. Targets represent different states or "runlevels" in systemd. For example,
multi-user.targetis similar to runlevel 3 in traditional SysVinit systems.
systemctl list-units --type=target
- Lists the systemd targets. Targets represent different states or "runlevels" in systemd. For example,
-
Switching Runlevels (Changing Targets):
- You can switch between different systemd targets (which are similar to runlevels).
systemctl isolate <target-name>
- Example: To switch to the multi-user target (which is equivalent to runlevel 3):
systemctl isolate multi-user.target
-
Checking System Boot Logs:
- You can view the logs related to the system’s boot process.
journalctl -b
systemctl start apache2systemctl status apache2systemctl enable apache2systemctl disable apache2systemctl restart apache2systemctl list-units --type=servicesystemctl rebootjournalctl -u nginxsystemctl poweroffThe systemctl command is an essential tool for managing a system running systemd. It provides control over services, targets (runlevels), and logs, enabling administrators to configure, troubleshoot, and maintain their Linux systems effectively. From simple service management to complex system configurations, systemctl is the go-to utility for modern Linux administration.