无法添加路由 SIOCADDRT: 网络不可达

无法添加路由 SIOCADDRT: 网络不可达

嗨,我想添加一条路由,通过主机 10.251.10.127 向 docker 容器网络 (10.20.0.0/16) 添加路由。在添加路由之前,我 ping 了主机 10.251.10.127,它是可访问的。但后来我

sudo route add -net 10.20.0.0/16 gw 10.251.10.127

我得到了

SIOCADDRT: Network is unreachable

这是我的配置

ifconfig 
docker0   Link encap:Ethernet  HWaddr 02:42:48:4c:40:16  
          inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth0      Link encap:Ethernet  HWaddr ec:b1:d7:56:9d:88  
          inet addr:192.168.108.155  Bcast:192.168.109.255  Mask:255.255.254.0
          inet6 addr: fe80::f784:a7df:5e4e:ce2f/64 Scope:Link
          inet6 addr: 2620:2c:40c0:c00:afef:c210:1878:1e12/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23681 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10525 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:15916466 (15.9 MB)  TX bytes:1961427 (1.9 MB)
          Interrupt:20 Memory:f7d00000-f7d20000 

eth1      Link encap:Ethernet  HWaddr 68:05:ca:3e:0c:f3  
          inet addr:10.250.1.46  Bcast:10.250.255.255  Mask:255.255.0.0
          inet6 addr: fe80::8f37:3338:32cf:ba39/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:18558 errors:0 dropped:0 overruns:0 frame:0
          TX packets:289 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:12158842 (12.1 MB)  TX bytes:29883 (29.8 KB)
          Interrupt:19 Memory:f7cc0000-f7ce0000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:1973 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1973 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:193934 (193.9 KB)  TX bytes:193934 (193.9 KB)

这是我的路线

 route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.108.1   0.0.0.0         UG    100    0        0 eth0
0.0.0.0         10.250.0.1      0.0.0.0         UG    101    0        0 eth1
10.250.0.0      0.0.0.0         255.255.0.0     U     100    0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.108.0   0.0.0.0         255.255.254.0   U     100    0        0 eth0

还有几张桌子

ip route 
default via 192.168.108.1 dev eth0  proto static  metric 100 
default via 10.250.0.1 dev eth1  proto static  metric 101 
10.250.0.0/16 dev eth1  proto kernel  scope link  src 10.250.1.46  metric 100 
169.254.0.0/16 dev eth0  scope link  metric 1000 
172.17.0.0/16 dev docker0  proto kernel  scope link  src 172.17.0.1 linkdown 
192.168.108.0/23 dev eth0  proto kernel  scope link  src 192.168.108.155  metric 100 

ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether ec:b1:d7:56:9d:88 brd ff:ff:ff:ff:ff:ff
    inet 192.168.108.155/23 brd 192.168.109.255 scope global dynamic eth0
       valid_lft 6539sec preferred_lft 6539sec
    inet6 2620:2c:40c0:c00:afef:c210:1878:1e12/64 scope global noprefixroute dynamic 
       valid_lft 86395sec preferred_lft 14395sec
    inet6 fe80::f784:a7df:5e4e:ce2f/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 68:05:ca:3e:0c:f3 brd ff:ff:ff:ff:ff:ff
    inet 10.250.1.46/16 brd 10.250.255.255 scope global dynamic eth1
       valid_lft 6540sec preferred_lft 6540sec
    inet6 fe80::8f37:3338:32cf:ba39/64 scope link 
       valid_lft forever preferred_lft forever
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:48:4c:40:16 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever

答案1

网关地址必须在本地连接的网络上可达,静态路由才可用。10.251.xx 不是 10.250.0.0/16 本地的

尝试这样的操作:

假设 10.251.10.127 可通过 10.250.1.46 接口访问,

ip route add -host 10.251.10.127 dev eth1
ip route add 10.20.0.0/16 via 10.251.10.127

相关内容