linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Simulator
  • Knowledge base
  • Interview prep
Index
Categories
All entries
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
home/linux/kb/Networking: L2 / L3/default-gateway

kb/network-l2-l3 ── Networking: L2 / L3 ── beginner

Default gateway: leaving your own network

The router IP in your subnet where the stack sends packets for every address that **is not local**. One gateway per host, but in multi-homed setups there can be several.

view as markdownaka: gateway, default-route

Why you need it

When you have a packet for 8.8.8.8, you ask two questions:

  1. Is it in my subnet? Compare IP+mask against the local network. If yes, send it directly over arp and an Ethernet frame
  2. If not, where do you send it? This is where the default gateway comes in: a special route that says "anything I don't know about, hand to this router"

In Linux it looks like this:

$ ip route
default via 192.168.1.1 dev eth0     # ← there it is, the default gateway
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.50

The first line means "everything unknown → send to 192.168.1.1". The second means "my own subnet → directly over eth0".

Where it comes from

Three typical paths:

  • DHCP provides it in option 3 (router). At home that is your Wi-Fi router
  • Set by hand (/etc/network/interfaces, NetworkManager, cloud-init)
  • Received via RA (router advertisement) in IPv6

How it is used

When a packet for 8.8.8.8 leaves:

  1. The stack checks the routing-table. The match for 8.8.8.8 is default
  2. The default route says: next hop = 192.168.1.1
  3. The stack checks: 192.168.1.1 is in my subnet, so I send an L2 frame
  4. ARP for 192.168.1.1 → we get the router's MAC
  5. We build a frame with dst MAC = router's MAC, dst IP = 8.8.8.8
  6. The router catches the frame, sees the dst IP is not its own, and walks its own table

An important point: in the L2 header the destination is the gateway's MAC, not the final server. At L3 the destination stays 8.8.8.8. At each hop the L2 header is rewritten, the L3 one is not.

Several gateways

  • Multiple default routes with different metrics, for failover between providers or Wi-Fi/Ethernet
  • Policy routing (ip rule), different gateways for different source IPs or netfilter marks
  • VPNs often change the default gateway to the tunnel, so all traffic goes through the VPN

When it breaks

  • No default route: you ping 8.8.8.8 and get network is unreachable. Fixed with ip route add default via X
  • Gateway unreachable: packets leave, but ARP cannot find the MAC. You see it in ip neigh as FAILED
  • Wrong gateway (not in your subnet): the stack rejects it immediately, next hop is not directly reachable

§ команды

bash
ip route show default

Show the default gateway

bash
ip route add default via 192.168.1.1 dev eth0

Set it by hand (if DHCP did not provide one or for manual configuration)

bash
ip route del default

Delete the default route. All packets to foreign networks start failing

bash
ip route get 8.8.8.8

Which route will be used for a specific destination

§ см. также

  • routing-tableRouting tableThe routing table lists where to send packets for each destination. The longest matching prefix wins.
  • subnetting-cidrSubnetting and CIDRCIDR /N specifies how many of the 32 bits (or 128 for IPv6) belong to the network. /24 gives 256 addresses, /30 gives 4 (p2p), /16 gives 65536. A host in a subnet can communicate directly only with hosts that share the same prefix.
  • policy-routingPolicy Routing: Rule-Based RoutingPolicy routing selects a routing table based on src-IP, fwmark, iif, or tos. ip rule + ip route table N. Multi-uplink, source-based routing, VRF, split-tunnel VPN. RPDB is the Routing Policy Database.
  • dhcp-protocolDHCP: Dynamic Host Configuration ProtocolDHCP gives a host its IP address, subnet mask, gateway, and DNS via broadcast. 4 packets: DORA = Discover (client), Offer (server), Request (client), Ack (server). The lease renews at 50% of the TTL.
  • ip-forwardingIP Forwarding: Turn a Host into a RouterLinux does not forward packets between interfaces by default. Enable it with `sysctl net.ipv4.ip_forward=1`. Without this, NAT, VPN routing, and any forwarding will not work.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies