Prevent Network Setup From Changing The Hostname

Home » CentOS » Prevent Network Setup From Changing The Hostname
CentOS 9 Comments

How can I block network setup (via NetworkManager) from changing the machine’s hostname whenever the network configuration changes?

The problem: When my graphical login session starts, the xauth database gets an token that is labeled with the hostname at that time. If there is not yet a network connection, that will be
“localhost.localdomain”. When a network connection is then made
(my wireless connection needs the key from my login), the hostname changes. That breaks X session sharing because there is no xauth token that matches the current hostname. If I get a root shell with “su -“, commands launched from that shell cannot access the display. If I set up an SSH connection with “ssh -X”, I get a complaint about missing xauth data.

On systems with an “ONBOOT=yes” network connection, there is no problem since the hostname is set before the X session starts. It’s just when the hostname changes during the X session that there is a problem.

I am running CentOS 6, fully updated. It’s taken me a long time to track down the root cause of this problem. Now I just need a solution, preferably something less ham-fisted than “xhost -“.

9 thoughts on - Prevent Network Setup From Changing The Hostname

  • Make it a system connection instead of a user connection. Or give the host a static name on install and don’t allow dhcp to override it.

  • Making my wireless connection a system connection increases the exposure of my WPA key and doesn’t solve the problem of the network configuration changing, perhaps because I connected or disconnected an ethernet cable or the machine went to sleep on one WLAN and woke up on another.

    Do you know of a place I can set a static name that NetworkManager won’t override? That would be ideal. I just doesn’t make sense that the machine’s internal relationships would depend on its external connections.

  • If you move networks and you are slaving your hostname to the DHCP
    offered name, then yes. But why do that? In /etc/dhcp/dhclient.conf you can configure exactly what you want and don’t want from the server. There’s a lot of options (man dhclient.conf is very helpful) but here’s an example:

    send dhcp-client-identifier = hardware;
    request subnet-mask, broadcast-address, time-offset, routers,
    domain-search, domain-name, domain-name-servers, host-name;

    Just take out the host-name and you won’t get (a new) one. You should however make sure that all your servers have a hostname configured before you do that. /etc/sysconfig/network is where you do that on CentOS6.

    So your key isn’t visible and only root can change a system device. A
    system device gets activated before the desktop. So you’re not depending on having access to gconf etc.

    See above. It’s standard dhclient options.

  • # which hostnamectl
    /usr/bin/which: no hostnamectl in
    # yum provides \*/hostnamectl
    No Matches found

    Apparently does not exist for CentOS 6.

  • NetworkManager invokes dhclient with a generated config file that ignores /etc/dhcp/dhclient.conf:

    dhclient … -cf /var/run/nm-dhclient-wlan0.conf …

    I ran across another report that suggests setting HOSTNAME to something other than “localhost.localdomain” in
    /etc/sysconfig/network would fix the problem. For the moment, that seems to be working.

  • Well, true to a degree. Put the file in /etc/dhcp/dhclient.d and it’ll be executed. Just make sure the script out-puts to stdout what goes into the dhclient configuration file used by NetworkManager.

    Strange – you may have a dhcp server that accepts host names from the clients – which of course would fit your use case. Just realize that not all dhcp servers are setup to be that “lenient” when it comes to preserving the host name picked by a client.

    But I am happy you got it working.

  • No, it’s a systemd thing, I think, started with RHEL7.

    I’ve always used /etc/sysconfig/network for the hostname entry, which works through CentOS-6.x

  • I can see in Wireshark that the client is not requesting any hostname in the DHCP Request and is ignoring the hostname that is returned in the DHCP ACK in favor of the one specified in /etc/sysconfig/network as long as the latter is _not_ “localhost.localdomain”.