Adding permanent routing

7,419

Solution 1

Add the route entry to your /etc/rc.local file (before exit 0):

/sbin/route add -net xxx.xxx.xxx.xxx netmask 255.255.240.0 gw xxx.xxx.xx.xxx

or add it to your crontab:

@reboot /sbin/route add -net xxx.xxx.xxx.xxx netmask 255.255.240.0 gw xxx.xxx.xx.xxx

Use the absolute path to route executable, on my system /sbin/route, change if needed.

As a side note, start using the ip command (ip route ....) for these trivial tasks now as the utilities of net-tools package are deprecated now.

Solution 2

To expand on heemayl's answer regarding the use of theip suite.

The correct command to put into /etc/rc.local is

/sbin/ip route add xxx.xxx.xxx.xxx/20 via yyy.yyy.yyy.yyy

where xxx.xxx.xxx.xxx/20 represents the network address and the netmask (24010=111100002), so 20 bit are set, that number is now called prefix, and yyy.yyy.yyy.yyy represents the gateway.

Your /etc/rc.local should look this way:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/sbin/ip route add xxx.xxx.xxx.xxx/20 via yyy.yyy.yyy.yyy
exit 0

It is important to leave exit 0 in place as the last line.

Share:
7,419

Related videos on Youtube

Garg
Author by

Garg

n/a

Updated on September 18, 2022

Comments

  • Garg
    Garg over 1 year

    Each time when I start my computer I type in the console

    sudo route add -net xxx.xxx.xxx.xxx netmask 255.255.240.0 gw xxx.xxx.xx.xxx
    

    When computer is rebooted or switched Off/On I must re-enter this in the terminal.

    Is there an option whit which I can add this route permanently? I'm using Ubuntu

  • Garg
    Garg almost 8 years
    Thank's a lot. It's working perfectly. With IP how will look this? Just IP instead ot route... ?
  • MAP
    MAP almost 8 years
    ip is a new command (in /sbin usually) which has all of the previously separate ip related commands (e.g. route) as subcommands. So this would be "ip route ..." to replace the direct call to route. In practical terms, add three characters "ip<space>" after the second slash.