Kiet Nguyen logo
NotesNotesResumeResume
© 2026 Kiet Nguyen
← All categories

12

Networking Basics

  • DNS fail vs connection refused vs timeout
  • What listens on a port
  • HTTP health with curl
ippingsscurlgetentdighostresolvectltraceroutetcpdumptsharkproxy

Must-know cold

  • ip -br a · ip r · ping -c 4 HOST
  • ss -tlnp · curl -I URL · curl -v --max-time 5 URL
  • Resolve: getent ahosts NAME · dig +short NAME · resolvectl query NAME
  • Capture: sudo tcpdump -ni any -c 20 port 22
  • Ladder: DNS → route/ping → port listen → TLS/HTTP → app

Failure vocabulary

Definition: DNS fail vs timeout vs connection refused vs HTTP/TLS errors.

SymptomTypical meaning
Could not resolve hostDNS / nss failure
Network unreachableRouting / interface down
Connection timed outFiltered, drop, or silent blackhole
Connection refusedHost answered with RST: nothing listening on that IP:port, or firewall REJECT
HTTP 5xxTransport (and TLS, if HTTPS) already OK; application/server error
SSL certificate problemTrust, name mismatch, expiry, or incomplete chain

Commands

ip

Definition: Show/configure interfaces, addresses, and routes (modern ifconfig replacement).

Option / objectArgumentMeaningExample
ip a / ip addr—Addresses on interfacesip a
ip -br a—Brief interface + addrip -br a
ip -br link—Brief link stateip -br link
ip r / ip route—Routing tableip r
ip route getADDRPath taken to ADDRip route get 1.1.1.1
ip neigh—ARP/neighbor tableip neigh
ip link setIF up / downAdmin link (needs root)sudo ip link set eth0 up
-4 / -6—IPv4 / IPv6 onlyip -4 a
-s—Statisticsip -s link

Flag combos

ComboMeaningExample
ip -br a; ip rAddr + default routeFirst look
ip route get 8.8.8.8Which IF/gatewayMulti-homed

ping

Definition: ICMP echo probe for basic reachability and latency.

OptionArgumentMeaningExample
(none)HOSTICMP echo (runs until Ctrl-C)ping 8.8.8.8
-cN HOSTSend N probes then stopping -c 4 host
-WSEC HOSTPer-probe timeout (Linux)ping -c 2 -W 2 host
-iSEC HOSTInterval between pingsping -i 0.5 host
-sBYTES HOSTICMP payload size (not total frame)ping -s 1472 host
-4 / -6HOSTForce familyping -4 host
-nHOSTNo DNS reverse lookupping -n host

Flag combos

ComboMeaningExample
ping -c 4 -W 2 HOSTFinite safe testScripts
NoteICMP may be blocked while TCP worksDon’t stop at ping fail

ss

Definition: Socket statistics: listening ports and connection states (ss replaces netstat).

OptionArgumentMeaningExample
(none)—Non-listening sockets (mostly established TCP) — not a summaryPrefer flags
-s—Summary countersss -s
-t—TCPss -t
-u—UDPss -u
-l—Listening onlyss -lt
-n—Numeric; no resolvess -tn
-p—Process (often needs root)ss -tlnp
-a—All (listen + established)ss -tan
-4 / -6—Address familyss -tln4
sport =:PORTFilter source portss -tlnp 'sport = :80'
dport =:PORTFilter dest portss -tan 'dport = :443'
-r—Resolve hostsss -tr

Flag combos

ComboMeaningExample
ss -tlnpListening TCP + processDefault
ss -ulnpListening UDP + processDNS etc.
ss -tanp | grep :8080State for portESTAB/TIME-WAIT
ss -tlnp | grep 127.0.0.1Localhost-only binds“Works locally only”

curl

Definition: Transfer data from URLs; inspect HTTP(S) status, headers, and bodies.

