How to add and delete route in linux

How to add and delete route in linux.

In linux / Unix like operating system we can add and delete route permanently and temporarily. In this blog I have explained how to add and delete route permanently and temporarily with syntax

 

How to add and delete route in linux.
Syntax to add route to a host and network.

route add -net/-host <IPaddress> netmask <subnetmask> gw <Gateway IPaddress> dev <Ethernet Interface>

Syntax to delete route to a host and network

route del -net/-host <IPaddres> netmask <subnetmask> gw <GatewayIP address> dev <Ethernet Interface>

How to add a static route to a linux host/machine

route add -host 192.168.1.175 gw 192.168.1.1 dev eth0

How to delete a static route to a linux host/machine

route del -host 192.168.1.175 gw 192.168.1.1 dev eth0

How to add a static route to a network

route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0

How to delete a static route to a network

route delete -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0

How to check whether the route is added or not

route -n

netstat -r

Using “route -n” and “netstat -r” you can verify that whether the current routing table.

How to add default route to a linux machine

route add default gw 10.60.1.1 eth0

Note:

When system reboot all the added routes will be deleted. To make it permanent or available even after reboot add the rule in file “/etc/rc.local”.

What is the use of “/etc/rc.local” file?

When the system boots all the commands in the file /etc/rc.local will be executed. The rc.local file will be executed only after all the services are started so if we add the routing commands in the rc.local file it will be added to the routing table while the system boots.

The newly added routes will also be deleted if we restart network services. To make it  permanent even after restarting network services add the routing rule in file “/etc/sysconfig/network-scripts/route-eth0″. The file will not be available by default we have to create it manually.

Syntax:

Target_address via gateway_address

create route for appropriate ethernet interface

cd /etc/sysconfig/network-scripts
cat > route-eth0
8.8.8.8 via 10.60.1.5

Then restart the network service, The route will not be removed.




Leave a Reply

Your email address will not be published. Required fields are marked *