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
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 “pingable” 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 naruto
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.
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-delorCommentedAug 6, 2018 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♦CommentedAug 6, 2018 at 11:35
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.
What I do not understand about this strategy is the fact (also pointed out earlier) that no services will generally be listening on that IP. Meaning that if I were to try, say, telnet naruto 25
, it will fail, whereas it would have worked if it had been mapped as 127.0.0.1 naruto
. So, what’s the point? Why did distros start doing this?
– cueedeeCommentedMay 30, 2023 at 12:26
@cueedee no idea, you’d have to ask them (distro maintiners) why. Also, remember that quite a few services bind to “all interfaces” (usually represented as 0.0.0.0:portnum in netstat) by default (apache, ssh, nginx, etc.) so yes, “ssh user@darkstar” would work if darkstar maps to 127.0.1.1 in /etc/hosts
– ivanivanCommentedMay 31, 2023 at 21:51
Granted, but not all services do this, which is also why I picked smtp
(port 25) as an example .
– cueedeeCommentedJun 2, 2023 at 7:14
Source from https://unix.stackexchange.com/questions/460722/how-to-configure-etc-hosts-properly