Linux – Default File for Hostname Configuration

hostnamelinux

Heyo! I'm currently working on a non-lfs system from scratch with busybox as the star. Now, my login says:

(none) login:

Hence, my hostname is broken. hostname brings me (none) too.

The guide I was following told me to throw the hostname to /etc/HOSTNAME. I've also tried /etc/hostname. No matter what I do, hostname returns (none) – unless I run hostname <thename> or hostname -F /etc/hostname. Now obviously, I don't want this to be done every time somebody freshly installed the distro — so what is the real default file, if not /etc/hostname?

Thanks in advance!

Best Answer

The hostname commands in common toolsets, including BusyBox, do not fall back to files when querying the hostname. They report solely what the kernel returns to them as the hostname from a system call, which the kernel initializes to a string such as "(none)", changeable by reconfiguring and rebuilding the kernel. (In systemd terminology this is the dynamic hostname, a.k.a. transient hostname; the one that is actually reported by Linux, the kernel.) There is no "default file".

There's usually a single-shot service that runs at system startup, fairly early on, that goes looking in these various files, pulls out the hostname, and initializes the kernel hostname with it. (In systemd terminology this configuration string is the static hostname.) For example:

  • In my toolset I provide an "early" hostname service that runs the toolset's set-dynamic-hostname command after local filesystem mounts and before user login services. The work is divided into stuff that is done (only) when one makes a configuration change, and stuff that is done at (every) system bootstrap:
    • The external configuration import mechanism reads /etc/hostname and /etc/HOSTNAME, amongst other sources (since different operating systems configure this in different ways), and makes an amalgamated rc.conf.
    • The external configuration import mechanism uses the amalgamated rc.conf to configure this service's hostname environment variable.
    • When the service runs, set-dynamic-hostname doesn't need to care about all of the configuration source possibilities and simply takes the environment variable, from the environment configured for the service, and sets the dynamic hostname from it.
  • In systemd this is an initialization action that is hardwired into the code of systemd itself, that runs before service management is even started up. The systemd program itself goes and reads /etc/hostname (and also /proc/cmdline, but not /etc/HOSTNAME nor /etc/default/hostname nor /etc/sysconfig/network) and passes that to the kernel.
  • In Void Linux there is a startup shell script that reads the static hostname from (only) /etc/hostname, with a fallback to the shell variable read from rc.conf, and sets the dynamic hostname from its value.

If you are building a system "from scratch", then you'll have to make a service that does the equivalent. The BusyBox and ToyBox tools for setting the hostname from a file are hostname -F "${filename}", so you'll have to make a service that runs that command against /etc/hostname or some such file.

BusyBox comes with runit's service management toolset, and a simple runit service would be something along the lines of:

#!/bin/sh -e
exec 2>&1
exec hostname -F /etc/hostname

Further reading

Related Question