Linux route Add Command Examples

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?

In Linux and Unix-like system, a gateway is nothing but device that connects two networks. Often it is called a router or gateway. In most cases your ISP’s modem act as a default router or gateway. You can use any one of the following tool to add, display, delete Linux kernel routing table:
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux with ip or route command
Est. reading time 3 mintues
  1. route command : show / manipulate the IP routing table on Linux.
  2. 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
# route -n
Sample outputs:

Fig.01: Display routing table using route command

Fig.01: Display routing table using route command

# ip route show
# ip route list
Sample outputs:

Fig.02: ip command in action

Linux route Add Command Examples

I am going to show you both ip and route command. Most modern Linux distro recommend and use the ip command for setting or displaying default gateway IP address on Linux. Let us see some examples.

Linux add a default route using route command

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

Linux add a default gateway (route) using ip command

Route all traffic via gateway connected via eth0 network interface:
# ip route add dev eth0

Verify newly added route ip in the Linux kernel routing table

To verify new routing table, enter:
# ip route list
# 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
# ping

You should able to ping public IP address too:

And use the dig command or host command to resolve domain names provided that /etc/resolv.conf configured with correct DNS server names:

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:

## setup default gateway ##

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

The above config sets static routing for network via 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 netmask gw
down route del -net netmask gw

Save and close the file.

Generic method to add persistent static routing on Linux

The following method works with almost all Linux distributions. However, on systemd based Linux distro, see how to enable /etc/rc.local with systemd tutorial for more info.

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

/sbin/ip route add dev eth0


/sbin/ip route add dev eth0

Save and close the file in vim text editor.

Summing up

You learned how to add routing on Linux. See

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 27 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
27 comments… add one
  • Chris Jackson Oct 29, 2008 @ 14:14

    Changes to survive a reboot can be set in

    Other profiles for switching between different configurations can be found in

  • Momo Feb 14, 2009 @ 6:23

    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,

    • rajesh p Feb 25, 2015 @ 4:44

      add configuration file

      vi /etc/sysconfig/network
  • sandheep Feb 23, 2009 @ 9:15

    Hi ,

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


    • sunil sharma Aug 6, 2010 @ 4:42

      add the route command in linux

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


    • rajesh p Feb 25, 2015 @ 4:45
      route add -net NETMASK gw
      • amit kumar Feb 26, 2015 @ 21:08

        it worked ..thanks

  • Prosper Mbabazi May 15, 2009 @ 10:22

    Adding the route command in Linux

    Route traffic via gateway destined to :

    route add -net netmask gw

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

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



    • Amal Withanage Aug 24, 2011 @ 11:10

      Thank you very much this works

  • Mr-lol Jul 30, 2009 @ 9:28

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

  • Alex Dec 14, 2010 @ 9:22

    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 netmask gw

    After reboot check your route table:

    • khamees Apr 30, 2012 @ 8:27

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

  • Pankaj Mar 16, 2011 @ 6:52

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

    [root@plnotxsmsc01 rc.d]# route add -net gw
    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]#

    • Ranjith Jun 7, 2011 @ 9:58

      Dear Pankaj,

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

  • soroush May 2, 2012 @ 4:17

    route add -net netmask gw

  • soroush May 2, 2012 @ 4:18

    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.

    # 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 netmask gw

  • PauloCritchi Jun 27, 2012 @ 14:23

    Yes Alex, your comment was also what I needed.

    Thank you so much for that!


  • srinivas Jun 25, 2013 @ 12:40

    how to find remote systems gateway with out login

  • sonali Jun 28, 2013 @ 16:14

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

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

    and how to make routing information persistent,

    • Bijay Apr 25, 2014 @ 11:20

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

  • Rajashree Apr 22, 2014 @ 5:54

    I want to do ping6 to another linux machine..I added inet6 address in both the linux machine in the main LAN 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.

  • Victor Juarez Cabrera Apr 28, 2014 @ 16:22

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

  • kamalkakkar Jun 6, 2014 @ 6:48

    awsome explanation!! love it

  • sayed Oct 5, 2015 @ 6:15

    Very help full

  • mtechmahendra Dec 7, 2015 @ 8:19

    check the routes #route

    route add -net netmask gw

    after add permanent route entry
    create a file vi /etc/sysconfig/network-scripts/route-eth0

    after add route entry this format




  • Lic. Matias Colli Jan 25, 2016 @ 18:01

    En los sistemas RHEL/CentOS/Fedora/Scientific Linux la configuración de la ruta por defecto (default gateway) se guarda en /etc/sysconfig/network y las rutas estáticas en /etc/sysconfig/network-scripts/route-ethX (siendo X el número de la placa de red, verbigracia eth0 para la primer placa red).

    En los sistemas Debian / Ubuntu Linux la configuración de la ruta por defecto (default gateway) se guarda en /etc/network/interfaces y las rutas estáticas se pueden agregar bajo a sección eth0 o bien en el archivo /etc/rc.local junto con las definiciones de ejecuciones de archivos locales al inicio del sistema.

    Lic. Matias Colli
    Perito Judicial en Informática

  • jadhav Feb 5, 2017 @ 7:52

    how to add the route in lubuntu
    route add mask -p also how to check the route table. the application needs IE siebel framework. need solution

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum