4

The output of /sbin/route -n on my Linux box doesn't mention the 127.x.x.x address range, neither it mentions lo interface. Nevertheless ping localhost and ssh localhost are working fine.

How the computer knows the route to 127.0.0.1 if it's not in the routing table?

7
  • cat /etc/hosts
    – gogoud
    Jun 29, 2016 at 11:34
  • 1
    /etc/hosts defines mapping from symbolical host name (localhost in our case) to its IP address (127.0.0.1 in our case). Now if you read the question you'll see that your comment ain't relevant.
    – ilya
    Jun 29, 2016 at 11:36
  • possible duplicate of serverfault.com/questions/683538/…
    – gogoud
    Jun 29, 2016 at 11:39
  • Yes, you're right, it's basically the same question, answered.
    – ilya
    Jun 29, 2016 at 12:01
  • @gogoud & ilya, you can copy the answer here (just put a link at the top for where it came from) so that this question has an answer.
    – phemmer
    Jun 29, 2016 at 12:39

1 Answer 1

8

(Copied from https://serverfault.com/a/683542/68920)

The route command is deprecated, and should not be used anymore.

The new way is to use the iproute set of commands, which are all invoked with ip followed by an object. For example:

$ ip route show
default via 192.168.1.254 dev eth0 
192.168.0.0/23 dev eth0  proto kernel  scope link  src 192.168.1.27 

Now, I hear you say, this is basically the same info! Yes, but this isn't the whole story. Before the routing tables (yes, plural) comes the rule table:

$ ip rule show
    0:  from all lookup local 
32766:  from all lookup main 
32767:  from all lookup default 

The routing table we were looking at before is the main routing table. Your question concerns the local routing table, which contains all routes relating to local connections. This table can be shown as follows:

$ ip ro sh table local
broadcast 127.0.0.0 dev lo  proto kernel  scope link  src 127.0.0.1 
local 127.0.0.0/8 dev lo  proto kernel  scope host  src 127.0.0.1 
local 127.0.0.1 dev lo  proto kernel  scope host  src 127.0.0.1 
broadcast 127.255.255.255 dev lo  proto kernel  scope link  src 127.0.0.1 
broadcast 192.168.0.0 dev eth0  proto kernel  scope link  src 192.168.1.27 
local 192.168.1.27 dev eth0  proto kernel  scope host  src 192.168.1.27 
broadcast 192.168.1.255 dev eth0  proto kernel  scope link  src 192.168.1.27

(You can abbreviate ip options / parameters as long as they're still unique, hence ip ro sh is the same as ip route show.)

Here you can see the loopback routes.

You can do all sorts of wonderful things with this policy-based routing, I recommend you read Policy Routing with Linux by Matthew G. Marsh for all the info you'll ever need.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .