Using the route command

The route command is used to manually manipulate the routing tables.

Usage:

route [ -f ] [ -n ] [ -q ] [ -C ] [ -v ] Command [ Family ] [ [ -net | -host ] Destination 
[ -prefixlen n] [ -netmask [ Address ] ] Gateway ] [ Arguments ] [- i] [-@ WparName] 

You can make manual entries into network routing tables with the route command. This command distinguishes between "routes to hosts" and "routes to networks" by interpreting the network address of the Destination variable. This variable can be specified with a symbolic name or numeric address.

The route command resolves all symbolic names into addresses, using the /etc/hosts file or the network name server.

Routes to a particular host are distinguished from those to a network by interpreting the internet address that is associated with the destination. The optional ph -net and -host force the destination to be interpreted as a network or a host, respectively. If the destination has a local address part of INADDR_ANY or if the destination is the symbolic name of a network, then the route is assumed to be to a network; otherwise, it is presumed to be a route to a host.

For example:

  • 128.32 is interpreted as -host 128.0.0.32
  • 128.32.130 is interpreted as -host 128.32.0.130
  • -net 128.32 is interpreted as 128.32.0.0
  • -net 128.32.130 is interpreted as 128.32.130.0

If the route is by way of an interface instead of through a gateway, then the -interface argument should be specified. The specified gateway is the address of the host on the common network. This indicates the interface to be used for transmission.

The -netmask argument must be followed by an address parameter to be interpreted as a network mask. You can override the implicit network mask generated in the -inet case by ensuring this option follows the Destination parameter.

All symbolic names that are specified for a destination or gateway are looked up first as a host name, using the gethostbyname subroutine. If this fails, then getnetbyname is used to interpret the name as a network name.

Note: route uses a routing socket and the message types RTM_ADD, RTM_DELETE, and RTM_CHANGE. Only the root user can modify the routing tables.

If the flush or -f command is specified, then route "flushes", or clears the routing tables of all gateway entries. You can choose to flush only those routes whose destinations are of a given address family by specifying an optional ph describing which address family.

The netstat -r command displays the current routing information contained in the routing tables.