Linux uses an IPv4 default route when no more specific route matches a destination. With ip route, you can add one temporarily and confirm that a remote destination uses the intended gateway.

A normal default route lives in the main routing table and points to a gateway on a connected subnet. The host needs a configured IPv4 address and a gateway supplied by the network administrator or router settings; an address ending in .1 is not automatically a router.

Default-route changes require elevated privileges and take effect immediately in the running routing table. They are temporary and can be lost after reboot or when the system's network configuration is reapplied.

Steps to add an IPv4 default route with ip route:

  1. Record the current IPv4 routes in the main routing table.
    $ ip route show
    192.168.73.0/24 dev eth0 proto kernel scope link src 192.168.73.10 

    There is no default line here, so this host has no default route yet. It has 192.168.73.10 on eth0, with a router at 192.168.73.1; use your own network's gateway and interface in the commands that follow.

  2. Arrange console access before changing a remote host's default route.

    A wrong gateway can immediately break SSH and other off-subnet connections. Remote changes need working local or out-of-band console access. If you replace an existing default, recovery means restoring the original gateway, interface and any metric from the recorded route.

  3. Add the default route through your gateway and interface.
    $ sudo ip route add default via 192.168.73.1 dev eth0

    default means 0.0.0.0/0, via identifies the gateway, and dev identifies the outgoing interface. Nexthop has invalid gateway means the gateway is not reachable through that interface.

    If this returns RTNETLINK answers: File exists, the host already has a default route. Use sudo ip route replace default via 192.168.73.1 dev eth0 instead.

  4. Confirm the route selected for a remote IPv4 destination.
    $ ip route get 1.1.1.1
    1.1.1.1 via 192.168.73.1 dev eth0 src 192.168.73.10 uid 1001 
        cache 

    The via gateway and dev interface should match the route you added. If you added the wrong gateway, undo it with sudo ip route del default via 192.168.73.1 dev eth0. This lookup sends no packets, so it does not prove Internet access.

Share this guide