To identify hosts on a network, you need a hostname.
And you can set the hostname while you are installing your server, and if you forget
to do so, you can always use hostnamectl.sethostname later to manage hostnames.
This hostname is written to ETC hostname.
And to resolve hostnames, ETC host is used.
And in ETC hosts, you have entries that look like 10011 or whatever you want to use, followed
by the name of the host that you want to use, like server2.example.com server2.
Now a local ETC host is convenient, but in the real world, you want to use DNS, the Domain
Name System.
That means that you are going to refer to a central DNS server by configuring the DNS
client in ETC resolve.conf.
So ETC resolve.conf contains the IP address of the DNS server, and that makes it you can
find all the hosts on the internet by name.
Theoretically, you can influence the order of hostname resolution through ETC nsswhich.conf.
So you can prioritize either the hostname or DNS.
Let me show you.
So for this demo, I'm on another host.
And as you can see, in this host, I didn't specify the hostname while installing it.
So localhost is the name I'm currently using.
And the hostname command is revealing localhost.localdomain.
That sounds very pretty.
So let me use hostname CTL, tab, tab, tab, tab is giving very convenient information.
And as you can see, I'm specifying hostname followed by RHCSA2.example.com.
So now I've set my hostname.
And why doesn't it appear?
Well, because it's a shell setting.
But if you do an exit in sudo minus I to open a new shell, there I can see RHCSA2 as the
current hostname.
Well, all good, you would say.
Well, no, because if you check the ETC hosts configuration file, then what do you see?
You see hostname resolution for localhost.localdomain still.
And you need to fix it, because otherwise, anything that involves the IP stack where
hostname lookup needs to be done is going to be extremely slow.
So I need to add RHCSA2.example.com as well as the short name RHCSA2 to the list.
And also for the IPv6 address, I need to do the exact same thing.
So as a result, at this point, I can ping RHCSA2.
And look at that.
I'm getting an answer from colon, colon, 1.
And you know why?
Well, that is because IPv6 is the default stack.
And that doesn't matter.
What matters is that it works.
Then there is etcresolve.conf.
And in etcresolve.conf, you can see that I'm using a name server 192.168.29.2.
That is the name server that is automatically assigned by the HCP server.
And that is OK.
There's nothing wrong with it.
Now another file that is relevant is the etcnsswitch.conf, where we see what?
Well, there we can see in hosts, the priority is files, and then DNS, and then my hostname.
Now that is kind of funny.
Because if I use ping to google.com, then it's pinging Google on the internet.
But if I would edit etchosts, and I would add an entry like 192.168.29.141, and I'm
calling it google.com, then you know what is going to happen if I ping google.com.
Well, it will ping the local host.
And that is because etchosts, the local etchosts, always has priority.
This is a simple and primitive trick that you could use if you want to make certain
hosts on the internet unreachable.
Just put an entry in the local etchosts, and that will always prioritize.