Hosts file - what is its relevance for incoming connections?

I'm confused over the hosts file. I have been for a long time and I have searched and read, but still not exactly got a grasp of it. I have two websites up and running on Linode as I always struggle through, but it's time I understood what's going on.

I thought the hosts file was only consulted on outgoing connections. For example I can put the following into the hosts file of the computer I'm typing on now, and as a result will take me to

But when I'm working on either of my Linodes I do not understand the point of making ip and domain name connections in their hosts files. The incoming connection is routed to my Linode by the A record at my domain host (another company). When the incoming connection gets to my linode is the hosts file consulted at all, or does the routing all happen via the virtualhosts settings I've setup?

Another thing - and it's a related question.

So far I have a one linode per website, but if I was running 3 websites from one linode would I need to have 3 entries for these in my linode's host file? I'm thinking no, as the apache virtualhosts settings would take care of this.

10 Replies


/etc/hosts is an adjunct to DNS for the node on which the /etc/hosts file resides. By the time you get an incoming connection from some remote party, DNS has already done it's job…so, IMHO, /etc/hosts doesn't have much, if any, relevance to unsolicited connections from nodes remote to you.

-- sw

One reason for the hosts file is to set the fqdn on the host.

Traditionally you put the hostname "betty" in /etc/hostname and set the fqdn "" in /etc/hosts

If you do this, the command hostname will return "betty" and the command hostname -f will return "". Some daemons need to know the host's fqdn and hostname.

(some people put the fqdn in /etc/hostname so the commands hostname and hostname -f both return "". I think that works but I prefer the traditional way)

Other than that, you don't need to use it.

All it does is map a domain name to an ip address. and I guess the reverse, maps an ip address to a domain.

If that mapping is in the DNS then you don't need to add it to /etc/hosts. You might do, though, if it will be looked up frequently to save time doing frequent DNS lookups. I think systemd caches DNS look ups so that might not be an advantage any more.

Thanks to you both for your replies.


Thanks for that information. That's kind of what I was thinking must be the case.


I think I have set my linode's hosts and hostname files up more or less as you say it's done traditionally.

Here is what I have in hosts. localhost

My hostname is

Yesterday I created a subdomain, set up the required virtual host file and directories, and created a new wordpress site on it. It's working great.

But I did not add anything to the hosts file, and things I have read make me feel that I should have. Should I? My hosts file contains no reference to It's still as you see it above.

I cannot help but wonder if I could even remove the 2nd line and just have localhost

As you can see I'm still not fully grasping this thing.

@nnn07 --

You write:

Here is what I have in hosts. localhost

IMHO, the second one is unnecessary (and incorrect IMHO…probably a typo). If you want to give localhost two names, you do it like this: localhost

All /etc/hosts does for you is short-circuit DNS on the node where /etc/hosts resides. Let's use an example… Suppose the DNS resolves: ->

You fire up your web browser on your laptop and navigate to:

After DNS does it's job, your browser will connect to the web server at and show you the page at /. So far, so good…

Let's say that you create an /etc/hosts entry that says:

You fire up your web browser on your laptop and navigate to:

After DNS does it's job, your browser will connect to the web server at and show you the page at /. Probably not so good…as you don't have a web server running on your laptop. The presence of the /etc/hosts entry has masked the DNS resolution (actually, it's prevented the the browser on your laptop from making a DNS request at all…since the domain name was resolved locally).

This is useful if you want to prevent the browser on your laptop from ever navigating to

This is also useful if you have test servers on a private LAN. I have an SBC in my living room (a Raspberry Pi-like device). It serves a development version of a website. The SBC has no domain name and no public IP address (it's on the LAN side of my router). To disambiguate requests to the production version, I rely on DNS to do it's job: ->

To navigate to the development version, I rely on the /etc/hosts entry:

so ->

My Linode cannot navigate to because it is only known to my laptop…not my ISP's DNS server (even if it was, the IP address won't be routed to the WAN side of my router… is a private network).

The important thing to remember is that /etc/hosts only affects activity on the node where it lives. An /etc/hosts entry on your Linode will not affect any activity on your laptop (and vice-versa).

-- sw


Thanks. That post will be helpful for me as there's lots of information in it.

I think I'm now understanding what was confusing me so much. When I switched to Linode for my websites I was very new to Linux (I'm still quite new). Anyway, I used this page to do the initial setup stuff.


Quite far down the page you will find this section on the hosts file.

Update Your System’s hosts File

The hosts file creates static associations between IP addresses and hostnames or domains which the system prioritizes before DNS for name resolution. Open this file in a text editor and add a line for your Linode’s public IP address. You can associate this address with your Linode’s Fully Qualified Domain Name (FQDN) if you have one, and with the local hostname you set in the steps above. In the example below, is the public IP address, example-hostname is the local hostname, and is the FQDN.

File: /etc/hosts localhost.localdomain localhost example-hostname


Since I'd just been through the section on that page telling me how to set the hostname on my Linode I assumed this section on the hosts file was also meaning I should do it on my Linode. Now that I'm reading it again I think it is telling me that this should be done on my local computer so I can go straight to my Linode (say for example because my domain is still propagating).

This is why I was asking how a hosts file was relevant to incoming connections. I though Linode's setup instructions were telling me how to setup the hosts file on my linode and I could not understand what the point of that was (and now I see there would be little unless I wanted to route my Linode directly to some other ip without querying a dns server).

If I am right about the above then the cause of my confusion is now clear to me, and the scales have fallen from my eyes :-)

You write:

