GNU/Linux Network Configuration See and manipulate routes


Example

Manipulate the IP routing table using route

Display routing table

$ route # Displays list or routes and also resolves host names
$ route -n # Displays list of routes without resolving host names for faster results

Add/Delete route

OptionDescription
add or delAdd or delete a route
-host x.x.x.xAdd route to a single host identified by the IP address
-net x.x.x.xAdd route to a network identified by the network address
gw x.x.x.xSpecify the network gateway
netmask x.x.x.xSpecify the network netmask
defaultAdd a default route

Examples:

  • add route to a host $ route add -host x.x.x.x eth1
  • add route to a network $ route add -net 2.2.2.0 netmask 255.255.255.0 eth0
  • Alternatively, you could also use cidr format to add a route to network route add -net 2.2.2.0/24 eth0
  • add default gateway $ route add default gw 2.2.2.1 eth0
  • delete a route $ route del -net 2.2.2.0/24

Manipulate the IP routing table using ip

Display routing table

$ ip route show # List routing table

Add/Delete route

OptionDescription
add or del or change or append or replaceChange a route
show or flushthe command displays the contents of the routing tables or remove it
restorerestore routing table information from stdin
getthis command gets a single route to a destination and prints its contents exactly as the kernel sees it

Examples:

  • Set default gateway to 1.2.3.254 $ ip route add default via 1.2.3.254
  • Adds a default route (for all addresses) via the local gateway 192.168.1.1 that can be reached on device eth0 $ ip route add default via 192.168.1.1 dev eth0