Systemd targets are an essential part of managing relationships between units in a Linux system. In the context of systemd, a target is a unit that represents a particular system state or goal. It serves as a logical grouping of other units and controls their activation and deactivation based on dependencies and requirements. Targets provide a flexible and efficient way to manage the boot process, service activation, and system shutdown.
Targets are defined using systemd unit files, which are text files that describe the properties and behavior of a unit. These files are typically stored in the `/etc/systemd/system/` directory or the `/usr/lib/systemd/system/` directory. Each target is represented by a .target file.
Targets can be classified into two main types: static targets and dynamic targets. Static targets represent well-defined system states, such as multi-user, graphical, or emergency mode. These targets are used during the boot process to transition the system from one state to another. For example, the `multi-user.target` represents the state where the system is ready to accept user logins and run services.
Dynamic targets, on the other hand, are used to manage the activation and deactivation of services during runtime. They allow grouping of related services and provide a way to start or stop them as a cohesive unit. Dynamic targets are often used to manage service dependencies and ensure that the required services are started or stopped in the correct order. For example, the `network.target` represents the state where network-related services are activated.
Targets can have dependencies on other targets or units, which are specified using the `Wants=` and `Requires=` directives in the unit files. These directives define the relationships between units and ensure that the necessary units are activated before the target can be reached. The `Wants=` directive specifies a weak dependency, meaning that the target can be reached even if the unit fails to start. On the other hand, the `Requires=` directive specifies a strong dependency, ensuring that the target cannot be reached unless the unit is successfully activated.
Systemd provides several commands to manage targets and their dependencies. The `systemctl` command is commonly used to control and query the state of systemd units. For example, to start a target, you can use the command `systemctl start target_name`. Similarly, to stop a target, you can use `systemctl stop target_name`. The `systemctl list-dependencies target_name` command displays the dependencies of a target, showing the units that must be started or stopped for the target to be reached.
Systemd targets play a important role in managing relationships between units in a Linux system. They provide a structured and organized approach to system initialization, service activation, and shutdown. By defining dependencies between units and grouping them into targets, administrators can ensure that the system reaches the desired state and that services are started or stopped in the correct order.
Other recent questions and answers regarding Examination review:
- What is the purpose of the "rescue.target" and how can it be used for troubleshooting without rebooting the system?
- What command can be used to switch between targets in systemd and how is it similar to switching between run levels in sysvinit?
- How can you ensure that necessary network configurations are completed before a specific network service starts?
- How are targets similar to named run levels in sysvinit and what is the purpose of the "multi-user.target"?

