Linux Set Up Routing with ip Command

Can you explain the ip command to setup routing on Linux based systems? How do I use the ip command to configure the routing table of the Linux kernel?

The ip command can be used for the following tasks on Linux:
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements ip command
Est. reading time 10m
  1. Show / manipulate routing
  2. Show / manipulate devices
  3. Policy routing
  4. Tunnels

How to view or display Linux routing table

Type the following command:
$ ip route show
$ ip route list
Sample Outputs: dev ppp0  proto kernel  scope link  src dev eth0  proto kernel  scope link  src dev ra0  proto kernel  scope link  src dev eth0  scope link  metric 1000 dev ppp0  scope link 
default via dev ra0  metric 100

Each entry is nothing but an entry in the routing table (Linux kernel routing table). For example, the following line represents the route for the local network. All network packets to a system in the same network are sent directly through the device ra0: dev ra0  proto kernel  scope link  src

Our default route is set via ra0 interface i.e. all network packets that cannot be sent according to the previous entries of the routing table are sent through the gateway defined in this entry i.e is our default gateway.

How to set a route to the locally connected network eth0 on Linux

Type the following command to sent all packets to the local network directly through the device eth0:, enter:
# ip route add dev eth0
OR route traffic via gateway for network:
# ip route add via dev eth0

Set a default route

All network packets that cannot be sent according to the previous entries of the routing table are sent through the following default gateway:
# ip route add default via

Delete route from table

Type the following command
# ip route delete dev eth0
Let us delete default route too:
# ip route add default via dev eth0

Linux add a default route/static route or delete a route using the ip command.

How do I verify routing configurations?

Use the ping command or host command commands to make sure you can reach to your gateway:
ping Your-Gateway-Ip-Here
ping Your-DNS-Server-IP-Here

Linux Set Up Routing with ip command and save it to a configuration file

All routing settings made with the ip tool (or route command) are lost when you reboot Linux server. See our previous article about configuring static routes in a Debian/Ubuntu or CentOS/Red Hat Enteprise Linux systems.

How to add a static route on Ubuntu or Debian

Here is a sample for eth0 displayed using the cat command cat /etc/network/interfaces

# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static

Add a static route on CentOS / RHEL

Create a file named /etc/sysconfig/network-scripts/route-eth0 for interface eth0 and add static route entry: via dev eth0
Save and close the file. Restart the networking service:
sudo service network restart
sudo systemctl restart network

Recommended readings:

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

🐧 4 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
4 comments… add one
  • manas Feb 21, 2011 @ 7:46

    i m having a linux pc and having 7 window based pc….
    i want a complete routing through linux pc…..
    so can anyone help me out

  • Jose Tapia Jun 21, 2012 @ 8:11

    Very useful info, direct to the mean :D thanks

  • higkoo Jan 23, 2013 @ 7:33

    How to save the ‘ip route table’ in config file,so that it won’t lose after network restart ?

    For example:
    # ip route show table lvs
    default via dev eth1 proto kernel
    # service network restart
    Shutting down interface eth1: [ OK ]
    Shutting down loopback interface: [ OK ]
    Bringing up loopback interface: [ OK ]
    Bringing up interface eth1: [ OK ]
    # ip route show table lvs

    How to save it?

  • innocent mwina Jun 8, 2015 @ 11:00

    ihave liked the procedures,are soo good and great.thanks

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