OptionArgumentMeaningExample
(none)URLGET body to stdoutcurl URL
-I / --headURLSend HTTP HEAD (not a GET; some apps 405 / differ)curl -I https://x
-vURLVerbose (DNS, TLS, headers)curl -v URL
-sURLSilent (no progress)curl -s URL
-SURLShow errors even with -scurl -sS URL
-oFILE URLWrite body to FILEcurl -o f URL
-OURLSave as remote filenamecurl -O URL
-LURLFollow redirectscurl -L URL
-kURLInsecure TLS (skip verify)curl -k https://...
-m / --max-timeSEC URLWhole operation timeoutcurl -m 5 URL
--connect-timeoutSEC URLConnect phase timeoutcurl --connect-timeout 3 URL
-uUSER:PASS URLBasic authcurl -u u:p URL
-HHEADER URLAdd headercurl -H 'Auth: t' URL
-XMETHOD URLHTTP methodcurl -X POST URL
-dDATA URLRequest body; implies POST unless -X / -I / --headcurl -d 'a=1' URL
-x / --proxyPROXY URLUse proxycurl -x http://proxy:8080 URL
-wFORMATWrite-out metricscurl -w '%{http_code}\n' -o /dev/null -s URL
-fURLExit ≠0 on HTTP 4xx/5xx (not on 3xx)curl -f URL

Flag combos

ComboMeaningExample
curl -sS -I -m 5 URLQuick health
curl -v --max-time 10 URLFull debug
curl -sS URL | python3 -m json.toolPretty JSON
curl -sS -o /dev/null -w '%{http_code}\n' URLStatus only

DNS & name resolution

Definition: What apps resolve (getaddrinfo / NSS) is not always what dig asks a nameserver. Split-horizon and systemd-resolved stubs make this the first SSH/curl “could not resolve host” check.

FileRole
/etc/nsswitch.confhosts: order (files dns resolve myhostname …)
/etc/hostsStatic names (often first in NSS)
/etc/resolv.confStub resolver list. With systemd-resolved this is often 127.0.0.53, not the upstream

getent

Definition: Query Name Service Switch databases (getent(1)). ahosts calls getaddrinfo(3) — the path curl, ssh, and glibc apps use.

CommandArgumentMeaningExample
getent ahostsNAMEgetaddrinfo / AF_UNSPEC (A + AAAA as the app sees it)getent ahosts api.local
getent ahostsv4 / ahostsv6NAMEIPv4-only / IPv6-only getaddrinfogetent ahostsv4 host
getent hostsNAMEgethostbyname2 — may hide AAAAgetent hosts api.local
getent servicesNAME/etc/services port namesgetent services ssh

Exit 2 = key not found (getent(1)). Prefer ahosts for “will ssh/curl resolve this?”

dig

Definition: BIND 9 DNS lookup (dig(1)). Talks to nameservers listed in /etc/resolv.conf unless @server is given. Default type is A. Package: bind9-dnsutils / bind-utils. No args → NS query for . (root).

FormMeaningExample
dig NAMEA record via default resolversdig example.com
dig NAME AAAAIPv6 addressdig example.com AAAA
dig +short NAMEAnswer RDATA onlydig +short example.com
dig @SERVER NAMEQuery that server, not resolv.confdig @8.8.8.8 example.com
dig -x ADDRReverse PTR (in-addr.arpa / ip6.arpa)dig -x 192.0.2.1
dig +trace NAMEIterative from the root (sets +dnssec)Slow; good for delegation
dig +norecurse NAME @AUTHRD bit off — what an authoritative server has
dig NAME MX / NS / TXT / CNAMEOther typesdig example.com MX
dig -p PORTNon-53 (DoT is +tls, default port 853)Test a hidden master

dig does not walk NSS/hosts. A name in /etc/hosts can still fail in dig and succeed in getent ahosts.

host

Definition: Shorter BIND lookup (host(1)). Same package family as dig. Default is A+AAAA+MX style output.

