大网数据广告活动
服务器运维问答

What is the local routing table used for?

· 52CE 我爱测

I am trying to understand what is the local routing table. If I add a random address in it with the following command: sudo ip route add to local <any-ip-address> dev <network interface> Now I can ping this address, but no interf…

问题原文

原帖链接 · Lilás · CC BY-SA 3.0

I am trying to understand what is the local routing table.

If I add a random address in it with the following command:

sudo ip route add to local <any-ip-address> dev <network interface>

Now I can ping this address, but no interface listed by ifconfig uses this address.

Example:

$ ping 192.168.22.22 -w 1
PING 192.168.22.22 (192.168.22.22) 56(84) bytes of data.

--- 192.168.22.22 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms

$ sudo ip route add to local 192.168.22.22 dev wlp2s0
$ ping 192.168.22.22 -w 1
PING 192.168.22.22 (192.168.22.22) 56(84) bytes of data.
64 bytes from 192.168.22.22: icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from 192.168.22.22: icmp_seq=2 ttl=64 time=0.015 ms

--- 192.168.22.22 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.015/0.016/0.018/0.004 ms

It feels that it works as a loopback, but from ifconfig, the lo interface still has only the 127.0.0.1 address. If I check this table ip route ls table local I see the following:

$ ip route ls 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 wlp2s0  proto kernel  scope link  src 192.168.0.13
local 192.168.0.13 dev wlp2s0  proto kernel  scope host  src 192.168.0.13
broadcast 192.168.0.255 dev wlp2s0  proto kernel  scope link  src 192.168.0.13
local 192.168.22.22 dev wlp2s0  scope host
broadcast 192.168.122.0 dev virbr0  proto kernel  scope link  src 192.168.122.1
local 192.168.122.1 dev virbr0  proto kernel  scope host  src 192.168.122.1
broadcast 192.168.122.255 dev virbr0  proto kernel  scope link  src 192.168.122.1

So what is the local table and in witch scenario should I use it?

I read somewhere that it is for broadcast and addresses hosted by the local machine but I don't understand why do we need a special table for it and when should I add any entry to this table.

Thanks

社区高分解答

原帖链接 · Wesley · CC BY-SA 4.0

poige's answer covers that the local routing table is 1) special, 2)highest priority and 3) is maintained by the kernel, but it doesn't answer the question "What is the local routing table used for?", which I will try to explain.

Local loopbacks

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

These routes specify that any address in the 127.*.*.* space will point back to the machine. The 127.*.*.* space is specially designated in the IETF spec to always be loopback addresses. To understand why 127.0.0.0/8 maps to 127.*.*.*, you have to understand CIDR notation if you are not already familiar with it.

127.0.0.0/8 - This block is assigned for use as the Internet host loopback address. A datagram sent by a higher-level protocol to an
address anywhere within this block loops back inside the host. This
is ordinarily implemented using only 127.0.0.1/32 for loopback. As
described in [RFC1122], Section 3.2.1.3, addresses within the entire 127.0.0.0/8 block do not legitimately appear on any network anywhere.

Broadcasts

broadcast 127.0.0.0 dev lo  proto kernel  scope link  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 wlp2s0  proto kernel  scope link  src 192.168.0.13
broadcast 192.168.0.255 dev wlp2s0  proto kernel  scope link  src 192.168.0.13
broadcast 192.168.122.0 dev virbr0  proto kernel  scope link  src 192.168.122.1
broadcast 192.168.122.255 dev virbr0  proto kernel  scope link  src 192.168.122.1

Typically, a packet is sent to a single destination IP. However, there are times where it is useful to send the same packet to a block of IPs of a subnet (for example, your computer could send a special packet to all the addresses within your router subnet to discover network printers, which respond to the special packet). A broadcast address is a special address that functions like sending to all addresses in the subnet. Conventionally, these are usually the first and last address in the subnet.

Local routes in different subnets

local 192.168.0.13 dev wlp2s0  proto kernel  scope host  src 192.168.0.13
local 192.168.122.1 dev virbr0  proto kernel  scope host  src 192.168.122.1

Lastly, the remaining addresses are the machine's address on each of these subnets. For example, a device (192.168.0.14) on the 192.168.0.0/24 subnet would reach your machine by sending packets to 192.168.0.13.

User defined

local 192.168.22.22 dev wlp2s0  scope host

This is the last route that's not accounted for, which I assume was added manually by you.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。