5 April 2014

Linux Routing table

We can check current Linux route table with netstat command

# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         12.96.176.1     0.0.0.0         UG        0 0          0 eth2
5.72.185.0      0.0.0.0         255.255.255.0   U         0 0          0 eth0
12.96.154.0     0.0.0.0         255.255.255.0   U         0 0          0 eth1
12.96.176.0     0.0.0.0         255.255.255.0   U         0 0          0 eth2
13.122.288.72   5.72.185.1      255.255.255.240 UG        0 0          0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U         0 0          0 lo
135.188.0.0     5.72.185.1      255.255.0.0     UG        0 0          0 eth0
135.188.45.153  5.72.185.1      255.255.255.255 UGH       0 0          0 eth0
174.65.146.0    5.72.185.1      255.255.255.0   UG        0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0


Q: How to add a new route?

To add a single host destination:
# route add $Host_IP gw $Gateway

e.g:
# route add 12.153.4.22 gw 6.64.215.1

To add a network destination:
# route add –net $Network_IP netmask $MASK gw $Gateway

e.g:
# route add -net 12.153.4.0 netmask 255.255.255.248 gw 6.64.215.1


Q: How to delete route?

# route del –net $Network_IP netmask $MASK gw $Gateway



Q: How to check the default gateway?

In routing table, the row start with 0.0.0.0 has gateway IP which in this example is 12.96.176.1

# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         12.96.176.1     0.0.0.0         UG        0 0          0 eth2
5.72.185.0      0.0.0.0         255.255.255.0   U         0 0          0 eth0
12.96.154.0     0.0.0.0         255.255.255.0   U         0 0          0 eth1
12.96.176.0     0.0.0.0         255.255.255.0   U         0 0          0 eth2
13.122.288.72   5.72.185.1      255.255.255.240 UG        0 0          0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U         0 0          0 lo
135.188.0.0     5.72.185.1      255.255.0.0     UG        0 0          0 eth0
135.188.45.153  5.72.185.1      255.255.255.255 UGH       0 0    
      0 eth0
174.65.146.0    5.72.185.1      255.255.255.0   UG        0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0

and finally to set default gateway we can use following command:

# route add default gw 192.168.1.254 eth0





No comments:

Post a Comment