How To Add Route In Linux
In this article, we will learn: Route add command Linux. How to configure static route, and delete temporary and permanent static routes in CentOS/RedHat Linux servers.
Route manipulates the kernel’s IP routing tables. Its primary use is to set up static routes to specific hosts or networks via an interface after it has been configured with the ifconfig program. Route program now is obsolete. The newer version is ip route.
Show Linux Routing Table
Command route -n will show Linux routing table:
The output of the kernel routing table is organized in the following columns:
- Destination
- The destination network or destination host.
- Gateway
- The gateway address or ‘*’ if none set.
- Genmask
- The netmask for the destination net; ‘255.255.255.255’ for a host destination and ‘0.0.0.0’ for the default route.
- Flags
- Possible flags include
U (route is up)
H (target is a host)
G (use gateway)
R (reinstate route for dynamic routing)
D (dynamically installed by daemon or redirect)
M (modified from routing daemon or redirect)
A (installed by addrconf)
C (cache entry)
! (reject route)
Also you can use one of the following commands:
# netstat -nr # ip route list
Add a Temporary Route
For adding temporary static routes in Linux we can use route command. So after system reboot all changes will be lost.
To add a static route for a specific host:
# route add -host 10.110.55.55 gw 10.110.0.2 # route add -host 10.110.55.55 eth0
Delete a static route:
# route del -host 10.110.55.55 eth0
To add a static route for a specific network:
# route add -net 192.168.51.0/24 gw 10.110.0.1 # route add -net 192.168.51.0/24 eth0
Delete a static route for a specific network:
# route add -net 192.168.51.0/24 eth0
Add Permanent Static Route
For add permanent static route, we need to edit the interface file in /etc/sysconfig/network-scripts folder.
Sample configuration for route-eth0 file:
# vi /etc/sysconfig/network-scripts/route-eth0
10.110.0.0/24 via 192.168.0.10 dev eth0
Add Default Gateway Linux CentOS/RedHat
To add Default Gateway:
# route add default gw 10.110.10.1 eth0
To delete default gateway use command route del:
# route del default gw 10.110.10.1 eth0
To set default gateway and make routing changes persistent after reboot we need edit configuration file: /etc/sysconfig/network.
NETWORKING=yes HOSTNAME=myhost GATEWAY=10.110.10.1
Save changes end restart networking service:
# service network restart
Discuss article in ArsTech Forum