/etc/hosts Explanation

kevr
  13 years ago
  7

This is a short tutorial about the basics of using /etc/hosts on a system with several IP addresses.  I haven't seen much of a plain tutorial about setting up /etc/hosts around, so I hope some people get some use out of this.

First off, if you are running a server with a paid domain, it'd probably be smart of you to setup a route from your local host name to the host map of your local network on Linux. When you installed Linux Mint, or any other distribution, you chose a host name (or a computer name) to use. You will usually see this on a shell prompt on the system you are running, after the user@. For example, my laptop has the guest user kevr - and my host name is LOC, so my shell prompt by default gives: [kevr@LOC][~]$ - in my home folder.

After you've understood that concept, it's time to setup your /etc/hosts file to match up with the hostname you've used. Keep in mind, this is not required for everything, but it's a good thing to do for the future if you need a server or a program that relies on the host name.

$ sudo nano /etc/hosts

or use your favorite editor.

A default /etc/hosts file should look something like...

# Comments Here
127.0.0.1     localhost.localdomain      localhost
# End of /etc/hosts

The format of the hosts file is: SOURCE DOMAIN HOSTNAMES.

So, whatever you format in your /etc/hosts file will resolve accordingly in any network action on the Linux install. Let's take for example you had three IP addresses to the box you are on, you could use something like:

# Comments here
127.0.0.1    localhost                         localhost
68.23.41.6  first.domain.com           first                hostname
68.23.41.7  second.domain.com    second
68.23.41.8  third.domain.com          third
# End of /etc/hosts

first, would be pointing to 68.23.41.6 (should have first.domain.com A hosted to 68.23.41.6 on a DNS) and accordingly for the second and third. Notice that first has hostname to the right of it, in my case, it would be LOC. first would be the default domain found when a service looks for the host name LOC on my install. An example service that does this is the exim mail server.  It is only appropriate to host to WAN IP addresses if you have more than one WAN IP bound to your system.

Here is an example of an official server.

[user@scrappers][~]$ cat /etc/hosts
# Comments here
127.0.0.1    localhost                             localhost
68.23.41.6  mail.irc-scrappers.com   maild                scrappers
68.23.41.7  irc.irc-scappers.com        ircd
68.23.41.8  ssh.irc-scrappers.com    sshd
# End of /etc/hosts

Now, you can successfully test "ping maild" or "ping scrappers", any of the hosts you setup can be resolved as long as your network is up and running.

Okay, I might have went a little overboard on that, but it's very important to learn - you need to be able to manage your hosts correctly when running a server.

Comments
Keith 13 years ago

It is true that "probably" not many people use Linux Mint as servers, since most people use it because it is easier to setup and use than other distros.

But....

Many people have home networks where they want to connect other computers in order to easily share and transfer data. If they do this with a second network card this type of info can be very useful.

But be aware that:

Many times other configuration files have to be changed since currently most distros are not good at automatically setting up and connecting multiple network cards. So if you are trying to setup such a system do not think that changing the host file is the only thing that has to be done.

**** ckristi ****

It must be frustrating when you know all the answers, but nobody bothers to ask you the questions.

The only people I've ever worked with who believed what you said:"since there is enough good documentation on this subject in the manual pages and over the Internet" implying that no more documentation or discussion is needed--were the ones who paid other people to solve their problems when they tried to do it and failed, instead of continuing to work at it.

Forums are not books, forums are where you discuss things.... Much of the educational value of "How To" forum posts come from the interaction between the readers and the author and the new concepts, information and ideas that result--not "just" from the authors efforts as you seem to think.

Since your social and communication skills don't seem to be fully developed, you might not understand that trying to force people to do things the way you want and threating them (although I do admit that threating to demote them is hilarious) is ALWAYS taken personally... just like I expect you will take my post, even though I am just trying to help improve your social skills.

In theory there is no difference between theory and practice.
In practice there is.


kevr 13 years ago

True, it is at /bin. Apologies for not double checking the path at 6:00AM.


ckristi 13 years ago

@kevr: since there is enough good documentation on this subject in the manual pages and over the Internet and you seem to take our pertinent comments personally instead of trying to improve the tutorial I feel forced to "demote" this tutorial and render it useless.

And by the way, the hostname executable is under /bin, not in /usr/bin.


kevr 13 years ago

@ckristi: The terminology I used in the tutorials pertains to the system hostname, '/usr/bin/hostname'. They are using the general hostname terminology..

@remoulder: Even if not everyone on here sets up servers, is it such a crime to share information to people? Since most users who endeavor will set up servers eventually or at least mess with them. It's still knowledge.

Thanks for flooding the comments with bashes guys. I'm done.


remoulder 13 years ago

@Suhana: it seems you're prone to over emotional outbursts! I wasn't knocking the post, simply pointing out that Mint is aimed at desktop users as described in the FAQ at http://www.linuxmint.com/faq.php:
"What is Linux Mint?

Linux Mint is a free Linux-based operating system designed for use on desktop computers."
few of whom are likely to be running any kind of server as the post implies. Please take a step back and reconsider before reading between the lines and jumping to the wrong conclusions.


ckristi 13 years ago

@kevr: please try reading the Hosts (file) wiki page or, for convenience, the manual page for the /etc/hosts file (man 5 hosts).

Excerpt from the Hosts wiki page:
The hosts file contains lines of text consisting of an IP address in the first text field followed by one or more hostnames, each field separated by white space (blanks or tabulation characters).
Excerpt from the hosts manual page:
This manual page describes the format of the /etc/hosts file. This file is a simple text file that associates IP addresses with hostnames, one line per IP address.
For each host a single line should be present with the following information:
IP_address canonical_hostname [aliases...]


ckristi 13 years ago

First of all, congratulations for your effort to write this review.
Second of all will be me disagreeing with what you have written. Please amend your review in order to provide clear information for new users.
My recommendation would be to check the Wikipedia page for the Hosts (file), but I'll try to write some punctual issues that I've found misleading:

- The /etc/hosts file has relevance only locally on the host it is present.
Servers use DNS as a service to provide name resolutions to a LAN or for the Internet.
- The correct format for the hosts file is: IP hostname1 hostname2..
It has nothing to do with the domain name. Of course you can add to the hostname the FQDN (fully qualified domain name, but it is not mandatory to be the second entry on the line; every single word on the right of the IP will be treated as a hostname)


remoulder 13 years ago

Mint is not intended to be a server OS so this might be of limited use.