Route

From LQWiki
Jump to: navigation, search

route is the command to view and edit the routing table. With route you can see what is the routing for IP-packets coming from a network interface for a specified destination is. A default gateway may be listed for non-routable packets.

Example

Show your routing table:

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         10.0.0.138      0.0.0.0         UG    0      0        0 eth1

Options

-n show numerical addresses instead of hostnames

Adding a route

Gateway

Typical pc

For a pc with typical network access, to add a gateway issue

route add default gw 192.168.1.1 

(where 192.168.1.1 is the address of your gateway)

Adding a network

To tell your Linux to send packages for the network 192.168.0.0/24 to the network device eth3:

route add -net 192.168.0.0 netmask 255.255.255.0 eth3

The same with a default gateway of 192.168.0.1:

route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.1 eth3

Deleting an entry

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 rausb0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 rausb0
$ route delete -net 0.0.0.0
$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 rausb0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
$

Strange error messages should not hold you back:

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     *               255.255.255.0   U     0      0        0 eth1
link-local      *               255.255.0.0     U     0      0        0 eth1
loopback        *               255.0.0.0       U     0      0        0 lo
default         192.168.0.1     0.0.0.0         UG    0      0        0 eth1
$ route del -host 192.168.0.0
SIOCDELRT: No such process
$ route del -net 192.168.0.0
SIOCDELRT: Invalid argument
$ route del -net 192.168.0.0 netmask 255.255.255.0 eth1
$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
link-local      *               255.255.0.0     U     0      0        0 eth1
loopback        *               255.0.0.0       U     0      0        0 lo
default         192.168.0.1     0.0.0.0         UG    0      0        0 eth1 
$

See also