13
$ sudo docker run --rm ubuntu:14.04 route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.17.42.1     0.0.0.0         UG    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

Doesn't this mean that 127.0.0.0/8 is routed towards the gateway of 172.17.42.1 and not the loopback device ?

Balazs Rau
  • 153
  • 1
  • 7
  • I meant: Why isn't there a route for loopback in Ubuntu? – Balazs Rau Apr 17 '15 at 14:02
  • I suppose explicit routes for loopback network are from the past. Seeing your question I take a look to some of our servers and there are no routes on the routing table for loopback. That's also valid for Solaris 10, not for Solaris 8 (yeah, we still have some boxes with Solaris 8). – alphamikevictor Apr 17 '15 at 14:11

2 Answers2

26

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.

wurtel
  • 3,864
  • 12
  • 15
  • 1
    Just a concern about your wording: His question was actually about routing for local addresses (127.0.0.0/8). `table local` is the answer, but not the question. It's the answer because traffic to any 127.* address gets converted to having a source address of 127.0.0.1, and I think also is forced to use `dev lo`. – Peter Cordes Apr 17 '15 at 22:18
  • 1
    So the real answer to the question is "because loopback is special and doesn't need one", is the real answer. None of those routing table entries say anything about how to route to 127.1.1.1, for example, but it still works (as you can tell from ping times). Presumably the networking code special-cases local addresses for performance reasons, since `ip addr` doesn't show `lo` having more than just `127.0.0.1` as its own address, but it will actually receive traffic for any ip in 127.../8 – Peter Cordes Apr 17 '15 at 22:23
  • 4
    @PeterCordes Actually the line `local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1` covers the route to 127.1.1.1; it means that anything starting with 127. is local. – wurtel Apr 20 '15 at 07:08
  • Ah, ok I hadn't understood everything that a `local` entry in the `local` table implied. Thanks. – Peter Cordes Apr 20 '15 at 08:25
3

The route command was old since 10 years ago and you should go with the iproute2 packages.

When you're using ip route show the main table is displayed. To display the local table use ip route show table local.

Hope it helped.

Iulian
  • 428
  • 1
  • 3
  • 8