FormMeaningExample
host NAMEAddress lookuphost example.com
host -t TYPE NAMEOne RR typehost -t AAAA example.com
host -a NAMEANY (verbose)
host ADDRReverse lookuphost 192.0.2.1

resolvectl

Definition: systemd-resolved client (resolvectl(1)). Use when /etc/resolv.conf points at 127.0.0.53. Default query returns A and AAAA.

CommandMeaningExample
resolvectl query NAMEResolve (cache / LLMNR / mDNS / DNS)resolvectl query api.local
resolvectl statusGlobal + per-link DNS servers, DNSSEC, DoTFirst look on Ubuntu
resolvectl dnsShow (or set, with a link) DNS servers
resolvectl flush-cachesDrop the local RR cacheAfter a zone change
-t TYPE query NAMERaw RR (A, AAAA, MX, …); FQDN, no search listresolvectl -t MX query example.com
-4 / -6Only that family

resolvectl is not installed’s answer if you are on a chroot/nscd box without resolved — then getent + dig @upstream.

Flag combos

ComboMeaning
getent ahosts HOST; ping -c1 HOSTApp path, then L3
dig +short HOST; dig +short HOST AAAAv4 and v6 from DNS only
resolvectl status; resolvectl query HOSTStub-resolver RCA

traceroute

Definition: Show network path hops toward a destination. Optional package.

CommandArgumentMeaningExample
tracerouteHOSTPath hops (Linux default is often UDP, not ICMP)traceroute host
traceroute -IHOSTICMP echo probestraceroute -I host
traceroute -nHOSTNo reverse DNStraceroute -n host
tracepathHOSTOften no root neededtracepath host
mtrHOSTLive loss/latency (if installed)mtr -r -c 10 host

Flag combos

ComboMeaningExample
traceroute -n HOSTFaster path view

Packet capture & analysis

Definition: Look at packets on the wire. tcpdump uses pcap-filter(7) (capture filter). tshark adds Wireshark display filters (-Y). SSH/TLS payloads are encrypted — you still see IP/TCP flags, ports, and retransmits. Capture needs root or CAP_NET_RAW / CAP_NET_ADMIN.

tcpdump

Definition: Dump packets matching a Boolean expression (tcpdump(1), libpcap). Default snaplen is 262144 bytes (-s 0 means that default). Interrupt with Ctrl-C.

OptionArgumentMeaningExample
-iIF / anyInterface. Linux any = all regular IF, not promiscuoussudo tcpdump -i any
-n—Do not convert addresses or ports to names (tcpdump(1) -n)Always for RCA
-cNExit after N packetssudo tcpdump -c 20 -ni any
-wFILEWrite pcap (raw), not text. Later -rsudo tcpdump -w ssh.pcap port 22
-rFILERead a savefile (no extra privilege)tcpdump -nr ssh.pcap
-v / -vv—More IP/TCP fields; with -w, a 1 Hz count on stderr
-e—Link-layer header (MACs)ARP / wrong VLAN
-l—Line-buffered stdout (pipes)… | grep …
-p—Don’t request promiscuous
-sSNAPLENBytes per packet; 0 = default 262144Don’t need -s0 on modern tcpdump
-X—Payload hex+ASCII (useless for SSH/TLS crypto)HTTP/DNS plaintext
-D—List capture interfaces

Expression (quote it; see pcap-filter(7)). Put -- before the expression if it could look like an option:

FilterMeaning
host 10.0.0.5To or from that address
port 22TCP or UDP 22
tcp port 22SSH TCP only
src host 10.0.0.5 and port 53DNS from that host
icmp / arpThose protocols
tcp[tcpflags] & (tcp-syn|tcp-fin) != 0SYN or FIN

SSH RCA: SYN with no SYN-ACK → filter/drop (timeout). SYN then RST → refused. Handshake then app data you cannot read → crypto is working; debug ssh -vv instead.

Flag combos

