今天这篇给自己解释了一下为什么 Linux veth0/1 + bridge ping 不通。 原因都在 ip route show table local 输出里。直接上步骤:
[dahui@192 ~]$ route -v
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.1 0.0.0.0 UG 100 0 0 em1
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 em1
[dahui@192 ~]$
# 创建veth对。
sudo ip link add veth0 type veth peer name veth1
# 给veth设置IP地址
sudo ip addr add 192.168.2.10/24 dev veth0
sudo ip addr add 192.168.2.20/24 dev veth1
# 激活接口
sudo ip link set veth0 up
sudo ip link set veth1 up
# 直连路由被自动添加了路由表
[dahui@192 ~]$ route -v
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.1 0.0.0.0 UG 100 0 0 em1
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 em1
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 veth0
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 veth1
[dahui@192 ~]$
# 创建一个linux网桥 br0
sudo ip link add name br0 type bridge
sudo ip link set br0 up
# 把veth0接到br0,veth0有ip的情况下虽然能接收来自协议栈的数据但是reponse只能走br0了。
sudo ip link set dev veth0 master br0
# veth0 拥有IP已经没有意义,把这个IP给br0
sudo ip addr del 192.168.2.10/24 dev veth0
sudo ip addr add 192.168.2.10/24 dev br0
# 期待通过 br0接口ping veth2能成功。但是失败了。
[dahui@192 ~]$ ping -c 1 -I br0 192.168.2.20
PING 192.168.2.20 (192.168.2.20) from 192.168.2.10 br0: 56(84) bytes of data.
--- 192.168.2.20 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
[dahui@192 ~]$
下面是由系统维护的 local 路由表:
[dahui@192 ~]$ ip route show 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.1.0 dev em1 proto kernel scope link src 192.168.1.10
local 192.168.1.10 dev em1 proto kernel scope host src 192.168.1.10
broadcast 192.168.1.255 dev em1 proto kernel scope link src 192.168.1.10
broadcast 192.168.2.0 dev veth1 proto kernel scope link src 192.168.2.20
broadcast 192.168.2.0 dev br0 proto kernel scope link src 192.168.2.10
local 192.168.2.10 dev br0 proto kernel scope host src 192.168.2.10
local 192.168.2.20 dev veth1 proto kernel scope host src 192.168.2.20
broadcast 192.168.2.255 dev veth1 proto kernel scope link src 192.168.2.20
broadcast 192.168.2.255 dev br0 proto kernel scope link src 192.168.2.10
[dahui@192 ~]$
Reference: https://unix.stackexchange.com/questions/205708/linux-does-not-reply-to-arp-request-messages-if-requested-ip-address-is-associat