The [/etc/]hosts file creates static associations between IP addresses and hostnames or domains which the system prioritizes before DNS for name resolution.

This is exactly correct.

Now that I'm reading it again I think it is telling me that this should be done on my local computer so I can go straight to my Linode (say for example because my domain is still propagating).

This is exactly correct.

In the dark ages before DNS (the early 1980s), HP distributed a company-wide /etc/hosts file from Corporate. You had to install it on every computer on the company network you were responsible for (and wanted to be known). Every time Corporate assigned/moved/un-assigned an address anywhere in the world, a several dozen megabyte file had to be distributed/installed on every computer participating on the company network. You can imagine what an unbelievable hassle that was…updates several times a day…

The first thing you did when you got a new workstation or new computer in your workgroup was install the cron(8) job from your old workstation that ftp'd the Corporate /etc/hosts file from HPLabs every hour (changes or no).

It was a firing offense to allow that file to leak to the outside world.

The first question in troubleshooting a communications issue was "What's the date code on your /etc/hosts?"

Of course, Unix systems were bit players on the company network in those days. Most of the traffic was an HP-proprietary system called DS (for Distributed Systems… original, huh?)…a departmental LAN system based entirely on static addressing and virtual circuits…and had only recently been migrated from SDLC to "EtherNet".

Ahh… the good ol' days… ThickLAN, no switched fabrics, no DNS, no CIDR, no firewalls… You had your (class A, IPv4) address and your company-issue subnet mask and that was that… You had your three email addresses:

  • HP Desk[Manager] - Steve Wi/HP-SID/LAB (really proprietary!);
  • Unix for inside HP - (internet); and
  • Unix for outside HP - …!ucbvax!hplabs!hpsid!hpsidc05!stevewi (uucp)

The last one was called your UUCP bang path… You can Google it.

What a long strange trip it's been!

-- sw


Yes, I read about that when I was searching the internet trying to understand why I was getting so confused. Hosts came before dns and everyone needed the file - I can imagine the headaches that caused :-)

It sounds like you have been using computers and Unix linux for a fair bit longer than me. I got my first computer in 2001 and then switched to Linux (completely - no dual boot machines) in March 2020. Should have switched to Linux years ago. Windows was very boring and disrespected my privacy in a big way - but I got my work done. Linux is private, great fun to mess about with, and I still get my work done.

All my confusion came from me thinking that Linode were instructing me to set up the hosts file on my Linode. Now I see the light and I'm glad. The whole question was giving me a sore head :-)

Many thanks for replying to me. It was when I was reading your 2nd last post that the penny dropped. Something you wrote made me see what I was getting wrong in my head. Thanks again !!

You write:

It sounds like you have been using computers and Unix linux for a fair bit longer than me. I got my first computer in 2001

My first exposure to Unix was in 1975…on a PDP-11/40. This was Unix 6th Edition (no virtual memory)…shipped on a DECtape directly from Bell Labs in NJ (with the source code). Like most college kids in the (then, relatively new) discipline of computer science, I was captivated by it. It was small and simple (compared to OS/360)…focused on getting stuff done…not sales of large-scale heat generators.

After I got out of school, I had to go to work in proprietary systems for about 10 years -- mostly instrument controllers & large-scale test/automation systems for big aerospace companies. HP had a burgeoning business in selling/integrating/testing/certifying/supporting these things. The average price for one was $2-10million…labor included (really big bux in 1979!).

I got back to Unix in 1984…I got picked for the project because I had "college experience" with it (also with C programming)…light-years more advanced experience than my boss -- I could write shell scripts…using pipes!

I had a brief detour into Windoze in the early '00s. My colleagues & I couldn't believe what unbelievable crap it was (this was Windoze NT4/2000…the last "good" versions…BSoDs happening only once per day instead of multiple times per day with NT). The kernel was designed well (by, basically, non-Micro$ofties who knew what they were doing…basically a bunch of DEC refugees led by Dave Cutler) but everything around it was absolute junk (networking, in particular, was a complete abortion). Even the compilers (essentially application software) could cause BSoDs! All the Unix guys hated it…COM/OLE especially (all the MPE guys thought it was the greatest thing since the steam engine -- MPE was an HP proprietary OS for the HP3000…a department/small-enterprise timesharing system with heavy batch influences)… The first thing we installed on every new Windoze machine we got was the MKS Toolkit.

I gave that up pretty quickly. I started using the early versions of SuSE in about 2003. I haven't even owned a Windoze machine since 2005…macOS is Berkeley Unix in drag.

-- sw

That is quite a history. I will never even begin to know what you know about these machines !! :-)

I know what you mean about windows networking. It wasted hours and hours of my life, and all I ever had to connect was a laptop and a desktop. Sometimes it would work fine, and other times it would not. This seemed to happen at random - and I should add that I DID know pretty well how to set up windows networking. Now I use nfs on linux and it's rock solid - connects first time every time.

Windoze NT3.1 & early versions of NT4 didn't support TCP/IP…so they wouldn't talk to all our Unix workstations. We had to build these really klugey LANManager gateways using spare Unix machines to do this. By Windoze 2K, TCP/IP support was there (Bill G thought "the internet" was a passing fad…so added it grudgingly) but it was dog slow and really poor quality (BSoDs were common with things like ftp). We still had to use the LANManager gateways because there was no NFS support (Windoze 2K <-> LANManager gateway (Unix) <-> NFS server). Later, we replaced this with a Company-licensed, 3rd party NFS implementation for Win2K.

DNS remained problematic until WinXP came out in 2001.

I need a shower just thinking about all this…

-- sw


Please enter an answer

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct