我首先关闭接口
# Shutdown the interfaces
ip link set enp2s0f0 down
ip link set enp2s0f1 down
使用ifconfig
:
# Set subnets to interfaces
ifconfig enp2s0f0 10.0.0.1/24
ifconfig enp2s0f1 10.0.1.1/24
# Add two routes
ip route add 10.11.1.1 dev enp2s0f0
ip route add 10.11.0.1 dev enp2s0f1
# Result => NO PROBLEM, my routes are here even tho the interfaces are down
$ip route
10.0.0.0/24 dev enp2s0f0 proto kernel scope link src 10.0.0.1
10.0.1.0/24 dev enp2s0f1 proto kernel scope link src 10.0.1.1
10.11.0.1 dev enp2s0f1 scope link
10.11.1.1 dev enp2s0f0 scope link
使用ip
:
# Set subnets to interfaces
ip addr add 10.0.0.1/24 dev enp2s0f0
ip addr add 10.0.1.1/24 dev enp2s0f1
$ip route
#NOTHING => Where are the routes?!
# Add two routes
ip route add 10.11.1.1 dev enp2s0f0
ip route add 10.11.0.1 dev enp2s0f1
$ip route
#NOTHING => Where are my routes?!
ip link set enp2s0f0 up
ip link set enp2s0f1 up
# Result => WOW only two routes appeared miraculously
$ip route
10.0.0.0/24 dev enp2s0f0 proto kernel scope link src 10.0.0.1
10.0.1.0/24 dev enp2s0f1 proto kernel scope link src 10.0.1.1
# => Where are my 10.11.*.* routes?
- 为什么不
ip route
显示我的路线? - 启动接口后,我的路由奇迹般地添加到了哪里?为什么我以前看不到他们?
- 为什么我打开接口后我的路由不显示?
我想在启动接口之前设置所有路由。我这样做没有任何问题ifconfig
。不知道接下来会有什么惊喜。
我认为ifconfig
并且ip
兼容,从 ifconfig 转移到 ip 不会那么容易。
您的所有三个问题都源于一个误解:
ifconfig
带有接口和地址的选项意味着up
选项,因此在您的ifconfig
示例中,接口在您为其指定地址的同时出现。这表示
实际上是缩写
ip
从iproute2更干净地组织以下行为:ip link
),ip address
),以及ip route
)。ifconfig
将这些不同的概念混合在一起,可以说这更令人困惑或更不直观。如果您想要一种方便的方式来管理路由、网络设备、接口和隧道的状态,您应该考虑使用网络配置管理工具。 它们允许您以方便的声明性语法定义您的网络,您可以使用一个命令快速启动或关闭它。
以下是常见 Linux 发行版中包含的一些工具:
/etc/network/interfaces
,/etc/network/interfaces.d/
)/etc/netplan/
)/etc/sysconfig/network-scripts/
)/etc/NetworkManager/
)为了完整起见,以下是您的问题的答案:
如果相应的链接断开,路由不会保存到主表中。您的
ip
命令序列不会首先启动相关接口。如果接口关闭,则设备的路由不会在路由表中注册。
未添加路由,因为必须先启动相应的接口。您应该已经看到一条错误消息,例如
RTNETLINK answers: Network is down
您Error: Device for nexthop is not up.
的路由添加是在关闭接口上尝试的。返回码 ($?
) 也应该是非零的。据我所知,即使使用
ifconfig
. 请记住,您的ifconfig
命令实际上是在添加路由之前启动接口。希望没有。 如果您了解它
ip
,关注点分离就很有意义。这两个命令有一些重叠,但它们不可互换。