Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Definition

Every interface knows which addresses it can reach directly: those inside its own subnet. For a server with 203.0.113.10/24, that is 203.0.113.0 to 203.0.113.255. Anything else must pass through a router, and the default gateway is the router used when no more specific route matches. Without it, the server can talk to its neighbors but not to the rest of the internet. Subnet sizes are explained in subnet mask and CIDR notation.

Find the Default Gateway

ip route show default

Typical output:

default via 203.0.113.1 dev eth0 proto static
PartMeaning
defaultMatches every destination, 0.0.0.0/0
via 203.0.113.1The gateway's address
dev eth0The interface used
proto staticSet in the configuration; dhcp means learned by DHCP

Interface names differ between servers, such as eth0, ens3, or enp1s0. For IPv6, run ip -6 route show default. Other fields are covered in the ip command guide.

How a Server Picks a Route

For every packet the kernel looks through the routing table and takes the most specific match, the route with the longest prefix. A /32 route to one host wins over a /24 route, and both win over the default route, which is /0 and matches everything. When two routes have the same prefix, the lower metric wins. Ask the kernel which route it would use:

ip route get 198.51.100.20

The reply names the gateway, the interface, and the source address the packet would carry. ip route show prints the whole table.

Before You Change It

A wrong gateway cuts the server off from the internet, including your SSH session. Open the server console in your provider's control panel before you start, so you have a way back in that does not depend on the network, and use a command that rolls back by itself where there is one.

Set It with Netplan or nmcli

On Ubuntu, the gateway is a route in the Netplan file under /etc/netplan/:

network:
  version: 2
  ethernets:
    eth0:
      addresses:
        - 203.0.113.10/24
      routes:
        - to: default
          via: 203.0.113.1

Run sudo netplan try, which undoes the change unless you confirm it, before sudo netplan apply. The older gateway4 key is deprecated. Full steps are in how to configure a static IP with Netplan.

On AlmaLinux and Rocky Linux, NetworkManager keeps the gateway in the connection profile:

sudo nmcli connection modify eth0 ipv4.gateway 203.0.113.1
sudo nmcli connection up eth0

The connection name can differ from the interface name; nmcli connection show lists both. See how to set a static IP with nmcli.

Gateway Outside the Subnet

Some cloud networks give a server an address with a /32 mask, or a gateway that is not inside the server's subnet. The kernel then rejects a plain default route, because it sees no way to reach the gateway. The fix is to declare the gateway as directly on the link: in Netplan, add on-link: true to the route; with the ip command, add onlink:

sudo ip route add default via 198.51.100.1 dev eth0 onlink

A route added with ip is lost at reboot. Use the exact gateway and settings your provider documents for your server.

The IPv6 Gateway

IPv6 servers often use a link-local gateway starting with fe80::, or learn the gateway from router advertisements. A link-local address is valid only together with an interface, so the route always names one. Setup is in how to configure IPv6 on Linux.

Symptoms of a Wrong Gateway

  • Local works, internet does not: hosts in the same subnet answer, nothing outside it does.
  • No default line: ip route show default prints nothing, and outgoing connections fail with "Network is unreachable".
  • Gateway silent: ping 203.0.113.1 times out. Some routers ignore ping, so also check ip neigh for a resolved MAC address.
  • Two default routes: traffic leaves through the wrong interface; remove one or give it a higher metric.

traceroute shows whether packets get past the first hop at all.

Was this answer helpful?
Loading...