Linux route Add Command Examples

by on February 7, 2008 · 20 comments· LAST UPDATED November 9, 2013

in , ,

I am a new Linux user. How do I add a new or default gateway using route command on Linux operating systems? How can I use route command to show or set a new route on Linux based server or desktop system?

You can use any one of the following tool to add, display, delete Linux kernel routing table:
Tutorial details
DifficultyEasy (rss)
Root privilegesYes
RequirementsNone
Estimated completion time5m

(a) route command : show / manipulate the IP routing table on Linux.

(b) ip command : show / manipulate routing, devices, policy routing and tunnels on Linux.

Display your current routing table

Open the Terminal or login to server using ssh/console. Type the following command to display routing table:
# route
OR
# route -n
Sample outputs:

Fig.01: Display routing table using route command

Fig.01: Display routing table using route command


# ip route show
OR
# ip route list
Sample outputs:
Fig.02: ip command in action

Fig.02: ip command in action

Linux add a default route using route command

Route all traffic via 192.168.1.254 gateway connected via eth0 network interface:
# route add default gw 192.168.1.254 eth0

Linux add a default gateway (route) using ip command

Route all traffic via 192.168.1.254 gateway connected via eth0 network interface:
# ip route add 192.168.1.0/24 dev eth0

Verify newly added route ip in the Linux kernel routing table

To verify new routing table, enter:
# ip route list
OR
# route -n

Verify new route

Use the ping command to verify connectivity to your router or external network:
# ping your-router-ip-here
# ping your-ISPs-Gateway-ip-here
# ping 192.168.1.254
# ping www.cyberciti.biz

How do I make routing changes persistent across reboots?

To make route entry persistent in the Linux kernel routing table, you need to modify config file as per your Linux distributions.

RHEL/CentOS/Fedora/Scientific Linux persistent routing configuration

Edit /etc/sysconfig/network and set default gateway IP address:
# vi /etc/sysconfig/network
Sample outputs:

 
GATEWAY=192.168.1.254
 

You can add additional static route for eth0 by editing /etc/sysconfig/network-scripts/route-eth0 file as follows:

10.0.0.0/8 via 10.10.29.65

The above config sets static routing for network 10.0.0.0/8 via 10.9.38.65 router.

Debian / Ubuntu Linux persistence static routing configuration

Edit /etc/network/interfaces file, enter:
# vi /etc/network/interfaces
Append the following in eth0 section:

up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254
down route del -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254

Save and close the file.

Generic method to add persistent static routing on Linux

The following method works with almost all Linux distributions.

Edit /etc/rc.d/rc.local or /etc/rc.local, enter
# vi /etc/rc.local
Append the following line:

/sbin/ip route add 192.168.1.0/24 dev eth0

Save and close the file.

Recommended readings
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 20 comments… read them below or add one }

1 Chris Jackson October 29, 2008 at 2:14 pm

Changes to survive a reboot can be set in
/etc/sysconfig/networking/devices/route-ethX

Other profiles for switching between different configurations can be found in
/etc/sysconfig/networking/profiles/default/route-ethX

Reply

2 Momo February 14, 2009 at 6:23 am

Dear All,

I have a problem , my default route will go away when i reboot my system, regarding to this matter that all info was issued under interface network-script.

Please advice,
Momo.

Reply

3 sandheep February 23, 2009 at 9:15 am

Hi ,

Can you please let me know how to add the route command in Linux.

Regards
Sandheep

Reply

4 sunil sharma August 6, 2010 at 4:42 am

Hi
add the route command in linux

# route add -net 10.10.1.1/24 ethx
Alternate is by ip command
# ip route add 10.10.2.2/24 ethx
after that configure gateway,netmask and ip in route-ethx file located in /etc/sysconfig/network-scripts dir.

Thanks.

Reply

5 Prosper Mbabazi May 15, 2009 at 10:22 am

Adding the route command in Linux

