what is the unix equivalent to windows command route add

Posted on

Problem :

I need to run the command

route add -p mask

on a linux server. what is the unix equivalent?

Solution :

They are very similar.

To add a route for a network – in this example, mask

sudo route add -net gw


sudo route add -net netmask gw

To add a route for a host

sudo route add -host gw

If you run route add with no options it will give you the list of options that you can use. e.g., metrics, interface, and some others.

Also the man page will help.

Without an explanation of what that Windows command does, it’s hard to answer, but it looks like you should read the manual page for ip-route.

One way to make the route be persistent is to add it every time the machine is started. This has same result as -p flag on Windows.

This can be done using Cron:

@reboot /sbin/route add -net netmask gw 

dev eth0 can be used at the end of command if there is need to specify port to be used.

This works at least on Ubuntu 14.04 and probably on most modern implimentations of Cron.

To add the route every time the interface is brought up, and remove it every time it goes down, a suitable command can be added to the interface’s stanza in /etc/network/interfaces (for Debian). Here’s one of mine (this makes local broadcasts go to the local network rather than to the ISP on my NAT gateway):

iface eth1 inet static
      pre-up iptables-restore </etc/iptables.rules
      up route add eth1
      down route del

This can be easily adapted to your OS and needs.

Leave a Reply

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