Iproute

路由表和(iproute2 vs net-tools)

  • December 23, 2020

我有以下路由表:

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.42.129  0.0.0.0         UG    0      0        0 enx6a58445c5d43
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enx6a58445c5d43
192.168.42.0    0.0.0.0         255.255.255.0   U     0      0        0 enx6a58445c5d43
192.168.57.0    0.0.0.0         255.255.255.0   U     0      0        0 vboxnet1

當我使用以下命令時出現錯誤:

# ip route add 192.168.42.0/24 via  192.168.57.1 dev vboxnet1
RTNETLINK answers: File exists

但是當我從 net-tools 使用時,我沒有收到任何錯誤:

# route add -net 192.168.42.0/24 gw   192.168.57.1 dev vboxnet1
# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.42.129  0.0.0.0         UG    0      0        0 enx6a58445c5d43
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enx6a58445c5d43
192.168.42.0    192.168.57.1    255.255.255.0   UG    0      0        0 vboxnet1 ####add here
192.168.42.0    0.0.0.0         255.255.255.0   U     0      0        0 enx6a58445c5d43
192.168.57.0    0.0.0.0         255.255.255.0   U     0      0        0 vboxnet1

問題是:如何通過 iproute2 添加?

您的路由表中有網路地址 192.168.42.0/24。所以你必須:

ip route replace 192.168.42.0/24 via 192.168.57.1 dev vboxnet1 

或者

ip route del 192.168.42.0/24 
ip route add 192.168.42.0/24 via 192.168.57.1 dev vboxnet1 

引用自:https://unix.stackexchange.com/questions/237035