]> git.ipfire.org Git - thirdparty/systemd.git/blob - docs/NETWORK_ONLINE.md
Merge pull request #26410 from DaanDeMeyer/xattr-symlink
[thirdparty/systemd.git] / docs / NETWORK_ONLINE.md
1 ---
2 title: Running Services After the Network Is Up
3 category: Networking
4 layout: default
5 SPDX-License-Identifier: LGPL-2.1-or-later
6 ---
7
8 # Network Configuration Synchronization Points
9
10 systemd provides three target units related to network configuration:
11
12 ## Network pre-configuration: `network-pre.target`
13
14 `network-pre.target` is used to order services before any network interfaces
15 start to be configured. Its primary purpose is for usage with firewall services
16 that want to establish a firewall *before* any network interface is up.
17
18 `network-pre.target` is a passive unit: it cannot be started directly and it is
19 not pulled in by the network management service, but instead a service that
20 wants to run before it must pull it in. Network management services hence
21 should set `After=network-pre.target`, but not `Wants=network-pre.target` or
22 `Requires=network-pre.target`. Services that want to be run before the network
23 is configured should use `Before=network-pre.target` and
24 `Wants=network-pre.target`. This way, unless there's actually a service that
25 needs to be ordered before the network is up, this target is not pulled in,
26 avoiding an unnecessary synchronization point.
27
28 ## Network management services: `network.target`
29
30 `network.target` indicates that the network management stack has been started.
31 Ordering after it has little meaning during start-up: whether any network
32 interfaces are already configured when it is reached is not defined.
33
34 Its primary purpose is for ordering things properly at shutdown: since the
35 shutdown ordering of units in systemd is the reverse of the startup ordering,
36 any unit that has `After=network.target` can be sure that it is *stopped*
37 before the network is shut down when the system is going down. This allows
38 services to cleanly terminate connections before going down, instead of losing
39 ongoing connections leaving the other side in an undefined state.
40
41 Note that `network.target` is a passive unit: you cannot start it directly and
42 it is not pulled in by any services that want to make use of the network.
43 Instead, it is pulled in by the network management services
44 themselves. Services using the network should hence simply place an
45 `After=network.target` stanza in their unit files, without
46 `Wants=network.target` or `Requires=network.target`.
47
48 ## Network connectivity has been estabilished: `network-online.target`
49
50 `network-online.target` is a target that actively waits until the network is
51 "up", where the definition of "up" is defined by the network management
52 software. Usually it indicates a configured, routable IP address of some
53 kind. Its primary purpose is to actively delay activation of services until the
54 network has been set up.
55
56 It is an active target, meaning that it may be pulled in by the services
57 requiring the network to be up, but is not pulled in by the network management
58 service itself. By default all remote mounts defined in `/etc/fstab` make use
59 of this service, in order to make sure the network is up before attempts to
60 connect to a network share are made. Note that normally, if no service requires
61 it and if no remote mount point is configured, this target is not pulled into
62 the boot, thus avoiding any delays during boot should the network not be
63 available. It is strongly recommended not to make use of this target too
64 liberally: for example network server software should generally not pull this
65 in (since server software generally is happy to accept local connections even
66 before any routable network interface is up). Its primary purpose is network
67 client software that cannot operate without network.
68
69 For more details about those targets, see the
70 [systemd.special(7)](https://www.freedesktop.org/software/systemd/man/systemd.special.html)
71 man page.
72
73 ## Compatibility with SysV init
74
75 LSB defines a `$network` dependency for legacy init scripts. Whenever systemd
76 encounters a `$network` dependency in LSB headers of init scripts it will
77 translate this to `Wants=` and `After=` dependencies on
78 `network-online.target`, staying relatively close to traditional LSB behaviour.
79
80 # Discussion
81
82 The meaning of `$network` is defined [only very
83 unprecisely](http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/facilname.html)
84 and people tend to have different ideas what it is supposed to mean. Here are a
85 couple of ideas people came up with so far:
86
87 * The network management software is up.
88 * All "configured" network interfaces are up and an IP address has been assigned to each.
89 * All discovered local hardware interfaces that have a link beat have an IP address assigned, independently whether there is actually any explicit local configuration for them.
90 * The network has been set up precisely to the level that a DNS server is reachable.
91 * Same, but some specific site-specific server is reachable.
92 * Same, but "the Internet" is reachable.
93 * All "configured" ethernet devices are up, but all "configured" PPP links which are supposed to also start at boot don't have to be yet.
94 * A certain "profile" is enabled and some condition of the above holds. If another "profile" is enabled a different condition would have to be checked.
95 * Based on the location of the system a different set of configuration should be up or checked for.
96 * At least one global IPv4 address is configured.
97 * At least one global IPv6 address is configured.
98 * At least one global IPv4 or IPv6 address is configured.
99 * And so on and so on.
100
101 All these are valid approaches to the question "When is the network up?", but
102 none of them would be useful to be good as generic default.
103
104 Modern networking tends to be highly dynamic: machines are moved between
105 networks, network configuration changes, hardware is added and removed, virtual
106 networks are set up, reconfigured, and shut down again. Network connectivity is
107 not unconditionally and continuously available, and a machine is connected to
108 different networks at different times. This is particularly true for mobile
109 hardware such as handsets, tablets, and laptops, but also for embedded and
110 servers. Software that is written under the assumption that network
111 connectivity is available continuously and never changes is hence not
112 up-to-date with reality. Well-written software should be able to handle dynamic
113 configuration changes. It should react to changing network configuration and
114 make the best of it. If it cannot reach a server it must retry. If network
115 configuration connectivity is lost it must not fail catastrophically. Reacting
116 to local network configuration changes in daemon code is not particularly
117 hard. In fact many well-known network-facing services running on Linux have
118 been doing this for decades. A service written like this is robust, can be
119 started at any time, and will always do the best of the circumstances it is
120 running in.
121
122 `$network` / `network-online.target` is a mechanism that is required only to
123 deal with software that assumes continuous network is available (i.e. of the
124 simple not-well-written kind). Which facet of it it requires is undefined. An
125 IMAP server might just require a certain IP to be assigned so that it can
126 listen on it. OTOH a network file system client might need DNS up, and the
127 service to contact up, as well. What precisely is required is not obvious and
128 can be different things depending on local configuration.
129
130 A robust system boots up independently of external services. More specifically,
131 if a network DHCP server does not react, this should not slow down boot on most
132 setups, but only for those where network connectivity is strictly needed (for
133 example, because the host actually boots from the network).
134
135 # FAQ
136
137 ## How do I make sure that my service starts after the network is *really* online?
138
139 That depends on your setup and the services you plan to run after it (see
140 above). If you need to delay you service after network connectivity has been
141 established, include
142
143 ```ini
144 After=network-online.target
145 Wants=network-online.target
146 ```
147
148 in the `.service` file.
149
150 This will delay boot until the network management software says the network is "up".
151 For details, see the next question.
152
153 ## What does "up" actually mean?
154
155 The services that are ordered before `network-online.target` define it's
156 meaning. *Usually* means that all configured network devices are up and have an
157 IP address assigned, but details may vary. In particular, configuration may
158 affect which interfaces are taken into account.
159
160 `network-online.target` will time out after 90s. Enabling this might
161 considerably delay your boot even if the timeout is not reached.
162
163 The right "wait" service must be enabled:
164 `NetworkManager-wait-online.service` if `NetworkManager` is used to configure
165 the network, `systemd-networkd-wait-online.service` if `systemd-networkd` is
166 used, etc. `systemd-networkd.service` has
167 `Also=systemd-networkd-wait-online.service` in its `[Install]` section, so when
168 `systemd-networkd.service` is enabled, `systemd-networkd-wait-online.service`
169 will be enabled too, which means that `network-online.target` will include
170 `systemd-networkd-wait-online.service` when and only when
171 `systemd-networkd.service` is enabled. `NetworkManager-wait-online.service` is
172 set up similarly. This means that the "wait" services do not need to be enabled
173 explicitly. They will be enabled automatically when the "main" service is
174 enabled, though they will not be *used* unless something else pulls in
175 `network-online.target`.
176
177 To verify that the right service is enabled (usually only one should be):
178 ```console
179 $ systemctl is-enabled NetworkManager-wait-online.service systemd-networkd-wait-online.service
180 disabled
181 enabled
182 ```
183
184 ## Should `network-online.target` be used?
185
186 Please note that `network-online.target` means that the network connectivity
187 *has been* reached, not that it is currently available. By the very nature and
188 design of the network, connectivity may briefly or permanently disappear, so
189 for reasonable user experience, services need to handle temporary lack of
190 connectivity.
191
192 If you are a developer, instead of wondering what to do about `network.target`,
193 please just fix your program to be friendly to dynamically changing network
194 configuration. That way you will make your users happy because things just
195 start to work, and you will get fewer bug reports. You also make the boot
196 faster by not delaying services until network connectivity has been
197 established. This is particularly important for folks with slow address
198 assignment replies from a DHCP server.
199
200 Here are a couple of possible approaches:
201
202 1. Watch rtnetlink and react to network configuration changes as they
203 happen. This is usually the nicest solution, but not always the easiest.
204 2. If you write a server: listen on `[::]`, `[::1]`, `0.0.0.0`, and `127.0.0.1`
205 only. These pseudo-addresses are unconditionally available. If you always
206 bind to these addresses you will have code that doesn't have to react to
207 network changes, as all you listen on is catch-all and private addresses.
208 3. If you write a server: if you want to listen on other, explicitly configured
209 addresses, consider using the `IP_FREEBIND` sockopt functionality of the
210 Linux kernel. This allows your code to bind to an address even if it is not
211 actually (yet or ever) configured locally. This also makes your code robust
212 towards network configuration changes. This is provided as `FreeBind=`
213 for systemd services, see
214 [systemd.socket(5)](https://www.freedesktop.org/software/systemd/man/systemd.socket.html).
215
216 An exception to the above recommendations is services which require network
217 connectivity, but do not delay system startup. An example may be a service
218 which downloads package updates into a cache (to be used at some point in the
219 future by the package management software). Such a service may even start
220 during boot, and pull in and be ordered after `network-online.target`, but as
221 long as it is not ordered before any unit that is part of the default target,
222 it does not delay boot. It is usually easier to write such a service in a
223 "simplistic" way, where it doesn't try to wait for the network connectivity to
224 be (re-)established, but is instead started when the network has connectivity,
225 and if the network goes away, it fails and relies on the system manager to
226 restart it if appropriate.
227
228 ## Modyfing the meaning of `network-online.target`
229
230 As described above, the meaning of this target is defined first by which
231 implementing services are enabled (`NetworkManager-wait-online.service`,
232 `systemd-networkd-wait-online.service`, …), and second by the configuration
233 specific to those services.
234
235 For example, `systemd-networkd-wait-online.service` will wait until all
236 interfaces that are present and managed by
237 [systemd-networkd.service(8)](https://www.freedesktop.org/software/systemd/man/systemd-networkd.service.html).
238 are fully configured or failed and at least one link is online; see
239 [systemd-networkd-wait-online.service(8)](https://www.freedesktop.org/software/systemd/man/systemd-networkd-wait-online.service.html)
240 for details. Those conditions are affected by the presence of configuration
241 that matches various links, but also by settings like
242 `Unmanaged=`, `RequiredForOnline=`, `RequiredFamilyForOnline=`; see
243 [systemd.network(5)](https://www.freedesktop.org/software/systemd/man/systemd.network.html).
244
245 It is also possible to plug in additional checks for network state. For
246 example, to delay `network-online.target` until some a specific host is
247 reachable (the name can be resolved over DNS and the appropriate route has been
248 established), the following simple service could be used:
249
250 ```ini
251 [Unit]
252 DefaultDependencies=no
253 After=nss-lookup.target
254 Before=network-online.target
255 Type=oneshot
256 RemainAfterExit=yes
257
258 [Service]
259 ExecStart=sh -c 'while ! ping -c 1 example.com; do sleep 1; done'
260
261 [Install]
262 WantedBy=network-online.target
263 ```