From 4c49e864b0b8d3685c8e53d415289806278955ea Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 5 Mar 2026 10:39:28 +0100 Subject: [PATCH] firstboot: permit setting the static hostname via a system credential For the IMDS case there's value in being able to set the static hostname, instead of just the transient one. Let's introduce firstboot.hostname, which only applies to first boot, and write the static hostname. This is different from system.hostname which applies to any boot, and writes the transient hostname. --- man/systemd-firstboot.xml | 11 +++++++++++ man/systemd.system-credentials.xml | 23 +++++++++++++++++++---- src/firstboot/firstboot.c | 13 +++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/man/systemd-firstboot.xml b/man/systemd-firstboot.xml index 86a85f0bf28..db6f2569a8d 100644 --- a/man/systemd-firstboot.xml +++ b/man/systemd-firstboot.xml @@ -447,6 +447,17 @@ + + + firstboot.hostname + + This credential specifies the static system hostname to set during first boot. The + user will not be prompted for the hostname. Note that this controls the static hostname, not the + transient hostname, and only has an effect on first boot, unlike + system.hostname. + + + Note that by default the systemd-firstboot.service unit file is set up to diff --git a/man/systemd.system-credentials.xml b/man/systemd.system-credentials.xml index e3e28872077..a302be236d4 100644 --- a/man/systemd.system-credentials.xml +++ b/man/systemd.system-credentials.xml @@ -44,7 +44,7 @@ firstboot.keymap - The console key mapping to set (e.g. de). Read by + The console key mapping to set (e.g. de). Read by systemd-firstboot1, and only honoured if no console keymap has been configured before. @@ -52,6 +52,20 @@ + + firstboot.hostname + + This credential specifies the static system hostname to set during first boot. The + user will not be prompted for the hostname. Note that this controls the static hostname, not the transient + hostname, and only has an effect on first boot, unlike system.hostname (see + below). Read by + systemd-firstboot1 + and only honoured if no static hostname has been configured before. + + + + + firstboot.locale firstboot.locale-messages @@ -398,9 +412,10 @@ system.hostname Accepts a (transient) hostname to configure during early boot. The static hostname specified - in /etc/hostname, if configured, takes precedence over this setting. - Interpreted by the service manager (PID 1). For details see - systemd1. + in /etc/hostname, if configured, takes precedence over this setting. + Interpreted by the service manager (PID 1). For details see + systemd1. Also + see firstboot.hostname above. diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index ae1899593cd..8cb81e7f06e 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -655,6 +655,19 @@ static int prompt_hostname(int rfd, sd_varlink **mute_console_link) { if (arg_hostname) return 0; + _cleanup_free_ char *hn = NULL; + r = read_credential("firstboot.hostname", (void**) &hn, NULL); + if (r < 0) + log_debug_errno(r, "Failed to read credential firstboot.hostname, ignoring: %m"); + else if (!hostname_is_valid(hn, VALID_HOSTNAME_TRAILING_DOT|VALID_HOSTNAME_QUESTION_MARK)) + log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Hostname '%s' supplied via credential is not valid, ignoring.", hn); + else { + log_debug("Acquired hostname from credentials."); + arg_hostname = TAKE_PTR(hn); + hostname_cleanup(arg_hostname); + return 0; + } + if (!arg_prompt_hostname) { log_debug("Prompting for hostname was not requested."); return 0; -- 2.47.3