I know systemctl enable iptables
is similar to the command chkconfig --level 5 iptables on
, but both are not exactly same.
Using systemctl
, how do we restrict a service to be started only on a given target like graphical.target.
chkconfiglinuxsystemd
I know systemctl enable iptables
is similar to the command chkconfig --level 5 iptables on
, but both are not exactly same.
Using systemctl
, how do we restrict a service to be started only on a given target like graphical.target.
Best Answer
That's what the
WantedBy=
andRequiredBy=
directives insystemd
unit files are for:From
man systemd.unit
:So the symbolic link created is what causes
systemd
to start the given unit when the target/unit is started.So for example:
Would cause the unit to only be started when
graphical.target
is run (if the unit is enabled).Another example:
Would cause the unit to be started when
my-custom-target.target
orgraphical.target
is run (if the unit is enabled).One final thing to keep in mind, it can be difficult to restrict things to a single target because some targets depend on others. For example,
graphical.target
Requires=multi-user.target
, so whengraphical.target
is started all units frommulti-user.target
are also started. Just keep in mind that some targets are built up on top of others, and that the ones built on top will get everything from the targets they depend on.