我先關閉介面
# 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 的遷移不會那麼容易。
答案1
你的三個問題都源自於一個誤解:
ifconfig
帶有接口和地址意味著up
選項,因此在您的ifconfig
範例中,介面將在您為其指定位址的同時出現。
這意味著
ifconfig enp2s0f0 10.0.0.1/24
ifconfig enp2s0f1 10.0.1.1/24
其實是縮寫
ifconfig enp2s0f0 10.0.0.1/24 up
ifconfig enp2s0f1 10.0.1.1/24 up
ip
從ip路由2更清晰地組織以下行為:
- 帶來一個介面向上和向下 (
ip link
), - 分配IP地址到網路設備 (
ip address
),以及 - 配置內核路由表 (
ip route
)。
ifconfig
將這些不同的概念混合在一起,這可能更令人困惑或不太直觀。
如果您想要一種方便的方式來管理路由、網路設備、介面和隧道的狀態,您應該考慮使用網路設定管理工具。 它們允許您以一種方便的聲明性語法定義您的網絡,您可以使用一個命令快速啟動或關閉該網絡。
以下是常見 Linux 發行版中包含的一些工具:
- Debian –如果向上向下(
/etc/network/interfaces
,/etc/network/interfaces.d/
) - 烏班圖——網路計劃(
/etc/netplan/
) - 紅帽企業 Linux / CentOS –IFCFG(
/etc/sysconfig/network-scripts/
) - 通常帶有桌面環境的發行版 –網路管理器(
/etc/NetworkManager/
)
為了完整起見,以下是您問題的答案:
- 為什麼不
ip route
顯示我的路線?
如果相應的連結已關閉,則路由不會儲存到主表中。您的ip
命令序列不會先開啟所涉及的介面。
- 當我啟動介面後,我的路由奇蹟般地添加到了哪裡?為什麼我以前看不到他們?
如果介面關閉,設備的路由不會在路由表中註冊。
- 為什麼在我啟動介面後我的路由不顯示?
沒有新增路由,因為對應的介面必須先啟動。您應該已經看到一條錯誤訊息,例如RTNETLINK answers: Network is down
或Error: Device for nexthop is not up.
如果您在關閉的介面上嘗試新增路由。返回碼 ( $?
) 也應該非零。
我想在啟動介面之前設定所有路由。
據我所知,即使使用ifconfig
.請記住,您的ifconfig
命令實際上是在新增路由之前啟動介面。
不知道接下來還會有什麼驚喜呢。
希望沒有。 ip
關注點分離很有意義,如果你了解它。
我認為
ifconfig
和ip
相容
這兩個指令有一些重疊,但不能互換。