Take a look at this related U&L Q&A titled: Where does uname get its information from?. Information such as the hostname persists within a data structure within the Linux kernel, while the system is running. During a system's boot this information can be reattained through a variety of mechanisms that is typically distro specific.
If you look at the man 2 uname
man page there's a data structure mentioned there:
struct utsname {
char sysname[]; /* Operating system name (e.g., "Linux") */
char nodename[]; /* Name within "some implementation-defined
network" */
char release[]; /* Operating system release (e.g., "2.6.28") */
char version[]; /* Operating system version */
char machine[]; /* Hardware identifier */
#ifdef _GNU_SOURCE
char domainname[]; /* NIS or YP domain name */
#endif
};
The 2nd element of that structure, nodename[]
is one place where the hostname is stored within the Linux kernel.
/proc
If you take a look at /proc/sys/kernel/hostname
, the hostname is exposed here as well. This is a virtual location, /proc
, but it does give you an alternative method for accessing the hostname. The system's domainname is here too, /proc/sys/kernel/domainname
.
NOTE: Of interest, these values are UTS namespace specific.
Example
$ sudo hostname
oldhost
$ sudo unshare --uts /bin/bash
$ sudo echo newhost > /proc/sys/kernel/hostname
$ hostname
newhost
$ exit
$ hostname
oldhost
Manipulating the hostname
On system's with Systemd you can use the cli tool hostnamectl
to get/set the hostname. This will change it permanently between reboots.
$ sudo hostnamectl set-hostname --static somehostname
You can also find out it's value through sysctl
:
$ sudo sysctl -a | grep kernel.hostname
kernel.hostname = myhostname
For Fedora releases, this ask.fedoraproject.org Q&A covers the topic pretty thoroughly, titled: Correctly setting the hostname - Fedora 20 on Amazon EC2.
The closest equivalent to a human-readable (and human-chosen) name for any computer running Linux is the default hostname stored in /etc/hostname
. On some (not all) Linux distributions, this name is entered during installation as the computee’s name (but with network hostname constraints, unlike macOS’s computer name). This can be namespaced, i.e. each UTS namespace can have a different hostname.
Systems running systemd distinguish three different hostnames, including a “pretty” human-readable name which is supposed to be descriptive in a similar fashion to macOS’s computer name; this can be set and retrieved using hostnamectl
’s --pretty
option. The other two hostnames are the static hostname, which is the default hostname described above, and the transient hostname which reflects the current network configuration.
Systemd also supports a chassis type (e.g. “tablet”) and an icon for the host; see systemd-hostnamed.service
.
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:
hostname
service that runs the toolset'sset-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:/etc/hostname
and/etc/HOSTNAME
, amongst other sources (since different operating systems configure this in different ways), and makes an amalgamatedrc.conf
.rc.conf
to configure this service'shostname
environment variable.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.systemd
itself, that runs before service management is even started up. Thesystemd
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./etc/hostname
, with a fallback to the shell variable read fromrc.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:
Further reading
hostnamectl
. systemd manual pages. Freedesktop.org.set-dynamic-hostname
". User commands manual. nosh toolset. Softwares.rc.conf
amalgamation". nosh Guide. Softwares.hostname
. Toybox command list. landley.net.