<citerefentry><refentrytitle>systemd.network</refentrytitle><manvolnum>5</manvolnum></citerefentry> for details
about systemd's own configuration files for DNS servers. To improve compatibility,
<filename>/etc/resolv.conf</filename> is read in order to discover configured system DNS servers, but only if it is
- not a symlink to <filename>/run/systemd/resolve/resolv.conf</filename> (see below).</para>
+ not a symlink to <filename>/run/systemd/resolve/stub-resolv.conf</filename> or
+ <filename>/run/systemd/resolve/resolv.conf</filename> (see below).</para>
<para><command>systemd-resolved</command> synthesizes DNS resource records (RRs) for the following cases:</para>
<refsect1>
<title><filename>/etc/resolv.conf</filename></title>
- <para>Three modes of handling <filename>/etc/resolv.conf</filename> (see
+ <para>Four modes of handling <filename>/etc/resolv.conf</filename> (see
<citerefentry project='man-pages'><refentrytitle>resolv.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>) are
supported:</para>
<itemizedlist>
+ <listitem><para><command>systemd-resolved</command> maintains the
+ <filename>/run/systemd/resolve/stub-resolv.conf</filename> file for compatibility with traditional Linux
+ programs. This file may be symlinked from <filename>/etc/resolv.conf</filename>. This file lists the 127.0.0.53
+ DNS stub (see above) as the only DNS server. It also contains a list of search domains that are in use by
+ systemd-resolved. The list of search domains is always kept up-to-date. Note that
+ <filename>/run/systemd/resolve/stub-resolv.conf</filename> should not be used directly by applications, but only
+ through a symlink from <filename>/etc/resolv.conf</filename>. This file may be symlinked from
+ <filename>/etc/resolv.conf</filename> in order to connect all local clients that bypass local DNS APIs to
+ <command>systemd-resolved</command> with correct search domains settings. This mode of operation is
+ recommended.</para></listitem>
+
<listitem><para>A static file <filename>/usr/lib/systemd/resolv.conf</filename> is provided that lists
the 127.0.0.53 DNS stub (see above) as only DNS server. This file may be symlinked from
<filename>/etc/resolv.conf</filename> in order to connect all local clients that bypass local DNS APIs to
- <command>systemd-resolved</command>. This mode of operation is recommended.</para></listitem>
+ <command>systemd-resolved</command>. This file does not contain any search domains.</para></listitem>
<listitem><para><command>systemd-resolved</command> maintains the
<filename>/run/systemd/resolve/resolv.conf</filename> file for compatibility with traditional Linux
return 0;
/* Is it symlinked to our own file? */
- if (stat(PRIVATE_RESOLV_CONF, &own) >= 0 &&
+ if (stat(PRIVATE_UPLINK_RESOLV_CONF, &own) >= 0 &&
+ st.st_dev == own.st_dev &&
+ st.st_ino == own.st_ino)
+ return 0;
+
+ /* Is it symlinked to our own stub file? */
+ if (stat(PRIVATE_STUB_RESOLV_CONF, &own) >= 0 &&
st.st_dev == own.st_dev &&
st.st_ino == own.st_ino)
return 0;
fputs_unlocked("\n", f);
}
-static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
+static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
Iterator i;
fputs_unlocked("# This file is managed by man:systemd-resolved(8). Do not edit.\n#\n"
return fflush_and_check(f);
}
+static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
+ fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n#\n"
+ "# 127.0.0.53 is the systemd-resolved stub resolver.\n"
+ "# run \"systemd-resolve --status\" to see details about the actual nameservers.\n"
+ "nameserver 127.0.0.53\n\n", f);
+
+ if (!ordered_set_isempty(domains))
+ write_resolv_conf_search(domains, f);
+
+ return fflush_and_check(f);
+}
+
int manager_write_resolv_conf(Manager *m) {
_cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
- _cleanup_free_ char *temp_path = NULL;
- _cleanup_fclose_ FILE *f = NULL;
+ _cleanup_free_ char *temp_path_uplink = NULL;
+ _cleanup_free_ char *temp_path_stub = NULL;
+ _cleanup_fclose_ FILE *f_uplink = NULL;
+ _cleanup_fclose_ FILE *f_stub = NULL;
int r;
assert(m);
if (r < 0)
return log_warning_errno(r, "Failed to compile list of search domains: %m");
- r = fopen_temporary_label(PRIVATE_RESOLV_CONF, PRIVATE_RESOLV_CONF, &f, &temp_path);
+ r = fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF, PRIVATE_UPLINK_RESOLV_CONF, &f_uplink, &temp_path_uplink);
if (r < 0)
return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m");
+ r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub);
+ if (r < 0)
+ return log_warning_errno(r, "Failed to open private stub-resolv.conf file for writing: %m");
+ (void) fchmod(fileno(f_uplink), 0644);
+ (void) fchmod(fileno(f_stub), 0644);
- (void) fchmod(fileno(f), 0644);
-
- r = write_resolv_conf_contents(f, dns, domains);
+ r = write_uplink_resolv_conf_contents(f_uplink, dns, domains);
if (r < 0) {
log_error_errno(r, "Failed to write private resolv.conf contents: %m");
goto fail;
}
- if (rename(temp_path, PRIVATE_RESOLV_CONF) < 0) {
+ if (rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF) < 0) {
r = log_error_errno(errno, "Failed to move private resolv.conf file into place: %m");
goto fail;
}
+ r = write_stub_resolv_conf_contents(f_stub, dns, domains);
+ if (r < 0) {
+ log_error_errno(r, "Failed to write private stub-resolv.conf contents: %m");
+ goto fail;
+ }
+
+ if (rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF) < 0) {
+ r = log_error_errno(errno, "Failed to move private stub-resolv.conf file into place: %m");
+ goto fail;
+ }
+
return 0;
fail:
- (void) unlink(PRIVATE_RESOLV_CONF);
- (void) unlink(temp_path);
+ (void) unlink(PRIVATE_UPLINK_RESOLV_CONF);
+ (void) unlink(temp_path_uplink);
+ (void) unlink(PRIVATE_STUB_RESOLV_CONF);
+ (void) unlink(temp_path_stub);
return r;
}