Default gateway

From LQWiki
Jump to navigation Jump to search

When sending a data packet over the network, the computer takes a look at its routing table to see where to send it. If it can't find a route to the destination by looking in its own routing table, it forward the packet to the address given as default gateway, sometimes called the default route.

Viewing The Default Gateway

The command

route -n

can be used to show what default gateway is currently set.

In the output, you are usually looking for an entry like so:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
[Output Skipped]
0.0.0.0         192.168.1.1    0.0.0.0         UG    0      0        0 eth0

The Destination entry "0.0.0.0" means this is the default route, and the Gateway 192.168.1.1 is the address of the router that will receive the packets to send them out to the rest of the Internet. "eth0" is the device, in this case an Ethernet network card, which will send out the packets to the router.

Setting The Default Gateway

The route command can set the default gateway:

route add default gw 192.168.1.1 eth0

Replace 192.168.1.1 with the ip address of your gateway router, and "eth0" with the name of your network device. Note that if you do not first set the ip address of eth0 correctly (to be in the same subnetwork as the router) you will receive a "Network is unreachable" error.

iproute

Some distributions make available the newer "iproute" package of tools.

ip route add default via 192.168.1.1 dev eth0

Execute "ip route help", or see the IP-ROUTE(1) manual page, for more information.