How to start a systemd service before networking starts

systemd

I'm trying to set up a new service (under Debian Jessie) that needs to set up some mounts where the network configuration is stored and thus this service must complete before networking.service starts.

I tried the following:

[Unit]
Description=mount/repair remaining filesystems (all persistent fs beyond "/")
#Before=network-pre.target
Before=networking.service

[Service]
Type=oneshot
ExecStart=/opt/intermodul-mounts/start.sh
TimeoutSec=0
RemainAfterExit=yes

[Install]
RequiredBy=networking.service

Using systemd-analyze plot I can see that my service starts, but networking.service starts about 3 seconds earlier:

enter image description here

Apparently my config is wrong, but I'm having a hard time finding the problem… Any help greatly appreciated..

Update

I currently solved it by changing the service config to start before local-fs.target instead of networking.service:

[Unit]
DefaultDependencies=no
Description=mount/repair remaining filesystems (all persistent fs beyond "/")
Before=local-fs.target

[Service]
Type=oneshot
ExecStart=/opt/intermodul-mounts/start.sh
TimeoutSec=0
RemainAfterExit=yes

[Install]
RequiredBy=local-fs.target

Still, I'd like to understand why my first configuration didn't work as expected…?

Best Answer

network-pre.target is a target that may be used to order services before any network interface is configured. It's primary purpose is for usage with firewall services that want to establish a firewall before any network interface is up. It's a passive unit: you cannot start it directly and it is not pulled in by the the network management service, but by the service that wants to run before it.

You want to use network-pre.target if you want to setup something before network starts

Services that want to be run before the network is configured should place Before=network-pre.target and also set Wants=network-pre.target to pull it in.

You should put these under [Unit] section:

Before=network-pre.target
Wants=network-pre.target

Reference

Related Question