What is etc hosts file in linux

What is the purpose of /etc/hosts?

My /etc/hosts contains 127.0.0.2 mycouchdb bookmarkdb myothercouchapp and 127.0.0.3 myapachi website I use these to help me with my development running on my local machine. I have 3 services listening on port 80. I don’t want to put them on the DNS, as they are only local.

Does Linux not have a man page for this where it is explained in some detail? This is the most fundamental of questions.

3 Answers 3

The file /etc/hosts started in the old days of DARPA as the resolution file for all the hosts connected to the internet (before DNS existed). It has the maximum priority, meaning this file is preferred ahead of any other name system. 1

However, as a single file, it doesn’t scale well: the size of the file becomes too big very soon. That is why the DNS system was developed, a hierarchical distributed name system. It allows any host to find the numerical address of some other host efficiently.

The very old concept of the /etc/hosts file is very simple, just an address and a host name:

for each line. That is a simple list of pairs of address-host. 2

Its primary present-day use is to bypass DNS resolution. A match found in the /etc/hosts file will be used before any DNS entry. In fact, if the name searched (like localhost ) is found in the file, no DNS resolution will be performed at all.

1 Well, the order of name resolution is actually defined in /etc/nsswitch.conf , which usually has this entry:

which means «try files ( /etc/hosts ); and if it fails, try DNS.»

But that order could be changed or expanded.

