Adding persistent route on Ubuntu

I have to using VPN in my office for connecting to some services and add some routes to my Ubuntu routing table.

But the routes will be removed after reboot. So I have to add them manually again.

I googled that and find a solution for adding the routes when interface is coming up.

You need to create a script under “/etc/network/if-up.d/”, for creating the script, follow the below steps:

1. Create script file by using “cat” command or other text editors:

#cat > your-script-file

#!/bin/bash
route add -net x.x.x.x netmask x.x.x.x gw x.x.x.x

route …

route …

CTRL+D (For saving file)

  1. Make the script file as an executable script file:

#chmod +x your-script-file

Then restart networking service or unplug and plug your network and run “route -n”.

If the routes network are available, then the routes will be added to routing table.

Note: You script should be saved without “.sh”. I tested it on Ubuntu 14.04.

Run the below command for test:

run-parts –test /etc/network/if-up.d

Davoud Teimouri

Professional blogger, vExpert 2015/2016/2017/2018/2019/2020/2021/2022/2023, vExpert NSX, vExpert PRO, vExpert Security, vExpert EUC, VCA, MCITP. This blog is started with simple posts and now, it has large following readers.

Leave a Reply

Your email address will not be published. Required fields are marked *