ComboMeaning
sudo tcpdump -ni any -c 30 port 22SSH TCP on all IF, numeric, stop at 30
sudo tcpdump -ni any -c 20 port 53DNS (UDP/TCP 53)
sudo tcpdump -w /tmp/n.pcap -ni eth0 host 10.0.0.5Save for Wireshark
tcpdump -nr /tmp/n.pcap 'tcp port 22'Read back, no root

tshark

Definition: Wireshark CLI (tshark(1)). Optional package. Capture filter -f is pcap-filter (same as tcpdump). Display filter -Y is Wireshark syntax (ssh, dns, tcp.port == 22). Default write format is pcapng.

OptionMeaningExample
-i IF / -DCapture IF / list IFsudo tshark -i any
-f 'FILTER'Capture filter (pcap)sudo tshark -f 'port 22'
-Y 'FILTER'Display filter (after dissect)tshark -r f.pcap -Y ssh
-w FILERaw packets (not text). Text = redirect stdout, not -wsudo tshark -w n.pcapng
-r FILERead capturetshark -r n.pcapng
-VFull protocol treeHeavy
-T fields -e FOne column per fieldtshark -T fields -e ip.src -e tcp.dstport
-q -z conv,tcpTCP conversations after the fileStats, not a packet list

Without -r/-i, tshark picks the first non-loopback IF — set -i explicitly.

proxy

Definition: Environment variables that force HTTP(S) clients through a proxy.

VariableArgumentMeaningExample
http_proxy / https_proxyURLProxy for HTTP(S) clients (not ping/ssh). Uppercase variants tooexport https_proxy=http://proxy:8080
no_proxy / NO_PROXYLISTBypass list (matching rules vary by client)export no_proxy=localhost,127.0.0.1,.corp

Common recipes

GoalCommand
My IPsip -br a
Default routeip r
Reach hostping -c 4 HOST
Listening portsss -tlnp
Who owns :8080sudo ss -tlnp | grep :8080
HTTP headerscurl -I -m 5 URL
Verbose HTTPcurl -v --max-time 10 URL
Resolve (app path)getent ahosts NAME
Resolve (DNS only)dig +short NAME · dig +short NAME AAAA
Who is the stub?resolvectl status (if resolv.conf is 127.0.0.53)
Capture SSHsudo tcpdump -ni any -c 30 port 22
Capture DNSsudo tcpdump -ni any -c 20 port 53
Save pcapsudo tcpdump -w /tmp/n.pcap -ni eth0 host HOST
JSON APIcurl -sS URL | python3 -m json.tool

Pitfalls

  • Ping fail ≠ service down (ICMP blocked). Linux traceroute is often UDP; hops that “* * *” may still pass TCP.
  • 127.0.0.1 / ::1 bind → remote clients fail; ss the listen address (0.0.0.0 vs 127.0.0.1 vs [::]).
  • curl without -m / --connect-timeout can hang forever.
  • curl -I is HEAD, not GET — a 200 on HEAD does not prove GET works.
  • Corporate HTTP(S) clients need https_proxy / no_proxy. They do not apply to ping or ssh.
  • ss -p hides other users’ processes without root.
  • DNS: /etc/hosts is consulted per nsswitch. On systemd-resolved, /etc/resolv.conf is often 127.0.0.53 (stub), not the real upstream — use resolvectl status / query.
  • getent hosts ≠ getaddrinfo. Prefer getent ahosts for “what ssh/curl will see.” dig bypasses NSS entirely.
  • dig with no args queries NS for . — not a bug, just BIND’s default.
  • tcpdump -n skips both host and port name lookup (current tcpdump(1)). Quote filters; & | () are shell metacharacters.
  • Linux -i any is not promiscuous and is not every libpcap device. Live capture needs root / CAP_NET_RAW. -w is raw pcap, not text.
  • SSH/TLS capture shows TCP (SYN/RST/timeout), not passwords or shell. Use ssh -vv for auth.
  • tshark -f is pcap-filter; tshark -Y is Wireshark display filter. -w is still raw packets.
  • Off-subnet traffic follows the longest matching route, not always the default gateway.

