What's the default file for `hostname`?

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

  • Lennart Poettering et al. (2016). hostnamectl. systemd manual pages. Freedesktop.org.
  • Jonathan de Boyne Pollard (2017). "set-dynamic-hostname". User commands manual. nosh toolset. Softwares.
  • Jonathan de Boyne Pollard (2017). "rc.conf amalgamation". nosh Guide. Softwares.
  • Jonathan de Boyne Pollard (2015). "external formats". nosh Guide. Softwares.
  • Rob Landley. hostname. Toybox command list. landley.net.
  • https://unix.stackexchange.com/a/12832/5132

So you are building this system from scratch and you are asking where the hostname is configured?

The simple answer is that it isn't. The current hostname is stored inside the kernel and like most things kernel, it doesn't read any files by default.

Something in your system startup must read a config file (of your choosing) and set the kernel's hostname. This must happen every startup.


You can change it from make menuconfig when building the kernel.

Tags:

Linux

Hostname