← All cheatsheets

Network

Linux Networking & DNS — interfaces, routes, and name resolution

Read a Linux host's network state with iproute2 — interfaces, routes, ARP — then follow a name through the modern resolver stack (nsswitch → /etc/hosts → systemd-resolved → /etc/resolv.conf) so you fix the right layer when it breaks.

Docs-checkedlinuxnetworkingdnsiproutingresolv

Quick reference — reach for these first

addrip -br addrOne line per interface: name, admin state (UP/DOWN), and IPv4/IPv6 addresses. The fast 'what am I?' view.
routeip routeThe main routing table. The 'default via GW dev DEV' line is your gateway; no default line = no off-subnet traffic.
dnsresolvectl statusPer-link DNS servers and search domains as systemd-resolved sees them — the real resolver config, not just /etc/resolv.conf.
resolvegetent hosts HOSTResolves a name through nsswitch exactly as applications do (files + DNS). Empty output = resolution failed.
socketsss -tulpnListening TCP/UDP sockets with the owning process. Confirms what is bound to which address:port.

How a name resolves on modern Linux

How a name resolves on modern Linux An application calls getaddrinfo. glibc reads the hosts line of /etc/nsswitch.conf and tries each listed source left to right: files reads /etc/hosts; resolve asks systemd-resolved on the stub address 127.0.0.53; and dns reads nameservers from /etc/resolv.conf. The systemd-resolved path is highlighted because resolvectl status inspects it. The first source that returns an address wins. application getaddrinfo() /etc/nsswitch.conf hosts: order files · /etc/hosts (static entries) resolve · systemd-resolved stub 127.0.0.53 — resolvectl status dns · /etc/resolv.conf nameservers Sources are tried top-to-bottom in the hosts: order — the first that returns an address wins. getent hosts follows this; dig skips to DNS.

Text equivalent: Left to right: an application calls getaddrinfo, which consults the hosts: line of /etc/nsswitch.conf. From there three sources are tried in order: files reads static entries in /etc/hosts; resolve (highlighted) asks systemd-resolved at the stub address 127.0.0.53 and is what resolvectl status inspects; dns reads nameserver lines from /etc/resolv.conf. Sources are tried in the listed order and the first that returns an address wins. getent hosts follows this whole chain, while dig skips straight to DNS.

Common tasks

Task · Command · What to check

TaskCommandWhat to check
List interfaces + link stateip -br linkState UP and flag LOWER_UP = carrier/cable present. DOWN = admin-down; NO-CARRIER = no link.
Show addresses per interfaceip -br addrExpected IPv4/IPv6 on the right DEV. No inet line = interface has no address (DHCP failed or not configured).
Bring an interface upsudo ip link set DEV upRe-run ip -br link; state should flip to UP. (Not persistent across reboot.)
Show the routing tableip routeA 'default via GW dev DEV' line exists, plus a directly-connected subnet route for your LAN.
Which route to a destinationip route get 1.1.1.1Prints the chosen src address, gateway and dev. 'unreachable' = no matching route.
Show the ARP / neighbor tableip neighMAC per on-link IP with a state: REACHABLE/STALE good; FAILED/INCOMPLETE = no L2 answer (wrong subnet or host down).
Resolve a name like an app doesgetent hosts HOSTReturns IP + name via nsswitch. Empty/exit 2 = name resolution failed, even if the network is fine.
Query DNS directly (bypass nsswitch)dig +short HOSTAsks the servers in /etc/resolv.conf. An answer here but not from getent points at /etc/hosts or nsswitch, not DNS.
Inspect the active resolverresolvectl statusEach link lists its DNS Servers and search domain. Empty 'DNS Servers' = nothing to ask.
Check nsswitch resolution ordergrep '^hosts:' /etc/nsswitch.confTypically 'files resolve [!UNAVAIL=return] dns' or 'files dns'. Sources are tried left to right.
See the nameserver filecat /etc/resolv.confnameserver lines. '127.0.0.53' means queries go to the systemd-resolved stub, not straight to an upstream.
Show NetworkManager view of a linknmcli dev show DEVIP4.ADDRESS, IP4.GATEWAY, IP4.DNS and GENERAL.STATE in one place; good for 'is NM managing this?'.
List listening ports + processss -tulpnService is bound to 0.0.0.0/:: (all) vs 127.0.0.1 (local-only) on the port you expect.
Show firewall rulessudo nft list ruleset # or: sudo ufw statusConfirms whether the port/chain is accepted or dropped before you blame the app.

