Linux Set Up Routing with ip Command

last updated in Categories , ,

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:

  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
OR
$ ip route list
Sample Outputs:

10.0.31.18 dev ppp0  proto kernel  scope link  src 10.1.3.103 
192.168.2.0/24 dev eth0  proto kernel  scope link  src 192.168.2.1 
192.168.1.0/24 dev ra0  proto kernel  scope link  src 192.168.1.106 
169.254.0.0/16 dev eth0  scope link  metric 1000 
10.0.0.0/8 dev ppp0  scope link 
default via 192.168.1.1 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:

192.168.1.0/24 dev ra0  proto kernel  scope link  src 192.168.1.106

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 192.168.1.1 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 192.168.1.0 directly through the device eth0:, enter:
# ip route add 192.168.1.0/24 dev eth0
OR route traffic via 192.168.2.254 gateway for 192.168.2.0/24 network:
# ip route add 192.168.2.0/24 via 192.168.2.254 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 192.168.1.254

Delete route from table

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

Linux Set Up Routing with ip Command
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
ping 192.168.1.254
ping www.cyberciti.biz
host www.cyberciti.biz

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
    address 192.168.2.24
    gateway 192.168.2.254

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:
10.105.28.0/24 via 10.105.28.1 dev eth0
Save and close the file. Restart the networking service:
sudo service network restart
OR
sudo systemctl restart network

Recommended readings:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

4 comment

  1. 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

  2. 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 192.168.10.10 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
    [Empty]

    How to save it?

    Still, have a question? Get help on our forum!