veth-pair 是 Linux 的虚拟网络设备,成对出现。是了解、学习 Docker/K8S 网络的基础
最近对着教程做练习,发现 veth-pair 的网卡,ping 另外一张 ping 不通
参考了 http://www.muzhuangnet.com/show/81813.html 中的方法,终于解决了问题
记录一二
sudo ip link add veth0 type veth peer name veth1
sudo ip addr add 1.2.3.101/24 dev veth0
sudo ip addr add 1.2.3.102/24 dev veth1
sudo ip link set veth0 up
sudo ip link set veth1 up
ip link
fananchong@vm-ubuntu:~/test$ ping -c 1 -I veth0 1.2.3.102
PING 1.2.3.102 (1.2.3.102) from 1.2.3.101 veth0: 56(84) bytes of data.
From 1.2.3.101 icmp_seq=1 Destination Host Unreachable
--- 1.2.3.102 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms
提示Destination Host Unreachable
fananchong@vm-ubuntu:~/test$ sudo tcpdump -n -i veth1
[sudo] password for fananchong:
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on veth1, link-type EN10MB (Ethernet), snapshot length 262144 bytes
10:06:53.214350 ARP, Request who-has 1.2.3.102 tell 1.2.3.101, length 28
10:06:54.227995 ARP, Request who-has 1.2.3.102 tell 1.2.3.101, length 28
10:06:55.251999 ARP, Request who-has 1.2.3.102 tell 1.2.3.101, length 28
veth1 收到 veth0 的 ARP 协议请求了
但是没响应回包
按 http://www.muzhuangnet.com/show/81813.html 中的方法:
echo 1 > /proc/sys/net/ipv4/conf/veth1/accept_local
echo 1 > /proc/sys/net/ipv4/conf/veth0/accept_local
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth0/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth1/rp_filter
fananchong@vm-ubuntu:~/test$ ping -c 1 -I veth0 1.2.3.102
PING 1.2.3.102 (1.2.3.102) from 1.2.3.101 veth0: 56(84) bytes of data.
64 bytes from 1.2.3.102: icmp_seq=1 ttl=64 time=0.064 ms
--- 1.2.3.102 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.064/0.064/0.064/0.000 ms
fananchong@vm-ubuntu:~/test$ sudo tcpdump -n -i veth1
[sudo] password for fananchong:
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on veth1, link-type EN10MB (Ethernet), snapshot length 262144 bytes
10:49:44.381089 IP 1.2.3.101 > 1.2.3.102: ICMP echo request, id 6, seq 1, length 64
10:49:49.492037 ARP, Request who-has 1.2.3.102 tell 1.2.3.101, length 28
10:49:49.492083 ARP, Reply 1.2.3.102 is-at 36:6a:84:b3:b6:f4, length 28