Tryhing to change hostname for 2 days!

Almalinux 8.10 in a VPS …
NetworkManager 1.40.16-15.el8_9

This is very frustrarting, I am trying to change hostname for more than 2 days :tired_face:

This command works in all servers I have in various versions and configurations, but NOT in this one. It changes back after reboot:

I want to change myserver.tntcode.net (default) to new.myserver.tntcode.new

hostnamectl set-hostname new.myserver.tntcode.net
reboot

My web host / ISP can change the hostname for me, and that works fine, but I want to be able to change from my end, from the command line.

After rebot I always see it chanfing int the logs /var/log/messages

Sep 16 22:45:42 new NetworkManager[630]: <info>  [1726541142.2514] device (eth0): Activation: successful, device activated.
Sep 16 22:45:42 new NetworkManager[630]: <info>  [1726541142.2518] manager: NetworkManager state is now CONNECTED_GLOBAL
Sep 16 22:45:42 new nm-dispatcher[670]: req:23 'up' [eth0]: new request (2 scripts)
Sep 16 22:45:42 new nm-dispatcher[670]: req:24 'connectivity-change': new request (2 scripts)
Sep 16 22:45:42 new chronyd[628]: Source 178.239.19.60 online
Sep 16 22:45:42 new chronyd[628]: Source 129.250.35.251 online
Sep 16 22:45:42 new chronyd[628]: Source 185.51.192.62 online
Sep 16 22:45:42 new chronyd[628]: Source 94.198.159.15 online
Sep 16 22:45:42 new nm-dispatcher[670]: req:23 'up' [eth0]: start running ordered scripts...
Sep 16 22:45:42 new nm-dispatcher[670]: req:24 'connectivity-change': start running ordered scripts...
Sep 16 22:45:42 new systemd-hostnamed[654]: Changed static host name to 'myserver.tntcode.net'

Sep 16 22:45:42 new NetworkManager[630]: <info>  [1726541142.4532] hostname: static hostname changed from "new.myserver.tntcode.net" to "myserver.tntcode.net"
Sep 16 22:45:42 new NetworkManager[630]: <info>  [1726541142.4533] audit: op="hostname-save" arg="myserver.tntcode.net" pid=1252 uid=0 result="success"

Sep 16 22:45:42 new nm-dispatcher[670]: req:25 'hostname': new request (2 scripts)
Sep 16 22:45:42 new nm-dispatcher[670]: req:25 'hostname': start running ordered scripts...
Sep 16 22:45:42 new qemu-ga[682]: info: guest-exec close [stdout cid=2] queue=0 total=0
Sep 16 22:45:42 new qemu-ga[682]: info: guest-exec close [stderr cid=3] queue=0 total=0

I understand NetworkManager changes my hostname when it connects to network, I tried setting these in /etc/NetworkManager/NetworkManager.conf but I find the documentation very unclear regarding what goes where, if I need to add the configs under [main] or under [connection] or even what values to use, but I think these are related:

[main]
hostname-mode=none
hostname.from-dhcp=0
hostname.from-dns-lookup=0
hostname.only-from-default=1

Edit: when I change hostname with hostnamectl set-hostname new.myserver.tntcode.net, without rebooting, the service NetworkManager status mentions hostname is unmanaged and it remains set to new hostname until I reboot.
It also keeps new hostname and hostname is unmanaged if I restart NetworkManager service NetworkManager restart.

Sep 16 22:58:20 myserver.tntcode.net NetworkManager[616]: <trace> [1726541900.1130] l3cfg[1238d57f0644a7d4,ifindex=2]: emit signal (platform-change, obj-type=ip6-route, change=add>
Sep 16 22:58:20 myserver.tntcode.net NetworkManager[616]: <trace> [1726541900.1130] l3cfg[1238d57f0644a7d4,ifindex=2]: emit signal (platform-change-on-idle, obj-type-flags=0x28)
Sep 16 22:58:20 myserver.tntcode.net NetworkManager[616]: <trace> [1726541900.1131] policy: set-hostname: hostname is unmanaged
Sep 16 22:58:24 myserver.tntcode.net NetworkManager[616]: <trace> [1726541904.1546] device[7f2db18f7addb8fa] (eth0): remove_pending_action (0): 'carrier-wait' not pending (expecte>
Sep 16 23:17:04 new.myserver.tntcode.net NetworkManager[616]: <info>  [1726543024.9421] hostname: static hostname changed from "myserver.tntcode.net" to "new.myserver.tntcode.net"
Sep 16 23:17:04 new.myserver.tntcode.net NetworkManager[616]: <debug> [1726543024.9421] dispatcher: (25) dispatching action 'hostname'
Sep 16 23:17:04 new.myserver.tntcode.net NetworkManager[616]: <trace> [1726543024.9422] policy: set-hostname: hostname is unmanaged
Sep 16 23:17:04 new.myserver.tntcode.net NetworkManager[616]: <debug> [1726543024.9566] dispatcher: (25) succeeded (after 0.014 sec, 2 scripts invoked)
Sep 16 23:17:04 new.myserver.tntcode.net NetworkManager[616]: <debug> [1726543024.9566] dispatcher: (25) /etc/NetworkManager/dispatcher.d/20-chrony-dhcp succeeded
Sep 16 23:17:04 new.myserver.tntcode.net NetworkManager[616]: <debug> [1726543024.9567] dispatcher: (25) /etc/NetworkManager/dispatcher.d/20-chrony-onoffline succeeded

Do you have the same results from using console gui interface nmtui ?
just type nmtui and enter. You need sudo or root to make changes.
Never had this problem. But could there be a setting or service that sets / configures the hostname after reboot.

For comparison, piece go log from AlmaLinux 8 that is also a VM and on almost default config:

Sep 25 11:31:37 ldap7 NetworkManager[703]: <info>  [1727253097.2489] manager[0x55e9ea8a5080]: monitoring kernel firmware directory '/lib/firmware'.
Sep 25 11:31:37 ldap7 dbus-daemon[612]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service' requested by ':1.3' (uid=0 pid=703 comm="/usr/sbin/NetworkManager --no-daemon " label="system_u:system_r:NetworkManager_t:s0")
Sep 25 11:31:37 ldap7 systemd[1]: Starting Hostname Service...
Sep 25 11:31:37 ldap7 dbus-daemon[612]: [system] Successfully activated service 'org.freedesktop.hostname1'
Sep 25 11:31:37 ldap7 systemd[1]: Started Hostname Service.
Sep 25 11:31:37 ldap7 NetworkManager[703]: <info>  [1727253097.3750] hostname: hostname: using hostnamed
Sep 25 11:31:37 ldap7 NetworkManager[703]: <info>  [1727253097.3751] hostname: static hostname changed from (none) to "ldap7.mydomain"
Sep 25 11:31:37 ldap7 NetworkManager[703]: <info>  [1727253097.3756] dns-mgr: init: dns=default,systemd-resolved rc-manager=symlink

This is from boot. The first and last lines are probably irrelevant.
The host has static hostname (set with hostnamectl).

It looks like NetworkManager checks whether there is systemd-hostnamed.service. Since there is, NM gets hostname from it.

If there is no config for static hostname, then NM will use the hostname that it receives from DHCP server (if any). That will be a transient hostname.


Overall, the hostname, as known by the machine, affects at most the machine itself. For everyone else the (DNS) resolved names are more important.

Do you have the same results from using console gui interface nmtui ?
just type nmtui and enter. You need sudo or root to make changes.
Never had this problem. But could there be a setting or service that sets / configures the hostname after reboot.

With nmtui I seen same results as hostnamectl set-hostname, it changes to default static one after reboot.

I was only able to set a transient hostname by

  • setting a kernel.hostname = my.host.name inside this sysctl file: /lib/sysctl.d/60-my-custom-settings.conf
  • adding hostname-mode=none in /etc/NetworkManager/NetworkManager.conf

After restart, results of above are:

  • the command hostname; shows new hostname
  • file /etc/hostname contains old default hostname
  • command hostnamectl; shows my new hostname only as transient one, and old default hostname as static one.

So that looks like a temporary fix.

It looks like NetworkManager checks whether there is systemd-hostnamed.service . Since there is, NM gets hostname from it.

Where does hostnamed.service take the hostname from ? Maybe I can try edit that.

It does look like some program/service is changing it at boot time, but it might be related to this host’s virtualisation, they must run some custom commands on boot time.

It is better to check whether your NM calls hostnamed before we start hunt for hostnamed. To do that you should show messages from boot, where you did set hostname before reboot. (Your earlier log excerpts did look like from “after boot”.)

It is true that cloud systems can have additional tooling. Cloud-init is common.

/etc/hostname should contain the updated hostname.
I use this file in my backup process to restore the hostname.
Maybe try to edit this file directly.

man 5 hostname

describes how hostname is set (on boot).

Wait. I was looking at el9’s man-page. It hostname(5) - Linux manual page is more verbose than in el8. I wonder whether the facts are valid already on el8?