2

This question already has an answer here:

I'm learning about networking, at home I have two physical machines and a bunch of VMs that I use to test my applications each machine has a different hostname and I map them manually in each /etc/hosts files

I would like to know what's the difference between home IP adress (127.0.0.1) and a real IP address given by the network in /etc/hosts

for example

let's say my IP address is 192.168.2.20 and the name host is naruto and my /etc/hosts looks like this:

127.0.0.1       localhost
192.168.2.20    naruto
127.0.0.1       naruto

all lines point to the same machine I understand that the main difference is how programs connect to each of them two are using loopback device and the other one is using a nic. my question is should I have all these lines? or what lines should I have? what's the use of each of them?

I was reading this post but it didn't help, I got more confused

marked as duplicate by msp9011, Thomas, schily, Jesse_b, jimmij Aug 7 '18 at 19:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Question #1

I would like to know what's the difference between home IP adress (127.0.0.1) and a real IP address given by the network in /etc/hosts

2 key characteristics of 127.0.0.1:

  • It's not routable outside of your computer on the Internet
  • The IP address 127.0.0.1 is part of a block of IP addresses that are associated to this interface on your system.

For example, take a look at your loopback interface, lo:

$ ip a l lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever

The block of IPs is designed by this line:

    inet 127.0.0.1/8 scope host lo

The /8 in this notation means that 8 bits of the 32-bits being presented here are the network's address, the remaining bits (32-8 = 24) are for addressing whatever you want within this computer.

We can convince ourselves that this is a range and they all point back to ourselves by trying to ping a couple of them. Let's ping 127.0.0.1, 127.0.0.2, & 127.0.0.3:

$ ping -c2 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.029 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.055 ms

$ ping -c2 127.0.0.2
PING 127.0.0.2 (127.0.0.2) 56(84) bytes of data.
64 bytes from 127.0.0.2: icmp_seq=1 ttl=64 time=0.029 ms
64 bytes from 127.0.0.2: icmp_seq=2 ttl=64 time=0.052 ms

$ ping -c2 127.0.0.3
PING 127.0.0.3 (127.0.0.3) 56(84) bytes of data.
64 bytes from 127.0.0.3: icmp_seq=1 ttl=64 time=0.030 ms
64 bytes from 127.0.0.3: icmp_seq=2 ttl=64 time=0.075 ms

NOTE: We can see that all these were pigable back to ourselves through our loopback interface.

Using traceroute shows the same thing:

$ traceroute -n 127.0.0.1
traceroute to 127.0.0.1 (127.0.0.1), 30 hops max, 60 byte packets
 1  127.0.0.1  0.032 ms  0.041 ms  0.010 ms

$ traceroute -n 127.0.0.2
traceroute to 127.0.0.2 (127.0.0.2), 30 hops max, 60 byte packets
 1  127.0.0.2  0.033 ms  0.009 ms  0.008 ms

$ traceroute -n 127.0.0.3
traceroute to 127.0.0.3 (127.0.0.3), 30 hops max, 60 byte packets
 1  127.0.0.3  0.034 ms  0.010 ms  0.008 ms

Question #2

my question is should I have all these lines? or what lines should I have? what's the use of each of them?

My recommendation would be to not assign any names to 127.0.0.1 except for whatever the system automatically assigned to it. Typically you'll see these types of entries in /etc/hosts:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

If I want to assign additional localhost type IPs for my system's hostname, then I'd use 127.0.0.2 instead, leaving 127.0.0.1 as it was setup by default.

Further still, for actual IP addresses that are assigned to my host, I'd either assign them like so in /etc/hosts or use DNS:

192.168.2.20    naruto.mydom.com naurto

But I would never assign the same name to 2 separate lines. This will never work, since the /etc/hosts file will only respond with the 1st entry, and the 2nd can never be reached.

For one off type work, using /etc/hosts is easy for local work. But if you expect any of the name to IP mappings to be accessible on your network, it's better to use DNS for those and forgo using /etc/hosts for anything but local IP/name resolution.

  • 1
    If you give a name to 127.0.0.2, then you have to have the services listen on 127.0.0.2; each address is different. This can be useful. E.g. I have several development servers all listening on port 80, all on different 127.?.?.? addresses. And different names in /etc/hosts. – ctrl-alt-delor Aug 6 '18 at 9:46
  • @ctrl-alt-delor - the HAProxys that power all of the SE sites use multiple 127.0.0.* addresses to get at additional TCP ports, b/c each TCP/IP is limited to 65k, by utilizing more localhost ports, you get another 65k per. – slm Aug 6 '18 at 11:35
1

The one thing I might do, and distros started doing it a few years back, is to use 127.0.0.1 only for localhost and use 127.0.1.1 for your hostname

So...

In /etc/hostname -

naruto

In /etc/hosts -

127.0.0.1 localhost
127.0.1.1 naruto

And that is it. (Add IPv6 stuff if needed) Only time I put a non-loopback IP into /etc/hosts is when I can't get a DNS entry for some reason, or if I want to redirect what would normally be found in DNS.

Not the answer you're looking for? Browse other questions tagged or ask your own question.