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!

  • 1
    On a GNU/Linux system, man 5 hostname states that "The /etc/hostname file configures the name of the local system that is set during boot using the sethostname(2) system call". I couldn't say whether this applies in your context, but does hostname work if you write the host name to /etc/hostname and reboot? – fra-san Oct 6 at 20:51
  • @fra-san thanks for the comment! If I cat /etc/hostname I get semc, my hostname. But no matter how many times I reboot, still hostname itself is (none). I'm using Busybox init, btw. – Bobbay Oct 6 at 21:05
  • On some systems the hostname is read from the first entry in /etc/hosts (yes, even loopback) – roaima Oct 6 at 22:53
  • @roaima The first line in /etc/hosts is semc - nothing. But, I wrote this after the initial install, not inside of the .tar.gz that I then untar to a partition. Just an FYI. – Bobbay Oct 7 at 0:12

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

| improve this answer | |
  • Awesome explanation! I have little experience writing services in runit/busybox init, but this seems like the way to go! Thank you again! – Bobbay Oct 7 at 13:31

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.

| improve this answer | |

From init/Kconfig in the kernel sources:

  string "Default hostname"
  default "(none)"
    This option determines the default system hostname before userspace
    calls sethostname(2). The kernel traditionally uses "(none)" here,
    but you may wish to use a different default here to make a minimal
    system more usable with less configuration.

In make menuconfig it's the near the top of "General setup".

sethostname(2) is the system call which is called by the hostname command when setting a hostname.

The hostname is not pulled by magic from /etc/hostname (or some other configuration file): init or another program should call either the hostname command or that system call directly with the content of the file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.