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.