4

I have a script (bottom of question) which adds a dummy link, adds some addresses to that link, and adds some routes to that link.

How can I make ip link, ip addr, and ip route commands persist?

I'm using Debian stretch, NetworkManager is installed.

Thought 1:

If I put this file in /etc/network/if-up.d/ it might be called when the machine boots. /etc/NetworkManager/dispatcher.d/01ifupdown seems to point to this directory.

Update: I tried, it wasn't a good idea

Thought 2:

I might need to translate this into a different configuration language and put it in /etc/network/interfaces.d/, but the examples I've seen so far for configuring this file assumes that devices exist. I haven't found a configuration line which is equivalent to ip link add mocknet type dummy.

This file relies on the auto keyword at the start of lines to identify physical interfaces (auto mocknet), but mocknet doesn't exist yet.

Working script

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
  • Thought 3, if you have systemd running: Translate it into a different language, and use /etc/systemd/network/ configuration files.
    – dirkt
    Commented Oct 21, 2018 at 9:46
  • What would an equivalent configuration look like in /etc/systemd/network?
    – Stewart
    Commented Oct 22, 2018 at 10:18

1 Answer 1

2

Thought 2 is correct. Also, it isn't that tough to translate from this simple script into a correct file. A nice surprise here is that ip link set mocknet up is automatic. man interfaces gives some good information. Here's the working file:

/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

I'm not sure whether /bin/ip or /sbin/ip is the "right" way to do this, but everything seems to work.

1
  • From the looks of the documentation (try man interfaces, man ip), the configuration file format is not 1-to-1 with the ip command features. For example, on a Debian 10.9 system, it's not clear if the ip command's type setting matches any config file options. In other words, it looks like using the ip commands is the current "right" way. Commented Apr 10, 2021 at 7:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .