A system administrator wants to define a custom `systemd` target named `my-custom.target` that depends on `network-online.target` and `multi-user.target`. This custom target should be reached only after both of its dependencies are active. Which `systemd` unit file configuration snippet correctly defines these dependencies?
- A[Unit] Description=My Custom Target Conflicts=network-online.target multi-user.target Requires=network-online.target multi-user.target
- B[Unit] Description=My Custom Target BindsTo=network-online.target multi-user.target Before=network-online.target multi-user.target
- C[Unit] Description=My Custom Target Wants=network-online.target multi-user.target After=network-online.target multi-user.target
- D[Unit] Description=My Custom Target Requires=network-online.target multi-user.target After=network-online.target multi-user.target
Show answer & explanationAnswer & explanation
Correct answer: D. [Unit] Description=My Custom Target Requires=network-online.target multi-user.target After=network-online.target multi-user.target
To ensure a target is reached *only after* its dependencies are active, both `Requires=` and `After=` directives are typically used. `Requires=` means the target cannot be started unless the listed units are started, and `After=` ensures correct ordering, meaning `my-custom.target` will start only after `network-online.target` and `multi-user.target` have started.
Why the other options are wrong
- A. `Conflicts=` means the target cannot run at the same time as the listed units. This is not the intent; the target needs to run *after* them.
- B. `BindsTo=` creates an even stronger dependency where the target is stopped if the bound unit stops. `Before=` would mean `my-custom.target` starts *before* its dependencies, which is the opposite of the requirement.
- C. `Wants=` is a weaker dependency; `my-custom.target` would still try to start even if `network-online.target` or `multi-user.target` failed. `Requires=` is needed for strict dependency.
systemd Target Dependencies
`systemd` targets use `Requires=` and `After=` directives to define strong dependencies and ordering, ensuring a target is activated only after its prerequisites are met and active.
- `Requires=`: strong dependency, unit fails if required unit fails.
- `After=`: ordering, unit starts after specified units have started.
- `Wants=`: weak dependency, unit still tries to start if wanted unit fails.
Memory trick: REQUIRE and AFTER for strict dependency order.