5

Let's say I ran quite a few ip commands and ended up with a required network configuration, but I didn't save the command history.

Instead of rewriting commands in a file or script again, is there a way to dump/save the state of network configuration like iptables-save or mysqldump and so, later we can restore?

I see similar thing is possible with netnsh in windows (not sure if it's exactly the kind of solution I am looking for..havne't gone through it, but it seems like it dumps the network configuration state).

But I can't find any option in Linux (especially CentOS/RHEL)

2
  • Doesn't iptables-save work at all? Mar 15, 2019 at 13:07
  • iptables-save working for saving iptable rules. I am looking for a similar way to save network configuration. However, I can think of some reasons why it may not be possible to save network configuration entirely, as if a physical interface is missing at the time of restore, few configurations may fail.
    – GP92
    Mar 15, 2019 at 13:22

3 Answers 3

6

Some support does exist for saving addresses, routes and rules, using iproute2's ip command.

For obvious reason, this doesn't exist for links, even if one could imagine the possibility to save some virtual links, not all ("saving" a single side of a veth-pair link with its peer accross an other network namespace? not gonna happen...), or being able to save a bridge's and bridge's ports configurations including vlan etc., this doesn't appear to exist currently.

Existing commands are:

ip address save
ip address restore
ip route save
ip route restore
ip rule save
ip rule restore

The dump format is binary and the commands will refuse to save to or restore from a tty.

I suggest restoring addresses before routes (rules can be done at any order), or most saved routes won't be restored because they can't satisfy routing conditions depending on addresses. Warning: of course all flush commands below will likely disrupt network connectivity until the restore is done, so this should be avoided from remote network access (or be done in other network namespaces).

  • ip address save / ip address restore

    So to copy addresses from a simple network namespace orig's configuration having a dummy0 interface (to keep the example simple) to a namespace copy:

    ip netns add orig
    ip netns exec orig sh << 'EOF'
    ip link add dummy0 type dummy
    ip address add dev dummy0 192.0.2.2/24
    ip address add dev dummy0 2001:db8:0:1::2/64
    ip link set dummy0 up
    ip address save > /tmp/address
    EOF
    
    ip netns add copy
    ip netns exec copy sh << 'EOF'
    ip link add dummy0 type dummy
    ip link set dummy0 up
    ip address restore < /tmp/address
    ip -br address
    EOF
    

    will give for example this result:

    lo               DOWN           
    dummy0           UNKNOWN        192.0.2.2/24 2001:db8:0:1::2/64 fe80::68e3:bdff:feb0:6e85/64 fe80::e823:d1ff:fe8c:3a15/64 
    

    Note: that previous automatic IPv6 link-local (scope link) address was also saved, and is thus restored, leading to an additional (and wrong) IPv6 link-local address, because the link/ether address (here in orig 6a:e3:bd:b0:6e:85) on which is based the IPv6 link-local address is not saved and thus not restored (leaving here the in copy the other random MAC ea:23:d1:8c:3a:15 on dummy0). So care should actually be done to separately save and copy the MAC address of such virtual interfaces if it really matters, or prune after some addresses for physical interfaces.

    You should probably flush all addresses before restoring them to avoid leaving old ones if the environment wasn't a "clean slate". Contrary to routes below, I couldn't find a simple way to flush all of them in one command without having to state an interface. Using those two should be good enough:

    ip address flush permanent
    ip address flush temporary
    

On the same principle, routes and rules can be saved and restored:

  • ip route save / ip route restore

    There's a trick. ip route save will save only the main table, which is good for common use cases, but not with policy routing's additional routing tables. You can state a specific table (eg ip rule save table 220) if needed. But the special table 0 represents all tables, using ip route save table 0 will save all of them (including for each route the table it belongs to, like would be displayed with ip route show table 0) at once. Before restoring routes, it should be preferable to flush all existing routes:

    ip route flush table 0 all
    

    Example showing any routing table can be saved without having to know its value beforehand:

    # ip route add table 220 unreachable 10.0.0.0/8 metric 9999
    # ip route show table 220
    unreachable 10.0.0.0/8 metric 9999 
    # ip route save table 0 > /tmp/route
    # ip route flush table 0 all
    # ip route show table 220
    #
    # ip route restore table 0 < /tmp/route
    # ip route show table 220
    unreachable 10.0.0.0/8 metric 9999 
    

    Of course all routes from other tables, including table 254 aka main, are also saved and restored.

  • ip rule save / ip rule restore

    This one is also tricky because if not flushed before it will add duplicates without complaining, and flushing the rules never flushes rule prio 0, so rule priority 0 has to be explicitly deleted:

    ip rule flush
    ip rule delete priority 0
    

    So to save and restore:

    ip rule save > /tmp/rule
    

    [...] just deleting, or switching to some other environment etc.

    ip rule flush
    ip rule delete priority 0
    ip rule restore < /tmp/rule
    

I hope you can find some usage of this, for example for automatization with multiple network namespaces.

2
  • Thanks for a very detailed answer :) I actually wanted to restore the links too, as I did create few namespaces, bridges and veth links. But I have also realized with all the complexity, it's a good idea to reconfigure manually or with scripts than trying to restore.:)
    – GP92
    Mar 18, 2019 at 5:01
  • good stuff... call it covid-fog but I want to connect it to systemd. :(
    – Richard
    Mar 25, 2021 at 22:28
2

There is a script in this github repository. by this script you can save your current network configuration commands to a bash script.

./save_my_network.py

After executing this script you can find a network.save file in your current directory

cat network.save

#!/bin/bash
ip addr add 192.168.1.97/24 dev wlan0
echo 0x1003 > /sys/class/net/wlan0/flags
ip addr add 172.17.0.1/16 dev docker0
ip addr add 172.19.0.1/24 dev docker0
ip addr add 172.18.0.1/24 dev docker0
ip addr add 172.20.0.1/16 dev docker0
echo 0x1002 > /sys/class/net/docker0/flags
ip addr add 127.0.0.1/8 dev lo
echo 0x9 > /sys/class/net/lo/flags
ip addr add 10.8.0.15/24 dev tun0
echo 0x1091 > /sys/class/net/tun0/flags
ip route add 0.0.0.0/1 via 10.8.0.1 dev tun0 
ip route add default via 192.168.1.1 dev wlan0 proto dhcp metric 600 
ip route add 10.8.0.0/24 dev tun0 proto kernel scope link src 10.8.0.15 
ip route add 94.130.24.225 via 192.168.1.1 dev wlan0 
ip route add 128.0.0.0/1 via 10.8.0.1 dev tun0 
ip route add 192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.97 metric 600 

Don't forget execute permission for network.save

chmod +x network.save

0

Your network configuration should be saved in files such as /etc/sysconfig/network-scripts/ifconfig-(interface) and /etc/sysconfig/network, /etc/resolv.conf, etc. You should just modify these files and restart your network service rather then making your changes on the fly. Any changes you make on the fly, like route or IP changes, will be lost when your system restarts.

1
  • Hi.. yes, I know the use of the config files.but I'm checking if it's possible to generate config files or a restoration script automatically from the current state.. anyhow I think it's a dead end :|
    – GP92
    Mar 16, 2019 at 11:26

You must log in to answer this question.

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