How can I configure the hostnames of my servers from services on the network? What should I use? A DHCP? A DNS? Both?

I'm trying to establish a way to configure my servers' hostnames in a centralized way, that is, from some service on the network that does it.

I know there are three names that identify a server...

  • Transient: Received from network configuration;
  • Static: Provided by the kernel;
  • Pretty: Provided by the user.

So I wanted my CentOS 7/8 server to use transient hostname as its name. In a practical way, that when logging in the terminal it shows me the name that was obtained from the network...

[user_name@my-net-hostname ~]$

... and that the machine can at least identify itself by name...

[user_name@my-net-hostname ~]$ ping -c 4 my-net-hostname
PING my-net-hostname.my.domain (10.3.0.4) 56(84) bytes of data.
64 bytes from my-net-hostname.my.domain (10.3.0.4): icmp_seq=1 ttl=64 time=0.193 ms
64 bytes from my-net-hostname.my.domain (10.3.0.4): icmp_seq=2 ttl=64 time=0.086 ms
64 bytes from my-net-hostname.my.domain (10.3.0.4): icmp_seq=3 ttl=64 time=0.077 ms
64 bytes from my-net-hostname.my.domain (10.3.0.4): icmp_seq=4 ttl=64 time=0.098 ms

--- my-net-hostname.my.domain ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 0.077/0.113/0.193/0.046 ms

NOTE: In the case above this hostname was defined in a DHCP and there is also a DNS that knows the name "my-net-hostname".

NOTE: It seems to me that the correct way to do this is with a DHCP and a DNS, that is, one defines the server name (hostname) and the other defines where to find this server, but I'm having difficulty confirming this information.


QUESTION: How can I configure the hostnames of my servers from services on the network? What should I use? A DHCP? A DNS? Both?


Thanks! =D

[Ref(s).: https://askubuntu.com/questions/104918/how-to-get-the-hostname-from-a-dhcp-server , https://codingbee.net/rhcsa/rhcsa-configuring-hostnames-and-dns , https://www.redhat.com/sysadmin/set-hostname-linux ]


Solution 1:

So I wanted my CentOS 7/8 server to use transient hostname as its name. In a practical way, that when logging in the terminal it shows me the name that was obtained from the network...

That's what it already does, though.

In systemd-hostnamed, "transient" hostname is what's currently set in the kernel. "Static" hostname is the one defined in /etc/hostname. Yes, the RedHat article is wrong. (The terminology was invented by systemd for its 'hostnamed' tool, thus see its API docs for more information.)

("Pretty" hostname isn't even a hostname, in the usual sense. It's used as a mDNS "service" name and possibly some other things, but not for actual address lookup.)

The "static" hostname is not the one provided by the kernel. It is provided by you through hostnamectl or by editing /etc/hostname – it is only given to the kernel on startup, not the other way around.

The "transient" hostname is the kernel hostname. It is also provided by you, this time through the hostname command (not to be confused with hostnamectl) or via /proc/sys/kernel/hostname. It is only that the transient hostname may be overridden by the network, but when the system boots it's still initialized from the static one (i.e. from /etc/hostname).

"Network configuration" here primarily means DHCP. Usually the DHCP client reports its own current hostname and the DHCP server only collects them, but the server can include a different hostname that it wants the client to use instead. If the client receives a new hostname via DHCP, that becomes the "transient" hostname aka the kernel hostname. (There is not much point in actually doing that, especially not for long-lived servers.)

The hostname is not set from DNS. However, it makes sense for it to be in your DNS domain, as that's usually the whole point of a hostname.

Whether the computer can ping its own hostname depends on several things (DNS among them), but usually your networking software will try to ensure that this works even without DNS – either by updating /etc/hosts, or by using libnss-myhostname (which always provides a virtual answer for the system's current transient hostname). Various things (such as sudo) can mysteriously break if the machine cannot resolve its own name to something at least.

(In fact, various things can mysteriously break if you let the machine to dynamically update its transient hostname from DHCP, too. If you enable this on a desktop/laptop and your graphical X11 programs start failing with "No protocol specified" errors, this is usually the cause.)

The "domain" part isn't usually stored in the hostname field at all. It's again sometimes defined in /etc/hosts, but it can also be defined through DNS. I'll link to an older post I already did about this...

Also, where the RedHat article talks about conflicts and "hostname-1", it only applies to the Avahi mDNS service, which tracks the system's mDNS name somewhat independently from the global hostnames. (The collision detection itself is only done within mDNS.) That is, if there is another mDNS-capable host with the same name, Avahi will choose a new mDNS name-1 or -2, but this does not actually affect the system hostname in any way.

"DHCP" and "DNS" are not countable items; they're abstract protocols. You can have "a DHCP lease" or "a DHCP server" or "a DNS domain" but it doesn't make any sense to talk about "a DHCP" or "a DNS".