Link to home
Start Free TrialLog in
Avatar of MickaelE
MickaelE

asked on

How do you add a route to Linux

Hello All,

I need to add a route to linux and do not know how.  I know nothing about linux so I need a step by step answer.

This is what I do in windows, I go to a command prompt and type the followng

Route add -p 172.16.0.0 mask 255.255.255.0 192.168.0.3

Route add -p 192.168.5.0 mask 255.255.255.0 192.168.0.3

How and where to you do that in linux?

Thank you

Mike

Avatar of grblades
grblades
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi MickaelE,
You can use this syntax:-
route add -net 16.0.0.0 gw 192.168.0.0 netmask 25.255.255.0
route add gw 192.168.0.1

where 192.168.0.1 is the address of the gateway

man route
This method will not be persistant (the -p parameter in windows). You can add the command to the end of /etc/rc.d/rc.local so that it runs whenever the computer starts if you wish it to be permanent.
Avatar of MickaelE
MickaelE

ASKER

Hi Grblades,

Would you mind using my numbers in the example, I am not sure which goes where?

Mike
Dont know for other linuxes but in debian /etc/network/interfaces

auto eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.1
This is for Redhat linux
In redhat you can edit /etc/sysconfig/network and set a line such as:-
GATEWAY=10.0.0.2
This only sets the default route. If you need additional static routes edit the rc.local file as I described previously.
In rc.local file for next reboots and in console for actual session:

route add -net 172.16.0.0 netmask 255.255.255.0 gw 192.168.0.3
route add -net 192.168.5.0 netmask 255.255.255.0 gw 192.168.0.3

Hope this helps.
ASKER CERTIFIED SOLUTION
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Errr... I just wrote that ^^
Hi,

Not wanting to duplicate answers (deliberately or otherwise) I would suggest that you use the newer "iproute2" package available in most newer linuxes.

The commands in that case would be:

ip route add 172.16.0.0/24 via 192.168.0.3
ip route add 192.168.5.0/24 via 192.168.0.3

Explanation:

ip route is the command you are invoking. You are telling the command to add a route for network 172.16.0.0/24 (this is equivalent to having 24 bits for network id, i.e. netmask = 255.255.255.0 A single 255 represent 8 bits.) via the gateway address 192.168.0.3. Similarly for the second route.

To make these persistent. Simply execute the following line on you favorite shell (konsole, bash, etc.)

echo "ip route add 172.16.0.0/24 via 192.168.0.3" >> /etc/rc.local
echo "ip route add 192.168.5.0/24 via 192.168.0.3" >> /etc/rc.local

THis is a very good method of appending lines to the /etc/rc.local file in linux. This file is like a autoexec.bat file and is executed every time you reboot. So next time you reboot, these two lines (i.e. the text inside the quotes.) will be present in this file and thus will be executed and you will again have your routes setup.

Cheers,

Karan
hi,

This is exact
If u want to assign route for NIC 1
 
route add -net 172.16.0.0 netmask 255.255.255.0 gw 192.168.0.3 dev eth0
route add -net 192.168.5.0 netmask 255.255.255.0 gw 192.168.0.3 dev eth0

if u restart the system the route may not exist.....

so edit this file /etc/rc.local file and add your static routes using the route statement. I mean add the above lines.

even your reboot the system the routes exist................
lol the guy got the points... what a rip off ^^
Hi

You are right.  Sorry I clicked on the wrong accept button.  Is there a way to correc it?
If you ask here https://www.experts-exchange.com/Community_Support/ a moderator will unaccept the answer for you.
lol, poor you, Deurk!