Iproute
如何使 ip 命令持久化
我有一個腳本(問題的底部),它添加了一個虛擬連結,向該連結添加了一些地址,並向該連結添加了一些路由。
我怎樣才能使
ip link
,ip addr
和ip route
命令持續存在?我正在使用 Debian stretch,安裝了 NetworkManager。
想法1:
如果我把這個文件放進去,
/etc/network/if-up.d/
它可能會在機器啟動時被呼叫。/etc/NetworkManager/dispatcher.d/01ifupdown
似乎指向這個目錄。**更新:**我試過了,這不是一個好主意
想法2:
我可能需要將其翻譯成不同的配置語言並將其放入 中
/etc/network/interfaces.d/
,但到目前為止我看到的配置此文件的範例假定設備存在。我還沒有找到等效於ip link add mocknet type dummy
.該文件依賴於行首的
auto
關鍵字來辨識物理介面 (auto mocknet
),但mocknet
尚不存在。工作腳本
ip link add mocknet type dummy ip addr add 10.10.1.1/24 dev mocknet ip addr add 10.10.2.1/24 dev mocknet ip addr add 10.10.3.1/24 dev mocknet ip addr add 10.10.4.1/24 dev mocknet ip addr add 10.10.5.1/24 dev mocknet ip addr add 10.10.6.1/24 dev mocknet ip addr add 10.10.7.1/24 dev mocknet ip addr add 10.10.8.1/24 dev mocknet ip addr add 10.10.9.1/24 dev mocknet ip link set mocknet up ip route del 10.10.1.0/24 ip route del 10.10.2.0/24 ip route del 10.10.3.0/24 ip route del 10.10.4.0/24 ip route del 10.10.5.0/24 ip route del 10.10.6.0/24 ip route del 10.10.7.0/24 ip route del 10.10.8.0/24 ip route del 10.10.9.0/24 ip route add 10.10.1.0/24 dev mocknet proto kernel scope link src 10.10.1.1 metric 6000 ip route add 10.10.2.0/24 dev mocknet proto kernel scope link src 10.10.2.1 metric 6000 ip route add 10.10.3.0/24 dev mocknet proto kernel scope link src 10.10.3.1 metric 6000 ip route add 10.10.4.0/24 dev mocknet proto kernel scope link src 10.10.4.1 metric 6000 ip route add 10.10.5.0/24 dev mocknet proto kernel scope link src 10.10.5.1 metric 6000 ip route add 10.10.6.0/24 dev mocknet proto kernel scope link src 10.10.6.1 metric 6000 ip route add 10.10.7.0/24 dev mocknet proto kernel scope link src 10.10.7.1 metric 6000 ip route add 10.10.8.0/24 dev mocknet proto kernel scope link src 10.10.8.1 metric 6000 ip route add 10.10.9.0/24 dev mocknet proto kernel scope link src 10.10.9.1 metric 6000
想法2是正確的。此外,將這個簡單的腳本翻譯成正確的文件並不難。這裡的一個驚喜是它
ip link set mocknet up
是自動的。man interfaces
提供了一些很好的資訊。這是工作文件:
/etc/network/interfaces.d/mocknet
:# Mocknet: # A mock network meant to replace parts of the real one if they don't exist. # # We are creating a network sink called mocknet. It's a dummy, so data sent to # this is ignored. This is useful for when we want to run the simulation without # all other machines connected. If a machine doesn't exist, then this prevents # that packet from being sent to the default route and spamming the LAN. auto mocknet iface mocknet inet manual # # This creates the dummy interface. Dummy means that data sent here will be # ignored. pre-up /bin/ip link add mocknet type dummy # # Here we add all of the IP adresses that we might have as host on the FFS. # This guarantees that we can bind to these addresses for Rx and helps us make # a route for Tx up /bin/ip addr add 10.10.1.1/24 dev mocknet up /bin/ip addr add 10.10.2.1/24 dev mocknet up /bin/ip addr add 10.10.3.1/24 dev mocknet up /bin/ip addr add 10.10.4.1/24 dev mocknet up /bin/ip addr add 10.10.5.1/24 dev mocknet up /bin/ip addr add 10.10.6.1/24 dev mocknet up /bin/ip addr add 10.10.7.1/24 dev mocknet up /bin/ip addr add 10.10.8.1/24 dev mocknet up /bin/ip addr add 10.10.9.1/24 dev mocknet # # The previous line created some default routes. Those routes are super high # priority (metric=0) so they override real connections if they exist. We # don't want that so here we need to delete the default routes and then add # good versions. Now if a packet is sent to something in this ip range it goes # to mocknet. `metric 6000` sets the route priority super low (a bigger # number is lower priority so if it conflicts with a real device, that real # device will take priority and data will be sent and not ignored. post-up /bin/ip route del 10.10.1.0/24 && /bin/ip route add 10.10.1.0/24 dev mocknet proto kernel scope link src 10.10.1.1 metric 6000 post-up /bin/ip route del 10.10.2.0/24 && /bin/ip route add 10.10.2.0/24 dev mocknet proto kernel scope link src 10.10.2.1 metric 6000 post-up /bin/ip route del 10.10.3.0/24 && /bin/ip route add 10.10.3.0/24 dev mocknet proto kernel scope link src 10.10.3.1 metric 6000 post-up /bin/ip route del 10.10.4.0/24 && /bin/ip route add 10.10.4.0/24 dev mocknet proto kernel scope link src 10.10.4.1 metric 6000 post-up /bin/ip route del 10.10.5.0/24 && /bin/ip route add 10.10.5.0/24 dev mocknet proto kernel scope link src 10.10.5.1 metric 6000 post-up /bin/ip route del 10.10.6.0/24 && /bin/ip route add 10.10.6.0/24 dev mocknet proto kernel scope link src 10.10.6.1 metric 6000 post-up /bin/ip route del 10.10.7.0/24 && /bin/ip route add 10.10.7.0/24 dev mocknet proto kernel scope link src 10.10.7.1 metric 6000 post-up /bin/ip route del 10.10.8.0/24 && /bin/ip route add 10.10.8.0/24 dev mocknet proto kernel scope link src 10.10.8.1 metric 6000 post-up /bin/ip route del 10.10.9.0/24 && /bin/ip route add 10.10.9.0/24 dev mocknet proto kernel scope link src 10.10.9.1 metric 6000 # # This cleans up the interface so it doesn't fail if we try to # systemctl restart networking. post-down /bin/ip link del mocknet
我不確定這樣做是否
/bin/ip
是/sbin/ip
“正確”的方法,但一切似乎都有效。