Addressing

Two devices talk on-link only if they share the same network prefix under the subnet mask (and can ARP/NDP). Otherwise the packet follows the longest matching route — often the default gateway, not always.

PieceQuestion
IP addressWho am I?
Subnet mask / CIDRWhat is local to me?
Default gatewayWhere do I send everything else?

Common CIDR sizes

CIDRMaskTotalUsableTypical use
/32255.255.255.25511Single host route
/31255.255.255.25422P2P (RFC 3021; both usable)
/30255.255.255.25242Classic point-to-point
/29255.255.255.24886Tiny segment
/24255.255.255.0256254Standard LAN
/16255.255.0.065,53665,534Large campus
/8255.0.0.016,777,216~16.7MHuge (rare)

For ordinary LAN prefixes, network (host bits 0) and broadcast (host bits 1) are reserved — a /24 has 254 usable hosts, not 256. Exceptions: /32 (1 address) and /31 (RFC 3021 point-to-point, both usable).

Worked /26: 192.168.1.10/26 → 64 addresses, network 192.168.1.0, broadcast 192.168.1.63, usable 192.168.1.1–192.168.1.62.

Private vs public

RangeNotes
10.0.0.0/8Large private
172.16.0.0/12Medium private (172.16–172.31)
192.168.0.0/16Small / home private
127.0.0.0/8Loopback (127.0.0.1 is one address in this range)
169.254.0.0/16IPv4 link-local (APIPA). Common after DHCP fail; also used on purpose

Isolate a reachability fail

ping -c 4 192.168.1.1    # gateway — local segment
ping -c 4 8.8.8.8        # public IP — routing, no DNS
ping -c 4 google.com     # hostname — DNS after routing

Socket & transport (L4)

VerbTool / syscallWhat it does
Socketsocket()Allocates a kernel socket FD (IPv4/IPv6/UNIX, TCP/UDP).
Bind (socket)bind()Associates a socket with a local IP:port. Not the same as ip addr add (that assigns an address on an interface).
Listenlisten()Passive TCP queue for incoming SYNs.
Connectconnect()TCP 3-way handshake (or UDP default peer).
Acceptaccept()Takes one established connection from the listen queue.
Send / receivesend(), recv()Copies payload between user buffers and kernel sk_buff.
Closeclose(), shutdown()FIN/RST tear-down; frees the socket.

Link & interface (L1/L2)

VerbToolWhat it does
Up / downip link set DEV up/downAdministrative NIC state.
Bridgeip link add type bridgeSoftware L2 switch (MAC learning).
Bondip link add type bondAggregate NICs (LACP / failover).
Veth pairip link add type vethVirtual wire between namespaces.
Promiscip link set DEV promisc onPass all frames to the kernel.

Routing (L3)

VerbToolWhat it does
Assignip addr add, DHCPAddress + prefix on an interface (L3). Separate from bind().
Routeip route addFIB entry: dest → nexthop.
Forwardsysctl net.ipv4.ip_forward=1Transit packets NIC-to-NIC.
Resolvegetent, dig, ARP/NDPName→IP or IP→MAC.

Filter & inspect

VerbToolWhat it does
Filternftables, iptablesMatch headers at Netfilter hooks.
Drop / rejectDROP, REJECTSilent discard vs ICMP/RST.
NATnft / iptables -t natRewrite addresses/ports (SNAT/DNAT).
ShapetcRate limits / qdiscs.
Capturetcpdump, tshark, WiresharkFrames via libpcap (pcap-filter).
Probeping, traceroute, ncPath, latency, port status.
Inspectss, ip -s linkSocket states and drop counters.

For more details, try man <command> in your terminal.

Previous11 Resources: Disk, Memory, CPUNext13 Users, Groups & sudo