15

This is quite puzzling. Does anyone know where the hostname command stores and reads hostname from?

I thought it was /etc/hostname but there is no such file on this Linux system that I'm using. I tried using strace to find where it was located but no read calls returned this information:

$ strace hostname 2>&1 | grep read
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340^\0\0\0\0\0\0"..., 832) = 832
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340\30\2\0\0\0\0\0"..., 832) = 832
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\16\0\0\0\0\0\0"..., 832) = 832
read(3, "nodev\tsysfs\nnodev\trootfs\nnodev\tr"..., 1024) = 248
read(3, "", 1024)                       = 0

Then I noticed it did uname syscall that returned this information:

uname({sys="Linux", node="server-name", ...}) = 0

A recursive grep in /etc/ returns nothing:

grep "server-name" -r /etc 

Where does uname store this information? Just in memory?

migrated from serverfault.com Oct 24 '14 at 22:19

This question came from our site for system and network administrators.

  • 1
    what linux distro you are using? – amir jj Oct 24 '14 at 23:04
8

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.

  • 1
    Meh. hostnamectl simply invokes a system call. How does the syscall store the information so that it's persistent across reboots? That's my question , if not the OPs as well. – Otheus May 20 '16 at 20:04
5

The hostname command doesn't store the name anywhere but kernel memory.

How the system decides what its name is at boot time depends on how the system is configured. Options range from reading a name from a file, to using DNS or /etc/hosts to set the name after a suitable network interface has been brought up.

  • 2
    Never /etc/hosts (which is for DNS lookups - a system that reads its hostname from there is oh so wrong) ... but possibly /etc/hostname (contains the fully qualified name) or rarely /etc/sysconfig/network (may contain a HOSTNAME=... shell command). On an amazon EC2 instance you may also have to modifiy /etc/cloud/cloud.cfg ... – David Tonhofer Oct 25 '14 at 9:01
4

hostname(1) is just a front-end to the sethostname(3) system call, which basically writes the hostname to where the kernel expects it to be stored.

If you want the change to be permanent, as per general Unix philosophy you have to store it yourself. The precise location however depends heavily on your init system. For instance, OpenBSD’s init reads the hostname from /etc/myname (during netstart).

All this should be described in your system’s manpages or supporting documentation.

2

As a long time *nix sysadmin, I'll state the obvious, without a direct RTFM reference: :)

   hostnamectl may be used to query and change the system hostname and
   related settings.

   This tool distinguishes three different hostnames: the high-level
   "pretty" hostname which might include all kinds of special characters
   (e.g. "Lennart's Laptop"), the static hostname which is used to
   initialize the kernel hostname at boot (e.g. "lennarts-laptop"), and
   the transient hostname which is a fallback value received from network
   configuration. If a static hostname is set, and is valid (something
   other than localhost), then the transient hostname is not used.

   Note that the pretty hostname has little restrictions on the characters
   used, while the static and transient hostnames are limited to the
   usually accepted characters of Internet domain names.

   The static hostname is stored in /etc/hostname, see hostname(5) for
   more information. The pretty hostname, chassis type, and icon name are
   stored in /etc/machine-info, see machine-info(5).

This holds true for most "linux" distros.

--mem

1

At runtime it is stored in memory as answered by the others here already.

To survive a reboot it has to be stored on file somewhere, which is linux distribution specific. On my Fedora 20 it's /etc/hostname

It's most likely stored in /etc. Try to search for the actual hostname in etc

grep -r `hostname` /etc

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.