# Multiple IP Addresses For Same Host in /etc/hosts



## fred974 (Dec 11, 2013)

Hello everyone,

I have just added two domains to my web server and I just wondered if I need/must add the two new domains to the /etc/hosts file? *A*t the moment I have got:

```
$More /etc/hosts
::1                     localhost localhost.curentwebsite.com
127.0.0.1               localhost localhost.curentwebsite.com
192.168.0.150           marvin.curentwebsite.com marvin
192.168.0.150           marvin.curentwebsite.com.
```

Should I replicate the block so it reads:

```
::1                     localhost localhost.curentwebsite.com
127.0.0.1               localhost localhost.curentwebsite.com
192.168.0.150           marvin.curentwebsite.com marvin
192.168.0.150           marvin.curentwebsite.com.

::1                     localhost localhost.website2.com
127.0.0.1               localhost localhost.website2.com
192.168.0.150           marvin.website2.com marvin
192.168.0.150           marvin.website2.com.

::1                     localhost localhost.website3.com
127.0.0.1               localhost localhost.website3.com
192.168.0.150           marvin.website3.com marvin
192.168.0.150           marvin.website3.com.
```

Thank you all in advance.

Fred


----------



## ShelLuser (Dec 11, 2013)

That is not required. Your second example would even be completely useless because only one entry would be used. First thing to keep in mind here is the true function of the hosts(5) file: "_The hosts file contains information regarding the known hosts on the network._".

So basically all it does is act as a local "DNS database" which can be used to easily translate a name into an IP number. So in your current situation it basically means that if you use the command `ping marvin` then your system will know that it should try to access 192.168.0.150. Next to that it also knows that marvin.curentwebsite.com should resolve to the same IP address.

But these entries are basically useless if the domain also exists outside the host (so basically if its available on other DNS servers). Apart from the fact that using the alias marvin is of course a lot easier than having to type out the entire server name (name and domain).

In general the hosts file is used to either quickly access certain servers using an alias or to override certain names (which could be useful for testing purposes or to force your system to use a local IP address instead of a public one).

Keep in mind that those aliases aren't always necessary. If you check /etc/resolv.conf (see also the resolv.conf(5) manualpage) then you can tell it to search specific domains (using the search keyword). The handy part here is that this means that you only need to supply the server name after which your system will try to fill in the domain name.

Put differently: If I were to add search website3.com to /etc/resolv.conf on my system and website3.com would be an existing domain, then all I'd need to access it is using the name marvin. Because of the search keyword in /etc/resolv.conf the domain name is looked up automatically.

So summing up: it's not required, and your current setup can be optimized a bit more by removing the double entry for 192.168.0.50.


----------



## fred974 (Dec 11, 2013)

Hi @ShelLuser,

Thank you for the clarification on /etc/hosts.
That said, would you add the domaine for wesite 2 and website 3 to the /etc/resolv.conf?

I started looking into adding my domain to the box when I realised that my SSL certificate wasn't working correctly.



> 2013/12/11 20:07:10 [info] 1899#0: *63 peer closed connection in SSL handshake while SSL handshaking, client: 82.31.44.171, server: 0.0.0.0:443


----------



## fred974 (Dec 11, 2013)

This is what *I* have at the moment:


```
$ more /etc/resolv.conf
domain  curentwebsite.com
nameserver      194.168.4.100
```

Question: is the nameserver suppose_d_ to be the same as the box's static IP?


----------



## ShelLuser (Dec 12, 2013)

fred974 said:
			
		

> That said, would you add the domaine wor wesite 2 and website 3 to the /etc/resolv.conf?


That would heavily depend on how often you'd use these. I most certainly wouldn't add them for regular domains (so, for example, domains which are only used for some regular websites).

But for anything else most definitely.

For example, I have a private and business domain which I heavily use to access home and company environments, therefore I added both to resolv.conf. But that's about it.


----------



## ShelLuser (Dec 14, 2013)

fred974 said:
			
		

> Question: is the nameserver suppose_d_ to be the same as the box's static IP?


Well, yes and no (my favourite answer ).

A Unix environment is so flexible and extensive that most often there isn't a "best" way to do things; there are simply too many roads leading to Rome to determine which one is the best.

If you run and maintain your own DNS server then using the servers own IP address most certainly won't hurt. However, in this scenario all your data is still going across (semi) public channels. You could opt to replace the public IP address for that of the localhost (so 127.0.0.1). This will make sure that the server is only going to use its own DNS server to resolve any names.


----------



## fred974 (Dec 15, 2013)

Thank you @ShelLuser*.*


----------

