ip (iproute2) (4/6)
The last everyday ip objects: ip route get/save/restore to inspect and back up routing decisions, ip rule for policy routing, ip neigh for the ARP/NDP cache, ip netns for isolated network stacks, ip tuntap, ip monitor, ip maddress and classic ip tunnel (GRE).
20.1 Lab and where it runs
- iproute2 is Linux only; Windows and macOS have no ip command.
- Docker LAN n020-lan: host-a 192.168.40.10, host-b 192.168.40.20, gateway 192.168.40.1 to the internet.
20.2 ip route get / flush / save / restore
- ip route get asks the kernel which route it would use, without sending a packet.
- fibmatch prints the full matching entry (here the default route); a directly connected host shows no via.
- Add selectors (ipproto tcp dport 443, from, iif, oif, mark, uid) to test one exact connection.
- save dumps a table to a binary file, flush deletes it (-s prints the count), showdump reads the file, restore brings routes back.
ip route get 8.8.8.8ip route get fibmatch 8.8.8.8ip route get 192.168.40.20ip route get 8.8.8.8 ipproto tcp dport 443$ ip route get 8.8.8.88.8.8.8 via 192.168.40.1 dev eth0 src 192.168.40.10 uid 0 cache$ ip route get fibmatch 8.8.8.8default via 192.168.40.1 dev eth0$ ip route get 192.168.40.20192.168.40.20 dev eth0 src 192.168.40.10 uid 0 cache$ ip route get 8.8.8.8 ipproto tcp dport 4438.8.8.8 via 192.168.40.1 dev eth0 src 192.168.40.10 uid 0 cacheip route show table 100ip route save table 100 > /tmp/t100.binip -s route flush table 100ip route show table 100ip route showdump < /tmp/t100.binip route restore < /tmp/t100.binip route show table 100$ ip route show table 100default via 192.168.40.20 dev eth0198.51.100.0/24 via 192.168.40.20 dev eth0$ ip route save table 100 > /tmp/t100.bin$ ip -s route flush table 100*** Round 1, deleting 2 entries ****** Flush is complete after 1 round ***$ ip route show table 100$ ip route showdump < /tmp/t100.bindefault via 192.168.40.20 dev eth0 table 100198.51.100.0/24 via 192.168.40.20 dev eth0 table 100$ ip route restore < /tmp/t100.bin$ ip route show table 100default via 192.168.40.20 dev eth0198.51.100.0/24 via 192.168.40.20 dev eth0flush dev eth0 delete the device's routesflush table 100 empty the whole tableflush cache flush the route cache-s route flush print the deleted countget IP from SRC lookup with a sourceget IP iif eth1 packet arrived on this deviceget IP oif eth0 force the output deviceget IP mark 0x10 with a firewall markget IP vrf NAME lookup in a VRFget IP uid 1000 with a user idget IP sport 5000 source portget IP tos 0x10 TOS valueget IP connected again with the preferred src20.3 ip rule (policy routing)
- Normally only the main table is used; with policy routing a list of rules is checked in priority order (lower pref first).
- Default rules: 0 local, 32766 main, 32767 default.
- Lab: second IP 192.168.40.50 on host-a; rule 1000 sends traffic from it to table 100, whose default route is host-b.
- ip route get ... from 192.168.40.50 shows via 192.168.40.20 table 100; tcpdump on host-b sees the echo request.
- Step 1
1.Packet from 192.168.40.50 to 8.8.8.8
- Step 2
2.Rule 0: lookup local, no match
- Step 3
3.Rule 1000: from 192.168.40.50 matches, lookup table 100
- Step 4
4.Table 100 default via 192.168.40.20 (host-b)
- Step 5
5.host-b forwards to gateway 192.168.40.1 and the internet
Traffic from 192.168.40.10 still uses the main table and goes straight to the gateway
ip rule add from 192.168.40.50 \ table 100 pref 1000ip rule showip route get 8.8.8.8ip route get 8.8.8.8 from 192.168.40.50ping -c 2 -W 1 -I 192.168.40.50 8.8.8.8# host-b:tcpdump -n -i eth0 -c 2 icmp$ ip rule show0: from all lookup local1000: from 192.168.40.50 lookup 10032766: from all lookup main32767: from all lookup default$ ip route get 8.8.8.88.8.8.8 via 192.168.40.1 dev eth0 src 192.168.40.10 uid 0$ ip route get 8.8.8.8 from 192.168.40.508.8.8.8 from 192.168.40.50 via 192.168.40.20 dev eth0 table 100 uid 0$ ping -c 2 -W 1 -I 192.168.40.50 8.8.8.864 bytes from 8.8.8.8: icmp_seq=1 ttl=63 time=59.5 ms64 bytes from 8.8.8.8: icmp_seq=2 ttl=63 time=22.2 ms2 packets transmitted, 2 received, 0% packet loss[host-b] $ tcpdump -n -i eth0 -c 2 icmp16:24:08.658610 IP 192.168.40.50 > 8.8.8.8: ICMP echo request, id 8348, seq 1, length 6416:24:08.658647 IP 192.168.40.50 > 8.8.8.8: ICMP echo request, id 8348, seq 1, length 642 packets captured- A rule can also act directly: prohibit (Permission denied), blackhole (Invalid argument), unreachable (Network unreachable).
- ip rule save/restore back up rules, but restore adds on top of existing rules and does not skip duplicates.
- Selectors: from, to, iif, oif, fwmark[/mask], uidrange, ipproto, dport; actions: table, goto, nat. Flush the route cache after changes.
ip rule add to 203.0.113.0/24 \ prohibit pref 900ip rule add to 198.51.100.0/24 \ blackhole pref 901ip rule add to 192.0.2.0/24 \ unreachable pref 902ip route get 203.0.113.5ip route get 198.51.100.5ip route get 192.0.2.5ip rule save > /tmp/rules.binip rule restore < /tmp/rules.bin$ ip rule show0: from all lookup local900: from all to 203.0.113.0/24 prohibit901: from all to 198.51.100.0/24 blackhole902: from all to 192.0.2.0/24 unreachable1000: from 192.168.40.50 lookup 10032766: from all lookup main$ ip route get 203.0.113.5RTNETLINK answers: Permission denied$ ip route get 198.51.100.5RTNETLINK answers: Invalid argument$ ip route get 192.0.2.5RTNETLINK answers: Network unreachable$ ip rule restore < /tmp/rules.bin; ip rule show999: from all lookup local1000: from 192.168.40.50 lookup 1001000: from 192.168.40.50 lookup 10032766: from all lookup main32766: from all lookup mainfrom / to PREFIX source, destinationnot invert the matchiif / oif NAME in or out devicetos / fwmark MARK TOS or firewall markfwmark MARK/MASK only masked bits countuidrange 1000-1999 user id rangeipproto sport dport protocol and portsl3mdev / tun_id VRF or tunnel idpref NUMBER lower number = checked firsttable / lookup ID which routing tableprotocol static who added the rulesuppress_prefixlength reject short prefixessuppress_ifgroup reject a device groupgoto / realms / nat jump, realm, old NATlist / flush show or remove allroute flush cache apply the rule change20.4 ip neighbour (ARP / NDP cache)
- Maps IP -> MAC; gateway and host-b show REACHABLE.
- nud permanent never expires; change updates the MAC; add on an existing entry = File exists, so use replace (add or change).
- proxy entries answer ARP for another IP.
- get shows host-b now STALE; flush removed 3 entries but the permanent one stayed (use ip neigh del).
ip neigh showip neigh add 192.168.40.99 \ lladdr 02:00:00:00:40:99 \ dev eth0 nud permanentip neigh change 192.168.40.99 \ lladdr 02:00:00:00:40:98 dev eth0ip neigh add 192.168.40.99 \ lladdr 02:00:00:00:40:97 dev eth0ip neigh replace 192.168.40.98 \ lladdr 02:00:00:00:40:96 dev eth0 nud staleip neigh add proxy 192.168.40.77 dev eth0$ ip neigh show192.168.40.20 dev eth0 lladdr ea:66:e8:eb:39:a1 REACHABLE192.168.40.1 dev eth0 lladdr e6:99:fd:2b:3c:5c REACHABLE$ ip neigh show 192.168.40.99192.168.40.99 dev eth0 lladdr 02:00:00:00:40:99 PERMANENT$ ip neigh show 192.168.40.99192.168.40.99 dev eth0 lladdr 02:00:00:00:40:98 PERMANENT$ ip neigh add 192.168.40.99 lladdr 02:00:00:00:40:97 dev eth0RTNETLINK answers: File existsip -br neighip neigh show nud permanentip neigh show proxyip neigh get 192.168.40.20 dev eth0ip -s neigh flush dev eth0ip neigh show nud allip neigh del 192.168.40.99 dev eth0$ ip -br neigh192.168.40.99 eth0 02:00:00:00:40:98192.168.40.20 eth0 ea:66:e8:eb:39:a1192.168.40.98 eth0 02:00:00:00:40:96192.168.40.1 eth0 e6:99:fd:2b:3c:5c$ ip neigh show nud permanent192.168.40.99 dev eth0 lladdr 02:00:00:00:40:98 PERMANENT$ ip neigh show proxy192.168.40.77 dev eth0 proxy$ ip neigh get 192.168.40.20 dev eth0192.168.40.20 dev eth0 lladdr ea:66:e8:eb:39:a1 STALE$ ip -s neigh flush dev eth0*** Round 1, deleting 3 entries ****** Flush is complete after 1 round ***$ ip neigh show nud all192.168.40.99 dev eth0 lladdr 02:00:00:00:40:98 PERMANENT| State | Meaning |
|---|---|
| PERMANENT | Always valid, only an admin removes it (survives flush) |
| NOARP | Valid, never revalidated |
| REACHABLE | Valid until the reachable timeout |
| STALE | Valid but not confirmed for a while (suspicious) |
| DELAY / PROBE | Validation delayed / probing now |
| INCOMPLETE | Not resolved yet |
| FAILED | Probes failed: MAC could not be resolved |
20.5 ip netns (network namespaces)
- Each namespace is a mini network stack with its own interfaces and routes.
- Namespace red joined to host-a with a veth pair; inside it only its own eth0 is visible and the ping reaches host-a.
- pids lists its processes, identify finds a process's namespace, delete removes it (kill its processes first).
- exec uses /etc/netns/NAME files (e.g. resolv.conf) for that namespace.
- Step 1
1.ip netns add red
- Step 2
2.veth: v-red stays in root ns, eth0 moves into red
- Step 3
3.v-red = 172.16.41.1/24, red eth0 = 172.16.41.2/24, set up
- Step 4
4.ip netns exec red ping 172.16.41.1: replies
- Step 5
5.ip netns delete red removes the stack
ip netns add redip link add v-red type veth \ peer name eth0 netns redip addr add 172.16.41.1/24 dev v-redip -n red addr add 172.16.41.2/24 dev eth0ip -n red link set eth0 upip netns exec red ip -br addrip netns exec red ping -c 2 172.16.41.1ip netns pids redip netns identify \ $(ip netns pids red | head -1)ip netns delete red$ ip netns add red; ip netns listred$ ip netns exec red ip -br addrlo UNKNOWN 127.0.0.1/8 ::1/128eth0@if10 UP 172.16.41.2/24$ ip netns exec red ping -c 2 172.16.41.164 bytes from 172.16.41.1: icmp_seq=1 ttl=64 time=0.264 ms64 bytes from 172.16.41.1: icmp_seq=2 ttl=64 time=0.178 ms2 packets transmitted, 2 received, 0% packet loss$ ip netns pids red408$ ip netns identify $(ip netns pids red | head -1)red$ ip netns delete red; ip netns list20.6 ip tuntap, ip monitor, ip maddress
- TUN = L3 device handing IP packets to a user-space program (OpenVPN); TAP = L2 device handing full Ethernet frames (VMs).
- ip monitor prints netlink events live; -ts adds timestamps, label adds [LINK]/[ADDR]/[ROUTE]/[NEIGH]; deletions show Deleted.
- ip maddress shows multicast groups an interface accepts (224.0.0.1 all hosts, ff02::1); add joins a link-layer group statically.
ip tuntap add dev tun0 mode tunip tuntap add dev tap0 mode tap multi_queueip tuntap showip -d link show tun0ip tuntap del dev tun0 mode tunip tuntap del dev tap0 mode tap multi_queue$ ip tuntap showtun0: tun persisttap0: tap multi_queue persist$ ip -d link show tun011: tun0: <POINTOPOINT,MULTICAST,NOARP> mtu 1500 qdisc noop state DOWN link/none promiscuity 0 allmulti 0 minmtu 68 maxmtu 65535 tun type tun pi off vnet_hdr off persist on$ ip tuntap del dev tun0 mode tun$ ip tuntap del dev tap0 mode tap multi_queue$ ip tuntap showtimeout 4 ip -ts monitor label \ link address neigh route &ip link add dummy0 type dummyip link set dummy0 upip addr add 172.16.42.1/24 dev dummy0ip neigh add 172.16.42.9 \ lladdr 02:00:00:00:42:09 dev dummy0ip link del dummy0[2026-11-08T16:25:24.888488] [LINK]13: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default[2026-11-08T16:25:24.891072] [LINK]13: dummy0: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default[2026-11-08T16:25:24.893346] [ADDR]13: dummy0 inet 172.16.42.1/24 scope global dummy0[2026-11-08T16:25:24.893509] [ROUTE]172.16.42.0/24 dev dummy0 proto kernel scope link src 172.16.42.1[2026-11-08T16:25:24.895848] [NEIGH]172.16.42.9 dev dummy0 lladdr 02:00:00:00:42:09 PERMANENT[2026-11-08T16:25:24.897948] [NEIGH]Deleted 172.16.42.9 dev dummy0 lladdr 02:00:00:00:42:09 PERMANENT[2026-11-08T16:25:24.898368] [ADDR]Deleted 13: dummy0 inet 172.16.42.1/24 scope global dummy0[2026-11-08T16:25:24.898455] [LINK]Deleted 13: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group defaultip maddress show dev eth0ip maddress add 01:00:5e:01:01:01 dev eth0ip maddress show dev eth0ip maddress del 01:00:5e:01:01:01 dev eth0$ ip maddress show dev eth09: eth0 link 33:33:00:00:00:01 link 01:00:5e:00:00:01 inet 224.0.0.1 inet6 ff02::1 inet6 ff01::1$ ip maddress add 01:00:5e:01:01:01 dev eth0$ ip maddress show dev eth09: eth0 link 33:33:00:00:00:01 link 01:00:5e:00:00:01 link 01:00:5e:01:01:01 static inet 224.0.0.1$ ip maddress del 01:00:5e:01:01:01 dev eth0netns attach NAME PID name a process's netnsnetns set NAME auto assign an nsidip -all netns exec CMD run in every netnsnetns monitor add/delete eventsnetns list-id nsid listexec NAME cmd uses /etc/netns/NAME filestuntap user / group set the ownertuntap pi / vnet_hdr extra headerstuntap one_queue single queuemonitor neigh / rule only these objectsmonitor dev eth0 only one devicemonitor all-nsid all namespacesrtmon file F record eventsmonitor file F replay a recordingip -t monitor timestamp on its own line20.7 ip tunnel (classic IP tunnels)
- A tunnel wraps one packet inside another; GRE puts the inner packet in an outer IP header 192.168.40.10 -> 192.168.40.20.
- tcpdump on host-b shows the outer header and, inside it, the ICMP echo request in plain text.
- change updates a tunnel (ttl 64); del removes it; key/csum/seq are GRE-only; nopmtudisc/ignore-df control fragmentation.
ip tunnel add gre1 mode gre \ remote 192.168.40.20 local 192.168.40.10 \ ttl 255ip addr add 172.16.40.1/30 dev gre1ip link set gre1 upip tunnel showping -c 2 172.16.40.2# host-b:tcpdump -n -i eth0 -c 2 proto greip tunnel change gre1 ttl 64ip tunnel del gre1$ ip tunnel showgre0: gre/ip remote any local any ttl inherit nopmtudiscgre1: gre/ip remote 192.168.40.20 local 192.168.40.10 ttl 255$ ping -c 2 172.16.40.264 bytes from 172.16.40.2: icmp_seq=1 ttl=64 time=0.322 ms64 bytes from 172.16.40.2: icmp_seq=2 ttl=64 time=4.76 ms[host-b] $ tcpdump -n -i eth0 -c 2 proto gre16:25:50.602640 IP 192.168.40.10 > 192.168.40.20: GREv0, length 88: IP 172.16.40.1 > 172.16.40.2: ICMP echo request, id 8351, seq 1, length 6416:25:50.602780 IP 192.168.40.20 > 192.168.40.10: GREv0, length 88: IP 172.16.40.2 > 172.16.40.1: ICMP echo reply, id 8351, seq 1, length 64$ ip tunnel change gre1 ttl 64; ip tunnel show gre1gre1: gre/ip remote 192.168.40.20 local 192.168.40.10 ttl 64$ ip tunnel del gre1| Mode | What goes inside what |
|---|---|
| ipip | IPv4 in IPv4 |
| gre | Any protocol in GRE over IPv4 (key, csum, seq) |
| sit / isatap | IPv6 in IPv4 |
| vti / vti6 | IPsec virtual tunnel |
| ip6ip6 / ipip6 / ip6gre | IPv6 tunnels (ip -6) |
20.8 Common mistakes
- Thinking a tunnel means encryption: GRE/IPIP only wrap; use IPsec or WireGuard for privacy.
- Testing policy routing with plain ip route get: add 'from SRC' or you only see the main table result.
- Running ip rule restore twice and getting duplicate rules.
- Expecting ip neigh flush to clear PERMANENT entries; deleting a netns while its processes still run.
20.9 Interview tips
- Policy routing: rules checked by priority choose a table by source, fwmark or iif; used for multiple ISP links, VPN split tunnelling, VRF.
- Two links, one source IP must use the second: ip rule add from IP table 100, verify with ip route get 8.8.8.8 from IP.
- ip route get shows the kernel decision without sending a packet.
- Neighbour states: REACHABLE -> STALE -> DELAY -> PROBE -> FAILED; PERMANENT survives flush.