SkillPilotAI

route

The route command shows a machine's IP routing table and adds, changes or deletes static routes. It decides which gateway a packet leaves through, on Windows, Linux (net-tools) and macOS.

55.1 What it does, where it runs

OSWhere it comes fromNotes
WindowsBuilt-inAdministrator prompt needed for add/change/delete
Linuxnet-tools package (legacy)Modern replacement: ip route
macOSBuilt-in /sbin/routeFull table: netstat -rn

55.2 How it works: a new path with route add

  • Lab: PC 192.168.75.10, home router 192.168.75.1 (default gateway), lab router 192.168.75.2, lab server 192.168.75.130 behind it.
  • At first the PC's table has only its own LAN and a default route.
  • Each destination is matched against every line; the longest matching prefix wins; metric only breaks ties between equal prefixes.
Before and after route add
  1. 1.PC pings 192.168.75.130; only the default route matches

  2. 2.Packet goes to the home router 192.168.75.1

  3. 3.Home router has no route to the lab network: Destination Net Unreachable

  4. 4.route add -net 192.168.75.128/26 gw 192.168.75.2 on the PC

  5. 5.Now /26 and default both match; /26 is longer, so it wins

  6. 6.Packet goes PC -> lab router -> lab server; reply with ttl=63

55.3 Windows route: real run (read-only)

PowerShell
routeroute print -4 127.*
Output
$ routeManipulates network routing tables.ROUTE [-f] [-p] [-4|-6] command [destination]                  [MASK netmask]  [gateway] [METRIC metric]  [IF interface]  command      One of these:                 PRINT     Prints  a route                 ADD       Adds    a route                 DELETE    Deletes a route                 CHANGE    Modifies an existing route$ route print -4 127.*IPv4 Route TableActive Routes:Network Destination        Netmask          Gateway       Interface  Metric        127.0.0.0        255.0.0.0         On-link         127.0.0.1    331        127.0.0.1  255.255.255.255         On-link         127.0.0.1    331  127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
  • Plain route prints the help: PRINT, ADD, DELETE, CHANGE.
  • print -4 127.* shows only the IPv4 loopback rows.
  • On-link = destination reachable directly, no gateway needed.
  • On an office PC, only print: never change routes there.

55.4 Windows route: commands and options

Output
route | route /?    helproute print          IPv4 + IPv6 + iface listroute print -4 | -6  only IPv4 / IPv6 tableroute print 10.*     pattern: * string, ? charroute add 10.41.0.0 mask 255.255.0.0 10.27.0.1                     network route via gatewayroute add 0.0.0.0 mask 0.0.0.0 192.168.1.1                     default routeroute add 3ffe::/32 3ffe::1   IPv6 routeroute change 10.41.0.0 mask 255.255.0.0  10.27.0.25         change gateway / metricroute delete 10.41.0.0 mask 255.255.0.0route delete 10.*    delete by patternroute delete 3ffe::/32   delete IPv6 routemask default 255.255.255.255, admin prompt
  • In add, MASK comes after the destination, then the gateway; without a mask the default 255.255.255.255 makes a host route.
  • change updates the gateway or metric of an existing route; delete can leave out the gateway.
  • * and ? patterns work only with print and delete.
Output
-p add ...       survives a reboot-p print         list persistent routes-f               clear all gateway routes-f add 0.0.0.0 mask 0.0.0.0 192.168.1.1                 clear first, then add-4 print         force IPv4-6 print         force IPv6metric 7         cost 1-9999, lowest winsif 0x3           interface index (route print)route add 157.0.0.0 mask 255.0.0.0 157.55.80.1  metric 3 if 2  full formregistry: Tcpip\Parameters\PersistentRoutes
  • -p with add = persistent: saved in the registry, survives a reboot.
  • -f clears all gateway routes: never try it on an office PC.
  • metric is a cost 1-9999 (lowest wins); IF is the interface index from route print's interface list.

55.5 Linux route (net-tools): display

Terminal
routeroute -nroute -e -nroute -6 -n
Output
$ routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Ifacedefault         home-router     0.0.0.0         UG    0      0        0 eth0192.168.75.0    0.0.0.0         255.255.255.192 U     0      0        0 eth0$ route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         192.168.75.1    0.0.0.0         UG    0      0        0 eth0192.168.75.0    0.0.0.0         255.255.255.192 U     0      0        0 eth0$ route -e -nKernel IP routing tableDestination     Gateway         Genmask         Flags   MSS Window  irtt Iface0.0.0.0         192.168.75.1    0.0.0.0         UG        0 0          0 eth0192.168.75.0    0.0.0.0         255.255.255.192 U         0 0          0 eth0$ route -6 -nKernel IPv6 routing tableDestination                    Next Hop                   Flag Met Ref  Use Iffe80::/64                      ::                         U    256 1      0 eth0::1/128                        ::                         Un   0   2      0 loff00::/8                       ::                         U    256 7      0 eth0
  • Plain route resolves the gateway name from the hosts file (home-router); -n is numeric and fast.
  • Flags: U = up, G = gateway.
  • -e adds MSS, Window and irtt columns; -6 shows the IPv6 table.
Output
route  |  route -n     table, numeric (fast)-e                     netstat format-ee                    all columns (long)-F                     FIB table (default)-C                     routing cache (empty)-A inet6  |  -6        IPv6 table-A inet   |  -4        IPv4 table-v                     verbose-V  --version          net-tools 2.10-h  --help             help + address familiesflags: U up  G gateway  H host  ! reject       R reinstate  D dynamic  M modified       A addrconf  C cache
  • -C (routing cache) is empty on a modern kernel: the lab printed only the header.
  • R, D and M flags are usually set by routing daemons.

55.6 Linux route: add a static route and a backup

Terminal
ping -c 2 -W 1 192.168.75.130route -v add -net 192.168.75.128/26 \  gw 192.168.75.2ping -c 2 192.168.75.130route add -net 192.168.75.128 \  netmask 255.255.255.192 \  gw 192.168.75.1 metric 50route -nip route get 192.168.75.130
Output
$ ping -c 2 -W 1 192.168.75.130From 192.168.75.1 icmp_seq=1 Destination Net UnreachableFrom 192.168.75.1 icmp_seq=2 Destination Net Unreachable2 packets transmitted, 0 received, +2 errors, 100% packet loss$ route -v add -net 192.168.75.128/26 \  gw 192.168.75.2$ ping -c 2 192.168.75.13064 bytes from 192.168.75.130: icmp_seq=1 ttl=63 time=0.128 ms64 bytes from 192.168.75.130: icmp_seq=2 ttl=63 time=0.092 ms2 packets transmitted, 2 received, 0% packet loss$ route -nDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         192.168.75.1    0.0.0.0         UG    0      0        0 eth0192.168.75.0    0.0.0.0         255.255.255.192 U     0      0        0 eth0192.168.75.128  192.168.75.2    255.255.255.192 UG    0      0        0 eth0192.168.75.128  192.168.75.1    255.255.255.192 UG    50     0        0 eth0$ ip route get 192.168.75.130192.168.75.130 via 192.168.75.2 dev eth0 src 192.168.75.10 uid 0
  • Before the route: the home router answers Destination Net Unreachable.
  • route add -net ... gw 192.168.75.2 fixes it; ttl=63 shows one router in between.
  • A backup route to the same /26 via the home router with metric 50; ip route get confirms the metric 0 route via the lab router is used.

55.7 Host route, reject and mss / window / irtt

Terminal
route add -host 192.168.75.140 \  gw 192.168.75.2route add -net 192.168.75.192/26 rejectping -c 1 -W 1 192.168.75.200route add -host 192.168.75.131 \  gw 192.168.75.2 mss 1400 \  window 4096 irtt 500route -ee -n
Output
$ route -nDestination     Gateway         Genmask         Flags Metric Ref    Use Iface192.168.75.128  192.168.75.2    255.255.255.192 UG    0      0        0 eth0192.168.75.140  192.168.75.2    255.255.255.255 UGH   0      0        0 eth0192.168.75.192  -               255.255.255.192 !     0      -        0 -$ ping -c 1 -W 1 192.168.75.200ping: connect: Host is unreachable$ route -ee -nDestination     Gateway         Genmask         Flags Metric Ref    Use Iface    MSS   Window irtt192.168.75.131  192.168.75.2    255.255.255.255 UGH   0      0        0 eth0     1400  4096   500192.168.75.140  192.168.75.2    255.255.255.255 UGH   0      0        0 eth0     0     0      0
  • -host creates a route for one IP: flags UGH, mask 255.255.255.255.
  • reject is a blocking route, not a firewall: ping does not even send the packet, it gets Host is unreachable. Flag !.
  • mss 1400 / window / irtt appear at the end of route -ee's long line.

55.8 route del and the default route

Terminal
route del -host 192.168.75.140 \  gw 192.168.75.2route del -net 192.168.75.128/26 metric 50route del defaultping -c 1 -W 1 8.8.8.8route add default gw 192.168.75.1 eth0route -n
Output
$ route del default$ ping -c 1 -W 1 8.8.8.8ping: connect: Network unreachable$ route add default gw 192.168.75.1 eth0$ route -nDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         192.168.75.1    0.0.0.0         UG    0      0        0 eth0192.168.75.0    0.0.0.0         255.255.255.192 U     0      0        0 eth0192.168.75.128  192.168.75.2    255.255.255.192 UG    0      0        0 eth0
  • del -host / del -net need the gateway or metric so the right line matches.
  • After route del default, ping 8.8.8.8 gets Network unreachable: no path outside the LAN.
  • route add default gw 192.168.75.1 eth0 brings it back.
Output
add -net 192.168.2.0 netmask 255.255.255.0add -net 192.168.2.0/24 gw 192.168.1.1add -host 203.0.113.5 gw 192.168.1.1add default gw 192.168.1.1dev eth0  |  eth0 (last)   force devicemetric 100       cost (IPv4 default 0)mss 1400         route MTUwindow 4096      TCP window (AX.25)irtt 500         initial RTT ms (300)reject           blocking route, no firewallmod  dyn  reinstate   flags M D Radd -net 224.0.0.0 netmask 240.0.0.0 dev eth0-6 add 2001:db8::/48 metric 1 dev eth0del default | del -net ... | del -host ...
  • The order of modifiers does not matter; if dev is last you can write just the interface name.
  • mod, dyn and reinstate are diagnostic flags the modern lab kernel did not show.

55.9 macOS route

macOS route could not be run here; these tables come from the PDF and the man page (no output shown).

Output
-d   debug, table not changed-n   numeric, no name lookup-t   test-only (/dev/null)-v   verbose      -q   quiet-- commands --route add default 192.168.1.1route add -net 10.10.0.0/16 192.168.1.254route add -host 203.0.113.5 192.168.1.254route delete -net 10.10.0.0/16route change default 192.168.1.2route get 8.8.8.8      gateway + interfaceroute -n flush  [-inet | -inet6]flush -osi | -xns | -atalk   old familiesroute -n monitor       changes live
  • -d (debug) and -t (test only) do not change the table.
  • No gw keyword: the gateway simply follows the destination.
  • route get shows the gateway and interface for a destination; flush deletes all gateway routes; monitor shows changes live.
Output
-net  -host          network or single host-netmask 255.255.0.0  explicit mask-inet6  -prefixlen 32   IPv6, prefix length-interface en0        direct, no gateway-ifscope en0          bind to interface-link en0:0.11.22.33.44.55   link address-ifp en1  -ifa 192.168.1.10  choose iface-proxy                published (proxy) ARP-- route flags ---static  -nostatic    manual or daemon-reject               ICMP unreachable-blackhole            silently discard-cloning  -xresolve   clone, external lookup-iface  -llinfo       direct, link info-proto1  -proto2      protocol flags
  • -reject sends ICMP unreachable; -blackhole silently drops the packet.
  • -interface / -ifscope bind the route to one interface.
Output
-mtu 1400  -hopcount 3     path MTU, hops-rtt 200  -rttvar 50       RTT estimate-sendpipe  -recvpipe 65536   buffers-ssthresh 32768   slow-start threshold-expire 3600      route expiry-lock -mtu 1400   lock one metric-lockrest ...     lock all that follow-- route -n monitor filters ---ifindex en0      only that interface-host  -gateway  -static  -local-reject  -blackhole  -cloning-llinfo  -router  -wascloned-add  -delete  -newmaddr-nollinfo         no = exclude
  • Transport metrics are initial TCP values (MTU, RTT, buffers); -lock stops protocols changing them.
  • Monitor filters show only chosen messages; a no prefix excludes them (useful to cut noise while a VPN connects).

55.10 Comparison: route on three OSes

TaskWindowsLinux (net-tools)macOS
Show tableroute printroute -nnetstat -rn
Add network routeroute add 10.41.0.0 mask 255.255.0.0 10.27.0.1route add -net 192.168.2.0/24 gw 192.168.1.1route add -net 10.10.0.0/16 192.168.1.254
Default routeroute add 0.0.0.0 mask 0.0.0.0 192.168.1.1route add default gw 192.168.1.1route add default 192.168.1.1
Which route is usedroute printip route get <ip>route get <ip>
Persistent-p (registry)netplan / NetworkManager configNot by route

55.11 Common mistakes

  • Thinking route add is forever: on Windows without -p it is gone after reboot; on Linux it lives only in the current kernel table.
  • Permanent routes: -p on Windows, netplan or NetworkManager config on Linux.
  • Thinking the lower metric always wins: metric is compared only between lines with the same prefix.
  • Running route -f or deleting the default route on a remote/office machine: you lose the path out.

55.12 Interview tips

  • Next hop choice: match the destination against every line; the longest matching prefix wins.
  • Same prefix: the lower metric wins.
  • Gateway 0.0.0.0 or On-link = directly connected subnet; no match at all = Network unreachable.
  • Flags to know: U up, G gateway, H host, ! reject.
  • Example: 0.0.0.0/0 via home router + 192.168.75.128/26 via lab router; 192.168.75.130 goes to the lab router.