3

We have a server with a CentOS distribution of Linux. It lost its hostname in the month somehow, since our last reboot. We ran an ETL (extract, transform, load) job last week, and part of that process stops JBoss before moving data to our database, and then restarts JBoss after that automatically. The restart failed.

I solved (with a hack) using this method:

https://stackoverflow.com/questions/6484275/what-causes-the-error-java-net-unknownhostexception/34094741#34094741

We didn't make any changes to the server in the last month (to any files), so I know /etc/hosts wouldn't have changed. I added that extra line (in the answer above), in the /etc/hosts file so JBoss could start.

Two questions:

How does the "hostname -f" command retrieve the hostname from the DNS, per this question? What is it checking on our DNS? Is there a term I should be using to communicate with our server team?

Where does `hostname` store the hostname that I've set?

How do most server administrators handle this, or avoid what happened to me? Do they always set something in the /etc/hosts file to avoid changes to the DNS?

Basically I'm trying to figure out what changed on our network (or DNS) which caused our hostname to go missing. See, the command prompt shows the hostname on the server when I log in. It's strange that I see it there, but not when I call "hostname -f". See "lvs-xyz" below.

[root@lvs-xyz ~]# hostname -f
hostname: Unknown host

If you can't answer all of my questions, I'm mainly looking for what the "norm" is to avoid losing a hostname for the administration of Linux. If I put it into /etc/hosts, then it seems like a hack because the command prompt could change if the kernal value changes, and then it would be different from /etc/hosts if that were to happen.

EDIT:

[root@lvs-xyz sysconfig]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
NETWORK=144.92.154.0
IPADDR=144.92.154.48
NETMASK=255.255.255.192
9
  • Are you using dynamic DHCP so you get a new IP address each time it reboots? That's likely the problem. Commented Dec 10, 2015 at 18:28
  • How can I check without rebooting? I can certainly schedule a downtime to find out. Feel free to answer in both use cases (with and without DHCP) if there is a difference. Commented Dec 10, 2015 at 18:40
  • 1
    You should be able to see something in /etc/sysconfig/network-scripts/ifcfg-eth0 and it'll say something like BOOTPROTO=dhcp. If it does, then you need to contact your networking team and either ask for a static DHCP address, or ask they assign you an address outside of the DHCP range and change it yourself. Commented Dec 10, 2015 at 18:51
  • see edit for the value.. looks like its set to "none" Commented Dec 10, 2015 at 19:00
  • HOSTNAME=lvs-xyz ... I edited the last part (xyz) in this comment and this question because it has our organization name, but there are no dots. Commented Dec 10, 2015 at 19:08

2 Answers 2

2

You can specify it (loaded on reboot anyway) in /etc/sysconfig/network with a HOSTNAME= line (see the documentation here)

You should specify it as a fully qualified name there generally.

If the host doesn't have a name already, and you're using DHCP it will often pick its name up that way.

1

There are a few places where the hostname is stored:

  • In the kernel (/proc/sys/kernel/hostname)
  • In systemd
  • In /etc/hostname
  • In /etc/hosts
  • Possibly others (Dbus?)

So far, the fastest way I've found to change them all is:

sudo hostnamectl set-hostname $new_hostname
sudo vi /etc/hosts   (updating the hostname for the host's ip)
hostname -f          (check your work)

The command /usr/bin/hostname $new_hostname only changes the kernel's hostname (AFAICT).

After that, restart any long-running program that's cached the old hostname, or accept that they're going to keep using it.

Edit: There are some authentication modules that enroll with a central authentication server using the hostname. If the hostname changes, they have to be re-enrolled or re-installed. SSSD comes to mind, plus anything Kerberos-related.

2
  • I think it's even possible to write a custom NSS module to access an arbitrary backend source.
    – Jesusaur
    Commented Sep 28, 2023 at 23:25
  • There are a number of services like NSS modules, systemd-resolved and SSSD that will load the hostname on start and keep on using it until they're restarted, but as long as they're not a persistent source of other programs getting the old hostname then they're only harming themselves.
    – PFudd
    Commented Oct 4, 2023 at 20:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .