[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190961
  • Last Modified:

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

0
MickaelE
Asked:
MickaelE
  • 4
  • 3
  • 3
  • +5
1 Solution
 
grbladesCommented:
Hi MickaelE,
You can use this syntax:-
route add -net 16.0.0.0 gw 192.168.0.0 netmask 25.255.255.0
0
 
paranoidcookieCommented:
route add gw 192.168.0.1

where 192.168.0.1 is the address of the gateway

man route
0
 
grbladesCommented:
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.
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
MickaelEAuthor Commented:
Hi Grblades,

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

Mike
0
 
paranoidcookieCommented:
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
0
 
MickaelEAuthor Commented:
This is for Redhat linux
0
 
grbladesCommented:
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.
0
 
deurkCommented:
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.
0
 
wesly_chenCommented:
> Would you mind using my numbers in the example,
> Route add -p 172.16.0.0 mask 255.255.255.0 192.168.0.3
==> # /sbin/route add -net 172.16.0.0 netmask 255.255.255.0 gw 192.168.0.3

> Route add -p 192.168.5.0 mask 255.255.255.0 192.168.0.3
==> # /sbin/route add -net 192.168.5.0 netmask 255.255.255.0 gw 192.168.0.3

Need to be root to do it.

Wesly
0
 
deurkCommented:
Errr... I just wrote that ^^
0
 
kidomanCommented:
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
0
 
thribhuCommented:
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................
0
 
deurkCommented:
lol the guy got the points... what a rip off ^^
0
 
MickaelEAuthor Commented:
Hi

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 3
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now