2 (in present days) The hosts file contains lines of text consisting of an IP address in the first text field followed by one or more host names. Each field is separated by white space – tabs are often preferred for historical reasons, but spaces are also used. Comment lines may be included; they are indicated by an octothorpe (#) in the first position of such lines. Entirely blank lines in the file are ignored. For example, a typical hosts file may contain the following:

127.0.0.1 localhost loopback ::1 localhost localhost6 ipv6-localhost ipv6-loopback mycomputer.local 192.168.0.8 mycomputer.lan 10.0.0.27 mycomputer.lan 

This example contains entries for the loopback addresses of the system and their host names, the first line is a typical default content of the hosts file. The second line has several additional (probably only valid in local systems) names. The example illustrates that an IP address may have multiple host names (localhost and loopback), and that a host name may be mapped to both IPv4 and IPv6 IP addresses, as shown on the first and second lines respectively. One name ( mycomputer.lan ) may resolve to several addresses ( 192.168.0.8 10.0.0.27 ). However, in that case, which one is used depends on the routes (and their priorities) set for the computer.

Читайте также:  Чем просмотреть linux диск

Some older OSes had no way to report a list of addresses for a given name.

Using the /etc/hosts file to give a human readable name to a local system within a desktop environment is perfectly reasonable. The hosts file is great to use in a home network or even in a small business environment. This cannot be public side like internet addressing — then you need DNS. If the local network is large enough, or simply cut in different sub-networks, or any other useful reason, DNS is preferred.

The local hosts file & DNS are managed with different priority so there is never any conflicts.

I would disagree about conflicts, I’ve had a number of hard-to-debug problems because host-provision-by-group-A left stale /etc/hosts entries that in the future caused operations-by-group-B (my group) to fail, and we ended up setting something to sanitize the contents of /etc/hosts

I also disagree with never having any conflicts. I had to debug very strange errors due to stale host entries conflicting with DNS changes.

@thrig: Perhaps the problem is that your groups are too large to effectively use /etc/hosts. Works perfectly well for the 5 machines (at the moment) on my home network.

in linux, dare I say there should always be at least 127.0.0.1 localhost within the file /etc/hosts as well as the corresponding IPv6 address for localhost.

It is simply host name resolution to a numerical IPv4 or IPv6 address. When you try to network to (a) unix.stackexchange.com somewhere on earth that is likely not near you, or (b) myotherpc on the local area network in your home, it is either /etc/hosts or some domain name server (DNS) that is doing the host resolution so when you type unix.stackexchage.com which as a human is what you care about, you don’t have to remember 185.53.179.7 for it which is what the computer really needs to know.

As was pointed out, in linux there is /etc/nsswitch typically that defines host name resolution order, meaning check DNS first before checking NIS then lastly check the file /etc/hosts otherwise host not found

How you manage the order checking and what/where/who manages the DNS or NIS service is up to you, or you can override everything by modifying /etc/nsswitch to only check in /etc/hosts and never check with DNS or NIS or.

For a simple home network of few computers, say 2 to N, an admin would simply edit /etc/hosts on every computer to be correct, would be easier, faster, cheaper than setting up a Domain Name Server or some other service to have one master location or authority to do host name resolution to IP address.

for linux web search the history & reason behind localhost . Many things in linux rely on the name localhost resolving to 127.0.0.1 and if that does not happen will cause problems, and this localhost definition would/should happen in a local system file such as /etc/hosts.

/etc/hosts is by default usually the final say and last thing for the linux operating system to fall back on for host name resolution. in Windows XP and 7 the corresponding file is C:\Windows\System32\drivers\etc\lmhosts I don’t know about windows 8 or 10.

Читайте также:  Linux convert ntfs to ext4

Источник

The /etc/hosts File Complete Guide for Linux

What Is the /etc/hosts File and What is its Purpose?

The /etc/hosts or simply the Hosts file is a plain text file that maps IP addresses with their corresponding hostnames. Primarily, the Hosts file is useful when you are working on a local network of computers. It provides some simple sort of hostname resolution.

The /etc/hosts file is usually useful when we are not using DNS or NIS service for resolving IP addresses. In fact, when DNS did not exist, there were no centralized systems for resolving hostnames. Local networks and computers relied on their Hosts file. This file holds the entries for all the known hostnames and their corresponding IP addresses.

After DNS appeared on the scene, computers did not need this file and started using the DNS service for fetching hostnames. Despite these advances, the Hosts file is still relevant on modern operating systems.

What will we Cover?

In this guide, we will see what a Hosts file in Linux is, some use cases of this file, and an application of this file based on the use cases.

Use Cases of the Hosts File

We can assign a domain name to an IP address using the Hosts file. However, these changes are local and will work on the local computer.

The Hosts file is still relevant today, there are some specific use cases for the Hosts file as mentioned below:

  1. We have built a website, but it is not live on the web since we have not registered the domain name for it. However, if we have a registered IP address from our hosting, we can map this IP to a dummy or non-existing domain name and continue building our website. Similarly, in a software testing environment, many web applications run on the local hosts address i.e. addresses of the type 127.0.0.1. Again, we can manage this using the Hosts file.
  2. In case we have migrated our website from one hosting to another one and we want to check the new hosting performance, we can connect our domain to the new hosting without closing our old hosting account. In this way, we can see how our website is loading from the new hosting.
  3. Suppose you want to block a website, like blocking Facebook on an educational system. We can easily do this by mapping the target domain name to an invalid address IP like 0.0.0.0. This will create a loopback. This is usually helpful for blocking non-educational websites in universities or blocking mature content for kids.
  4. When edited in a proper manner, the Hosts file can operate as a security firewall for a system.

Format of the /etc/hosts File

The Hosts file is located inside the /etc folder. The entries of the file are per line basis. For instance, every single line consists of a hostname followed by its IP address:

The IP addresses used here are either IPv4 or IPv6. These addresses and hostnames maintain a distance of any number white space or a distance of a tab character.

Читайте также:  What process is using my memory linux

On all operating systems, the Hosts file has the same format. Let us see the contents of the file on a Linux system:

# The following lines are desirable for IPv6 capable hosts
:: 1 ip6-localhost ip6-loopback
fe00:: 0 ip6-localnet
ff00:: 0 ip6-mcastprefix
ff02:: 1 ip6-allnodes
ff02:: 2 ip6-allrouters

The Hosts file contains the entries for both the IPv4 and IPv6 addresses.

Application of the Hosts File

As mentioned earlier, we can use the Hosts file for blocking a domain. Let us take the case of blocking Youtube. Open the file:

Use the tab character to insert space between the IP address and its corresponding domain name.

Now, save and close the file. Open any web browser and try to navigate to youtube and see what happens:

We can see youtube.com is blocked and we are getting an ‘Unable to connect’ message. To unblock the site, we have to simply remove the above entry from the /etc/hosts file.

Access Control Files

Besides the plain Hosts file, we also have a /etc/hosts.allow file and a /etc/hosts.deny file.

These files, referred to as hostess access files, are used by TCP wrappers. These files decide whether a client machine can connect to a host.

When a TCP wrapped service gets a client request, it performs the actions below:

  1. Refer to the hosts.allow file: it sequentially reads this file and executes the first rule stated for that service.
  2. Refer to the hosts.deny file: it sequentially reads this file and if a matching rule is found, it does not accept the connection request. Otherwise access is granted to the requesting service.

The hosts.allow file decides which IP addresses can connect to a host. The /etc/hosts.deny file is used in conjunction with it.

Security Issues of the Hosts File

Although the Hosts file looks like a simple file, in some cases, for example, on Windows systems, malware like adware or spyware modifies the Hosts file to take the users to malicious websites.

If you are unsure about the health of your Hosts file, you can use Lynis system auditor for Linux. Lynis has its built-in test build for checking the security strength of your Linux system.

Also, try to keep the Hosts file at a minimum length. If you have too many systems declared inside the Hosts file, you should consider placing them in a different DNS zone.

Conclusion

The Hosts file is an old file on Linux, but still, it is a powerful utility. Right from local name resolving, it is an important part of many software and their development process. In this article, we have seen a basic introduction to the Hosts file in Linux. We have also shown how to block a website using the Hosts file. Also, we can use it to block banners, adware, and other third party page counters.

After reading this article, you should now know how to use the Hosts file for managing DNS queries as per our requirements.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.

Источник

Оцените статью
Adblock
detector