Why isn't there a route for localhost in Ubuntu?
Solution 1:
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.
Solution 2:
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.