systemctl is a command-line utility in Linux operating systems that is used to control and manage the system’s services, daemons, and other processes.
It is an essential tool for system administrators and developers as it allows them to monitor and control various system processes.
Table of Contents
Viewing systemd information
In systemctl, a “unit” is a systemd object that represents a system resource that is managed by systemd. It could be a service, socket, target, device, mount point, timer, or a variety of other system resources.
Each unit is defined in a configuration file with a “.service”, “.socket”, “.target”, or other file extension, depending on the type of unit. The unit configuration file specifies information about the unit, such as its name, description, startup priority, dependencies, and the command to start or stop the unit.
Units are organized into a hierarchy of dependencies, with each unit depending on other units that must be started before it can be started. The dependencies are defined using the “Requires”, “Wants”, “After”, and “Before” directives in the unit configuration file.
The systemctl command is used to manage the units, such as starting or stopping a service, enabling or disabling a service to start at boot time, checking the status of a service, and so on. It is also used to query information about the units, such as their dependencies, runtime parameters, and logs.
The systemctl path is under /usr/lib/systemd/system by default.
- systemctl list-dependencies Displays the dependencies of a given systemd unit
- systemctl list-sockets Shows information about active sockets
- systemctl list-jobs Shows all active systemd jobs
- systemctl list-unit-files Lists all installed unit files and their states
- systemctl list-units Shows the status of all loaded and active systemd units
- systemctl list-units -all Shows the status of all not-found/ loaded/ active/ inactive systemd units
- systemctl get-default Displays the default target of the system
Working with services
systemctl is a powerful command-line tool used to manage services in Linux systems that use the systemd init system.
Services are background processes that run continuously and provide specific functionality to the system. systemctl allows you to start, stop, restart, enable, disable, reload, and check the status of these services.
For example, to start the httpd service which runs the Apache web server, you would use the command systemctl start httpd. To check the status of the service, you would use the command systemctl status httpd.
systemctl stop service: Stops a running service
systemctl start service: Starts a service
systemctl restart service: Restarts a running service
systemctl reload service: Reloads all configuration files of a service
systemctl daemon-reload: Reloads any changed unit files
systemctl status service: Shows the status of a service
systemctl –failed: Lists any services that failed to start
systemctl reset-failed: Resets any failed units
systemctl enable service: Enables a service to start automatically at boot
systemctl disable service: Disables a service from starting at boot
systemctl show service: Displays properties of a given systemd unit
systemctl edit service: Creates a snippet to drop into the unit file
systemctl edit –full service: Edits the entire unit file for a service
systemctl -H host status network: Runs any systemctl command on a remote host
Changing system states
systemctl is a command-line tool in Linux systems that can be used to change the system state.
For example, to restart the system, you would use the command systemctl reboot. To suspend the system to RAM, you would use the command systemctl suspend.
Note that some of these commands may require root or sudo privileges to execute.
systemctl reboot: Reboots the system
systemctl poweroff: Powers off the system
systemctl emergency: Puts the system in emergency mode
systemctl default: Sets the default target to multi-user target
Viewing log messages
journalctl is a command-line tool in Linux systems that is used to query and view the system journal, which contains log messages generated by the systemd system and service manager.
The journal is a binary file format that replaces the traditional text-based log files used in older Unix-like systems. Here are some common options and parameters for using journalctl:
- journalctl – Displays the entire system journal.
- journalctl -u <service> – Displays log messages for a specific service.
- journalctl –unit <service> – Same as above.
- journalctl –since <time> – Displays log messages since a specific time.
- journalctl –until <time> – Displays log messages until a specific time.
- journalctl –follow – Displays log messages in real-time as they are generated.
- journalctl –output=<format> – Sets the output format for log messages (e.g. –output=json).
- journalctl -p <priority> – Filters log messages by priority level (e.g. –priority=err for errors only).
- journalctl: Displays all collected log messages
- journalctl -u network.service: Displays log messages for a specific network service
- journalctl -f: Follows log messages as they appear
- journalctl -k: Displays only kernel messages