You may use the following method to add a static route to the routing table of a Linux server using the command line. The server in this example is running CentOS 5.5 64 bit.
I have decided to create a static route to Google.com but before doing so I need to find the Internet Protocol (IP) address of Google.com using the “host” command.
# host google.com
google.com has address 74.125.91.104
google.com has address 74.125.91.105
google.com has address 74.125.91.106
google.com has address 74.125.91.147
google.com has address 74.125.91.99
google.com has address 74.125.91.103
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
I will use the IP address 74.125.91.104 for this example.
First, we will view the current routing table of the Linux server using the “netstat -r” command.
# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
209.208.62.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
10.0.0.0 * 255.0.0.0 U 0 0 0 eth1
default 209.208.62.1 0.0.0.0 UG 0 0 0 eth0
Second, we will ping the IP address 74.125.91.104 to verify the network connection between these two devices is successful.
# ping -c 4 74.125.91.104
PING 74.125.91.104 (74.125.91.104) 56(84) bytes of data.
64 bytes from 74.125.91.104: icmp_seq=1 ttl=48 time=29.6 ms
64 bytes from 74.125.91.104: icmp_seq=2 ttl=48 time=29.6 ms
64 bytes from 74.125.91.104: icmp_seq=3 ttl=48 time=29.2 ms
64 bytes from 74.125.91.104: icmp_seq=4 ttl=48 time=29.2 ms
— 74.125.91.104 ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 29.257/29.451/29.668/0.256 ms
Third, I will purposely create a static route to route the IP address 74.125.91.104/24 to interface “eth 1″ which is a private interface and then change the static route to utilize the public interface “eth 0″.
# route add -net 74.125.91.0 netmask 255.255.255.0 dev eth1
Fourth, we will view the routing table to verify it has added the static route that was just configured.
# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
209.208.62.0 * 255.255.255.0 U 0 0 0 eth0
74.125.91.0 * 255.255.255.0 U 0 0 0 eth1
169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
10.0.0.0 * 255.0.0.0 U 0 0 0 eth1
default 209.208.62.1 0.0.0.0 UG 0 0 0 eth0
Fifth, we will now ping the IP address 74.125.91.104 and it should not work due to the fact that eth 1 is a private interface and does not route to the internet.
# ping -c 4 74.125.91.104
PING 74.125.91.104 (74.125.91.104) 56(84) bytes of data.
From 10.0.24.1 icmp_seq=2 Destination Host Unreachable
From 10.0.24.1 icmp_seq=3 Destination Host Unreachable
From 10.0.24.1 icmp_seq=4 Destination Host Unreachable
— 74.125.91.104 ping statistics —
4 packets transmitted, 0 received, +3 errors, 100% packet loss, time 3000ms
, pipe 3
Sixth, we will now delete the static route and create a new one that will forward the traffic to network 74.125.91.0/24 out of eth 0 then view the routing table.
# route del -net 74.125.91.0 netmask 255.255.255.0 dev eth1
# route add -net 74.125.91.0 netmask 255.255.255.0 dev eth0
# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
209.208.62.0 * 255.255.255.0 U 0 0 0 eth0
74.125.91.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
10.0.0.0 * 255.0.0.0 U 0 0 0 eth1
default 209.208.62.1 0.0.0.0 UG 0 0 0 eth0
Lastly, we will ping the IP address 74.125.91.104 to verify the static route configuration is functioning correctly.
# ping -c 4 74.125.91.104
PING 74.125.91.104 (74.125.91.104) 56(84) bytes of data.
64 bytes from 74.125.91.104: icmp_seq=1 ttl=48 time=32.1 ms
64 bytes from 74.125.91.104: icmp_seq=2 ttl=48 time=28.7 ms
64 bytes from 74.125.91.104: icmp_seq=3 ttl=48 time=29.2 ms
64 bytes from 74.125.91.104: icmp_seq=4 ttl=48 time=29.3 ms
— 74.125.91.104 ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 28.780/29.884/32.131/1.321 ms
Enjoy!