A host can ping by IP but not by name

DNS BREAK

1
Confirm the network is fine but names fail — ping a raw IP, then a name.
ping -c1 1.1.1.1 && ping -c1 HOST
# 1.1.1.1: 1 received, 0% loss   ·   ping: HOST: Name or service not known
2
Resolve the way applications do — getent walks nsswitch (files + DNS), so its failure means resolution, not the app.
getent hosts HOST
# (no output, exit status 2 → resolution failed)
3
Inspect the resolver systemd-resolved is actually using, per link.
resolvectl status
# Link 2 (eth0)
#   DNS Servers: (none)   ← nothing to query
4
Check what the stub file points at — 127.0.0.53 is the systemd-resolved stub, which here has no upstream, so queries dead-end.
cat /etc/resolv.conf
# nameserver 127.0.0.53   (stub; resolved itself has no upstream)
5
Give the link a working upstream DNS server (persist it in NetworkManager or /etc/systemd/resolved.conf for reboots).
sudo resolvectl dns eth0 1.1.1.1 1.0.0.1
# (set per-link DNS; resolvectl status now shows DNS Servers: 1.1.1.1 1.0.0.1)
✓
Re-resolve to confirm the fix — names resolve and ping by name works. Problem was the resolver, not connectivity or the app.
getent hosts HOST && ping -c1 HOST
# 203.0.113.10  HOST   →  1 received, 0% loss

Troubleshooting — symptom → first check → next check

SymptomFirst checkEstablishesNext check
Can ping by IP, not by namegetent hosts HOST (returns nothing)Connectivity is fine; name resolution is brokenInspect resolvectl status / /etc/resolv.conf and point the link at a working DNS server
"connect: Network is unreachable"ip route (no 'default via' line)No default gateway, so off-subnet traffic has nowhere to gosudo ip route add default via GW dev DEV (persist in NM/networkd)
"No route to host"ip route get IP; ip neighGateway/ARP not resolving or destination is off your subnet with no routeCheck DEV is UP, netmask is right, and the gateway is on your subnet
DNS not resolving (SERVFAIL / timeout)resolvectl query HOST; dig @SERVER HOSTResolver unreachable or misconfigured vs the record genuinely missingSet a reachable nameserver; verify the 127.0.0.53 stub (systemd-resolved) is running
/etc/resolv.conf keeps getting overwrittenls -l /etc/resolv.confIt's a symlink managed by systemd-resolved or NetworkManagerEdit resolved.conf or the NM connection — not the file; it is regenerated
Interface DOWN / has no IPip -br addr; ip -br linkNIC is admin-down, has no carrier, or DHCP never assigned an addressip link set DEV up, then renew the lease via nmcli/networkd/dhclient
Name resolves and route existsgetent hosts HOST && ip route get IPResolution and L3 path are both healthy end-to-endRecord this as your baseline before the next change

References

Modern Linux with iproute2 (ip, ss) and systemd (resolvectl, getent). Many desktops run NetworkManager (nmcli); servers may use systemd-networkd or ifupdown. Hosts without systemd-resolved edit /etc/resolv.conf directly instead of going through the 127.0.0.53 stub. nftables is the current firewall framework; iptables-nft and ufw are common front-ends. dig/host need dnsutils / bind-utils installed. HOST, DEV, GW, SERVER in CAPS are placeholders — substitute your own.