What Is My Gateway Address?

How to pick the gateway address on each Linux machine in a small network, with the ip route commands for per-subnet routes and a default gateway.

On this page

Updated November 2026: the commands now use ip route, because current Linux releases no longer install the old route command. The route versions are at the end of the post.

A gateway is the address a machine sends a packet to when it can’t reach the destination directly. Picking the right one on Linux is easy once you see the network on paper. Take the three Linux machines in Figure 1, with these subnets and IP addresses:

Figure 1. Network configuration.

  • Subnet 1: 10.0.0.0/24, IP Address 1: 10.0.0.1, and IP Address 2: 10.0.0.2.
  • Subnet 2: 11.0.0.0/24 and IP Address 3: 11.0.0.1.
  • Subnet 3: 12.0.0.0/24, IP Address 4: 12.0.0.1, and IP Address 6: 12.0.0.2.
  • Subnet 4: 13.0.0.0/24 and IP Address 5: 13.0.0.1.

Run ip route to check the current routes on each PC. Also, we need to enable IP forwarding on the routers.

There are two kinds of rows in the tables of Figure 1:

  • A subnet the machine is connected to. The “gateway” is the machine’s own address on that subnet. Linux adds this route by itself when you assign the address, so there is nothing to type.
  • Everything else. The gateway is the address of the next router, on a subnet that both machines share.

If you are not sure which subnet an address belongs to, the subnet calculator shows the range and prints these commands for you.

Configuring PC 1

PC 1 is only connected to Subnet 1. Assigning its address creates the route to Subnet 1:

$ sudo ip addr add 10.0.0.1/24 dev <interface>

To connect PC 1 to other subnets on the network, we either need to add other subnets addresses to the routing table one by one (as in this static routing network) or add a default gateway to send all the packets not intended for Subnet 1 to that gateway.

$ sudo ip route add default via 10.0.0.2

Configuring PC 2

PC 2 is a router with one address on each of Subnets 1 to 4. Assigning those four addresses creates the four routes in its table:

$ sudo ip addr add 10.0.0.2/24 dev <interface_1>
$ sudo ip addr add 11.0.0.1/24 dev <interface_2>
$ sudo ip addr add 12.0.0.1/24 dev <interface_3>
$ sudo ip addr add 13.0.0.1/24 dev <interface_4>

And the default gateway is for all the packets to other networks which are not directly connected to PC 2:

$ sudo ip route add default via 12.0.0.2

Configuring PC 3

PC 3 is directly connected to Subnet 3, but it is not directly connected to Subnets 1, 2, and 4. It reaches those through PC 2. To add the routes, we use the following commands:

$ sudo ip addr add 12.0.0.2/24 dev <interface>
$ sudo ip route add 10.0.0.0/24 via 12.0.0.1
$ sudo ip route add 11.0.0.0/24 via 12.0.0.1
$ sudo ip route add 13.0.0.0/24 via 12.0.0.1

NOTE: Addresses and routes that you add with ip are lost when the machine restarts. To make them permanent, put them in the network configuration of your distribution: Netplan on Ubuntu, or NetworkManager (nmcli) on Rocky Linux, AlmaLinux and RHEL.

The Old route Commands

The original version of this post used route from the net-tools package. If you still have it, these are the same routes for PC 3 and the default gateway for PC 1:

$ route add -net 10.0.0.0 netmask 255.255.255.0 gw 12.0.0.1
$ route add -net 11.0.0.0 netmask 255.255.255.0 gw 12.0.0.1
$ route add -net 13.0.0.0 netmask 255.255.255.0 gw 12.0.0.1
$ route add default gw 10.0.0.2
Written by Sina Fathi-Kazerooni

I write about reinforcement learning, transformers, multi-agent systems, and the tools I build along the way.