0

We have our servers running CentOS 4.9. I want to find how /etc/hosts file is created during installation.

A related question here (What creates /etc/hosts (RHEL 5.5)) says that in RHEL 5 /etc/hosts file is created by ananconda.

We use anaconda-10.1.1.46-4.x86_64.rpm. I extracted this RPM and found a file /usr/lib/anaconda/network.py. This Python file creates the /etc/hosts file.

After reading through this python script I could see that localhost.localdomain domain alias name should be added in the /etc/hosts file.

But during installation I could see that the /etc/hosts file is created with only.

127.0.0.1   loghost

We also have an ks.cfg file which creates /etc/hosts file with the following content

127.0.0.1         localhost loghost

So after installation and reboot, the /etc/hosts file is created as specified in the ks.cfg file.

I am bit confused here how the /etc/hosts file is created with only 127.0.0.1 loghost during installation.

EDIT:

Note: We bundle our application RPMs with the CentOS rpms and build an ISO.

The real problem is we are moving from CentOS 4.9 to 6.5. In CentOS 6.5 setup-2.8.14-20 rpm creates the hosts file with the following contents

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

localhost.localdomain is added in the hosts file.

So we are trying to find the difference between CentOS 4.9 & 6.5 /etc/hosts file creation during installation, because our application is tightly bound with the entries in /etc/hosts file.

1

I'm not sure of the answer to your question as-asked, but I think I have the solution to your real problem.

Instead of simply writing out a fresh /etc/hosts file, potentially overwriting the one already present, you can append the entries you need to the end. There is no functional difference between:

127.0.0.1 localhost loghost

and

127.0.0.1 localhost
127.0.0.1 loghost

That is, you do not need to collect all names for 127.0.0.1 on the same line. You just need to append a single line with the loghost entry you need after Anaconda creates the stock version.

Since you're doing this from an RPM you created, it means that you replace the way you're currently modifying or creating /etc/hosts with something like this in your RPM's %post script:

if ! grep -q loghost /etc/hosts
then
    echo '127.0.0.1 loghost' >> /etc/hosts
fi
  • Thanks for your inputs.I have edited my question with more information. – Arun Dec 2 '14 at 18:58
  • @Arun: Does my added %post script snippet help? – Warren Young Dec 2 '14 at 19:12
  • /etc/hosts file gets created before our RPMs are installed. One of our RPM installation depends on the entries in the /etc/hosts file. We have an check in the %post section of our RPM spec file checking for entries in /etc/hosts file. We see differences between contents of /etc/hosts(before our RPM is installed) file in CentOS 4.9 & CentOS 6.5 before our RPM gets installed. So we are trying to find how /etc/hosts file is created during installation. The %post section in our ks.cfg file overwrite the /etc/hosts file(which gets created during installation) after installing all the RPMs – Arun Dec 3 '14 at 10:16
  • I still don't see the problem. Use your %post script to make /etc/hosts look the way your RPM needs it to look by appending the entries you need. Don't just blindly overwrite it. – Warren Young Dec 3 '14 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.