Ubuntu – What determines the Linux hostname

fedorahostnameUbuntu

In the few years I've been using Linux as my main system, specifically Fedora, I've always seen my hostname set to just "localhost", with the exception of when I connect to some networks and it becomes my IP. Today I experienced the following behavior which I'm having trouble understanding though.

I set up an Ubuntu installation on another partition of my laptop, setting a computer name / hostname during the Ubuntu install. When I rebooted back into Fedora though, Fedora had updated my hostname to the name I set in the Ubuntu install.

I always thought the hostname was configured and stored on the partition of the distro installation, and indeed the contents of /etc/hostname on Fedora still read "localhost.localdomain", but running the hostname command shows the new hostname. Both installs share an efi boot partition, but are otherwise discrete. I'm wondering from where and why the Fedora install is reading the new hostname?

Best Answer

The hostnameprogram performs a uname syscall, as can be seen from running:

strace hostname
...
uname({sysname="Linux", nodename="my.hostname.com", ...}) = 0
...

From the uname syscall man page, it says the syscall retrieves the following struct from the kernel:

  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
           };

So the domain name comes from the NIS / YP system, if we believe the comment. So more than likely, there may be a NIS / YP service on your network that is trotting back the name to you that is set by the ubuntu OS.

Related Question