Fedora 35 overwritting $HOSTNAME when setting hostname to localhost

I am currently running Fedora 35 in a VM. What I have installed is the “Minimal” and basic GNOME shell.

I am currently setting the machine’s hostname. It behaves normally when I set it to something random. But when I try to set the hostname to localhost using hostnamectl, I find that the setting gets overwritten by Fedora.

If I set the hostname to something out of “localhost”, everything looks normal.

[loctune@machine ~]$ hostnamectl hostname random
[loctune@random ~]$ hostnamectl status
 Static hostname: random
       Icon name: computer-vm
         Chassis: vm
      Machine ID: b34555ec4ef84a81abc435f647d006d4
         Boot ID: 06a26791697547b483d72eece1d64316
  Virtualization: microsoft
Operating System: Fedora Linux 35 (Thirty Five)   
     CPE OS Name: cpe:/o:fedoraproject:fedora:35
          Kernel: Linux 5.16.5-200.fc35.x86_64
    Architecture: x86-64
 Hardware Vendor: Microsoft Corporation
  Hardware Model: Virtual Machine

If I try to set it to localhost, the output looks like this:

[loctune@random ~]$ hostnamectl hostname localhost
[loctune@localhost ~]$ hostnamectl status
   Static hostname: localhost
Transient hostname: fedora
         Icon name: computer-vm
           Chassis: vm
        Machine ID: b34555ec4ef84a81abc435f647d006d4
           Boot ID: 06a26791697547b483d72eece1d64316
    Virtualization: microsoft
  Operating System: Fedora Linux 35 (Thirty Five)   
       CPE OS Name: cpe:/o:fedoraproject:fedora:35
            Kernel: Linux 5.16.5-200.fc35.x86_64
      Architecture: x86-64
   Hardware Vendor: Microsoft Corporation
    Hardware Model: Virtual Machine

A transient hostname “fedora” kicks in immediately. At this time, the /etc/hosts, /etc/hostname, sysctl kernel.hostname, hostname -f all show “localhost” as expected as well as inside the terminal. However, fedora is shown in the gnome-terminal title as well as gnome-settings computer/device name, as the environment variable $HOSTNAME gets overwritten (need a refresh to show in echo).

[loctune@localhost ~]$ echo $HOSTNAME
fedora

Currently, I find no way to change this transient hostname. hostnamectl gives:

[loctune@localhost ~]$ hostnamectl hostname --transient localhost
Hint: static hostname is already set, so the specified transient hostname will not be used.

and the transient hostname fedora is not altered. Nor do I find a way to change this overwriting behavior. Settings in NetworkManager.conf with hostname-mode=none does not stop it from getting this fedora as expected, as likely this name does not come from DHCP. Even when I have no Internet connection at all, altering the hostname to localhost or localhost.localdomain still gives me this fedora immediately.

Oddly, this overwriting behavior is only found in Fedora. I tested a few other distros including RHEL 8.5 and 9 Beta, and a few Deb-based inside the same VM, and none of them provides/requires a transient hostname like Fedora and overwrites the $HOSTNAME when static is set to localhost. Thus I would infer that this is reasoned from some local settings in Fedora.

Do someone have experience with this behavior, and have experience dealing with this issue?

Thanks.

Loc

EDIT:

When I updated my Arch in VM, it now behaves similar to Fedora. When you do systemctl it also gets a transient hostname “archlinux” and write that to gnome-settings computer/device name. The difference is that it does not overwrite $HOSTNAME, so echo gives out normal output.

I wonder whether this is due to any change in GNOME, NetworkManager or other parts of the system.

Welcome to ask.:fedora:edora @loctune

It looks like that localhost is already an alias of the host-name … you might have to choose an other one.

I just tested it and think you need to reboot your system to see the change on echo $HOSTNAME too.

Well, you do not really need to reboot. A logout is fairly enough to verify the change on echo $HOSTNAME. That is what I mean by “need a refresh” because this variable does not get updated immediately.

I am a bit confused.

The localhost is basically available on every machine, which points to 127.0.0.1 as in /etc/hosts. It can be adopted as a host-name without issue on almost every distro.

Choosing a host-name out of the “localhost”, “localhost.localdomain” (if you use this one, you will be given a pretty name localhost.localdomain and your static name is set to localhost-localdomain) or maybe “localhost6.localdomain” for sure can have deal with this. But overwriting the $HOSTNAME is not a common behavior as among other distros and may cause problems when the environment variable gets silently overwritten to other stuff without notification.

Here is an output on a Rocky 8.5 under same VM (RHEL 8.5 outputs exactly the same).

[loctune@localhost ~]$ hostnamectl status
 Static hostname: localhost
       Icon name: computer-vm
         Chassis: vm
      Machine ID: ---
         Boot ID: ---
  Virtualization: microsoft
Operating System: Rocky Linux 8.5 (Green Obisidian)   
     CPE OS Name: cpe:/o:rocky:rocky:8.5.GA
          Kernel: Linux 4.18.0-348.el8.0.2.x86_64
    Architecture: x86-64
[loctune@localhost ~]$ echo $HOSTNAME
localhost

You can notice that it does not have a transient hostname setting like Fedora, and the $HOSTNAME is not overwritten. I do wonder about the reason and methods Fedora use to alter the system behavior, as I would like to tune my Fedora to behave the same.

In my personal opinion, I do not think this overwriting behavior as a valid and necessary approach (if it is not adopted even by RHEL and other distros, I cannot personally find a strong reason to do so in Fedora), and it would be better to have ways to change this transient hostname as well as $HOSTNAME overwriting.

Just that you know, changes in Fedora get made later in RHEL not viz versa.

Just think about the word it selves localhostmeans a host localy. If you ping localhost in your local network and 10 different clients give answer with different IP’s you get confused too, right.

So I think it is not a bug, it is a feature :wink:

Let’s see what others found out.

I explored a bit deeper into the profiles and found that the $HOSTNAME=fedora is set in the /etc/profile, where it writes hostnamectl --transient as the first choice of $HOSTNAME.

Thus, it is easy to alter the overwriting behavior by just editing the profile to whatever I want.

I do look more forward for a way to further tune the network setting behavior, and will update if I find anything useful.

2 Likes