LFCS System Service Management (systemd) Questions and Answers — Questions and Answers
Question 1: A web server unit named `apache2.service` is failing to start. A system administrator needs to view only the logs generated by this specific service from the systemd journal, starting from the most recent entries. Which command should they use?
- `cat /var/log/apache2/error.log`
- `systemctl status apache2.service`
- `journalctl -u apache2.service` (Correct answer)
- `journalctl /usr/sbin/apache2`
Correct answer: `journalctl -u apache2.service`
The `journalctl -u` (or `--unit`) command is the standard way to filter the systemd journal to show logs for a specific unit. While `systemctl status` shows the last few log entries, `journalctl` provides the full history and more powerful filtering options.
Question 2: A system administrator has just installed a new database package. They need to ensure its service, `mariadb.service`, automatically starts every time the system boots. Which command will configure this behavior?
- `systemctl start mariadb.service`
- `systemctl enable mariadb.service` (Correct answer)
- `systemctl add-dependency multi-user.target mariadb.service`
- `systemctl set-default graphical.target`
Correct answer: `systemctl enable mariadb.service`
The `systemctl enable <service>` command creates the necessary symbolic links in systemd's target directories to ensure the service is started automatically at boot. The `start` command only starts the service for the current session and does not persist through a reboot.
Question 3: To diagnose a slow boot process, an administrator wants to see a list of all services ordered by the time each one took to initialize. Which of the following commands provides this specific output?
- `systemd-analyze`
- `journalctl -b`
- `systemd-analyze critical-chain`
- `systemd-analyze blame` (Correct answer)
Correct answer: `systemd-analyze blame`
The `systemd-analyze blame` command prints a list of all running units, sorted by the time they took to initialize during startup. This is the primary tool for identifying which specific services are taking the longest to start.
Question 4: An administrator needs to override a single directive in the `[Service]` section of the `httpd.service` unit file without altering the original vendor-provided file. Which command is the recommended method for creating a "drop-in" configuration snippet to accomplish this?
- `systemctl cat httpd.service > /etc/systemd/system/httpd.service`
- `vi /usr/lib/systemd/system/httpd.service`
- `systemctl edit httpd.service` (Correct answer)
- `systemctl daemon-reload`
Correct answer: `systemctl edit httpd.service`
The `systemctl edit <service>` command is the best-practice method for creating an override file (or "drop-in" snippet). This command opens an editor and saves the changes in a file under `/etc/systemd/system/httpd.service.d/`, which overrides settings from the original unit file without modifying it. This prevents customizations from being lost during package upgrades.
Question 5: A system administrator must switch the running system from its current graphical target to a non-graphical, multi-user mode for maintenance, without rebooting. Which command will achieve this state change for the current session?
- `systemctl set-default multi-user.target`
- `systemctl isolate multi-user.target` (Correct answer)
- `telinit 3`
- `systemctl stop graphical.target`
Correct answer: `systemctl isolate multi-user.target`
The `systemctl isolate <target>` command switches to a new target, starting all of its dependencies and stopping all units that are not dependencies of the new target. This effectively changes the system's state without a reboot. `set-default` only changes the target for future boots.
Question 6: When creating a custom systemd service file, which section contains the `WantedBy` directive, used by `systemctl enable` to link the service to a specific target like `multi-user.target`?
- `[Unit]`
- `[Service]`
- `[Install]` (Correct answer)
- `[Socket]`
Correct answer: `[Install]`
The `[Install]` section of a unit file contains directives that are used when the unit is enabled or disabled. The `WantedBy` directive tells `systemctl enable` to create a symbolic link for this service within the specified target's `.wants` directory, causing it to start when that target is activated.
A web server unit named `apache2.service` is failing to start.
A system administrator needs to view only the logs generated by this specific service from the systemd journal, starting from the most recent entries.
Which command should they use?