Linux route Add Command Examples nixCraft Updated Tutorials/Posts

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:

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

You should able to ping public IP address too:
ping 1.1.1.1
ping 8.8.8.8

And use the dig command or host command to resolve domain names provided that /etc/resolv.conf configured with correct DNS server names:
host www.cyberciti.biz
host google.com
dig 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:

## setup default gateway ##
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

OR

/sbin/ip route add 192.168.1.0/24 dev eth0

Save and close the file in vim text editor.

Recommended readings
Get up to 50% off on Linux Foundation certified SysAdmin, Kubernetes/Containers and Open Stack certification bundle. Offer expires on August 3, 2018
training.linuxfoundation.org

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.

Facebook Comments
Spread the love

Posted by News Monkey