how to save static routes permanently in ubuntu

The current configuration:

server1:    
sudo route add -host 10.0.1.2 dev enp131s0f0
sudo route add -host 10.0.1.3 dev enp131s0f1

server2:    
sudo route add -host 10.0.1.1 dev enp131s0f0
sudo route add -host 10.0.1.3 dev enp131s0f1

server3:    
sudo route add -host 10.0.1.1 dev enp131s0f0
sudo route add -host 10.0.1.2 dev enp131s0f1

This configuration on both sides will be lost if any server is rebooted or the cable is unplugged and replugged.

Saving them in /etc/rc.local does not work for the above situations.

So, how to save them permanently for both netplan and NetworkManager. I have both Ubuntu desktop and Ubuntu server installed.


Solution 1:

I got the solution after some research. Just post here to share.

  • for netplan, edit each interface like this:

    enp131s0f0:
      addresses:
      - 10.0.1.3/24
      routes:
      - to: 10.0.1.4
        via: 10.0.1.3
    
  • for NetworkManager:

configure each interface like this enter image description here

or edit configuration file in /etc/NetworkManager/system-connections/ accordingly like this.

[ipv4]
address1=10.0.1.4/24
dns-search=
method=manual
route1=10.0.1.3/32,10.0.1.4