Route traffic via 192.168.0.5 gateway destined to 192.168.1.16 :

route add -net 192.168.1.16 netmask 255.255.255.240 gw 192.168.0.5

This will handle all the traffice from your client systems destined to the 192.168.1.16 subnet.
The resources that will be accessed are in the range 192.168.1.16 – 192.168.1.30

I have tried it on Suse Linux, and i belive other flavors are similar or close to this .

Thanks

Prosper

Reply

6 Amal Withanage August 24, 2011 at 11:10 am

Thank you very much this works

Reply

7 Mr-lol July 30, 2009 at 9:28 am

To add default route for a host:
#route add 192.168.0.1 gw 10.0.0.1
“where 192.168.0.1 is destination host, gw commad to specify gateway and 10.0.0.1 is the gateway to destination host. (Test on Feroda 11)”

Reply

8 Alex December 14, 2010 at 9:22 am

Another way to add route at startup (testing on CentOS)

edit file /etc/rc.d/rc.local (add to end of file your route):
/sbin/route add -net 192.168.11.0 netmask 255.255.255.0 gw 10.0.0.1

After reboot check your route table:
/sbin/route

Reply

9 khamees April 30, 2012 at 8:27 am

Thanks Alex. You saved my day. On CentOS this was needed

Reply

10 Pankaj March 16, 2011 at 6:52 am

i want to add below command in Linux server.but here it is showing error

[root@plnotxsmsc01 rc.d]# route add -net 10.202.11.0 gw 10.102.13.241
bash: route: command not found
[root@plnotxsmsc01 rc.d]#

=====

[root@plnotxsmsc01 rc.d]# uname -a
Linux plnotxsmsc01 2.6.18-128.el5xen #1 SMP Wed Dec 17 12:01:40 EST 2008 x86_64 x86_64 x86_64 GNU/Linux
[root@plnotxsmsc01 rc.d]#

Reply

11 Ranjith June 7, 2011 at 9:58 am

Dear Pankaj,

please try /sbin/route or locate where the route is

Reply

12 soroush May 2, 2012 at 4:17 am

route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1

Reply

13 soroush May 2, 2012 at 4:18 am

The routing information above is not persistent across reboots. After a reboot, the routing information will be lost and you need to add them in again.

To make the routing information persistent, add the “route add” line as seen above into the /etc/rc.local file.

Sample /etc/rc.local file.

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don’t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1

Reply

14 PauloCritchi June 27, 2012 at 2:23 pm

Yes Alex, your comment was also what I needed.

Thank you so much for that!

Cheers

Reply

15 srinivas June 25, 2013 at 12:40 pm

how to find remote systems gateway with out login

Reply

16 sonali June 28, 2013 at 4:14 pm

i want to add below command in Linux server.but here it is showing error

[root@zmocmp1 sbin]# route add -net 10.26.66.0 netmask 255.255.255.192 gw 0.0.0.0
SIOCADDRT: Invalid argument
[root@zmocmp1 sbin]#

and how to make routing information persistent,

Reply

17 Bijay April 25, 2014 at 11:20 am

please assign valid gateway it will work….!!!

Reply

18 Rajashree April 22, 2014 at 5:54 am

Hi,
I want to do ping6 to another linux machine..I added inet6 address in both the linux machine in the main LAN interface..bt ping6 is not wrking. “Destination Unreachable is coming”. So,For that when i tried to add route to ipv6 thorough a gateway,using the command ” /sbin/route -A inet6 add 2000::/64 gw 2000:db8:0:bbbb::1 ” is is telling invalid command..pls help me with this and how can i rectify it.

Reply

19 Victor Juarez Cabrera April 28, 2014 at 4:22 pm

Muchas gracias me ayudo bastante, saludos desde México…

Reply

20 kamalkakkar June 6, 2014 at 6:48 am

awsome explanation!! love it

Reply

Leave a Comment

Tagged as: , , , , , , , ,

Previous Faq:

Next Faq: