tips
90 tips Network and DNS
One command, setting or habit at a time about Network and DNS: what it does, the line to copy and a caveat or two.
Network and DNS 90
-
List every process that is listening on a TCP port.
sudo lsof -nP -iTCP -sTCP:LISTEN -
Open a saved packet capture in tcpdump and filter it with the usual expressions
tcpdump -nn -r capture.pcap port 53 -
Prints a summary line for each packet Nmap sends or receives during a scan.
nmap --packet-trace -p 22,80 192.0.2.10 -
Saves scan results as one line per host, so grep and cut can pick out open ports.
nmap -p 22,80,443 -oG scan.gnmap 192.0.2.0/24 -
Generates a random preshared key that adds a symmetric layer on top of a WireGuard peer's key pair.
wg genpsk > peer1.psk -
Tells wg-quick to bring the tunnel up without creating any routes, so you add your own.
Table = off -
Prints the forwarding table of a Linux bridge, so you can see where a MAC address lives.
bridge fdb show br br0 -
Fix a reserved address that your router never hands to an Ubuntu server.
dhcp-identifier: mac -
Scan the UDP side of a host, where DNS, NTP and similar services listen.
sudo nmap -sU -p 53,67,123,161 192.0.2.10 -
Preview what a target range expands to and which names reverse DNS gives.
nmap -sL 192.0.2.0/28 -
See which reply, or missing reply, led nmap to each port state.
sudo nmap --reason -p 22,443,5432 192.0.2.10 -
Ask the local network for a DHCP offer and see who replies.
sudo nmap --script broadcast-dhcp-discover -
Trade coverage for speed by checking the most frequently used ports first.
nmap --top-ports 20 192.0.2.0/24 -
Start a WireGuard interface automatically at boot with the unit that ships with wg-quick.
sudo systemctl enable --now wg-quick@wg0 -
Match device names like en0 or en5 to Wi-Fi, Ethernet and Thunderbolt ports.
networksetup -listallhardwareports -
Add or change a WireGuard peer without restarting the interface and cutting everyone off.
sudo systemctl reload wg-quick@wg0 -
Understand why traffic goes missing when a WireGuard peer's AllowedIPs list is too narrow.
AllowedIPs = 198.51.100.2/32, 192.0.2.0/24 -
Check that a site answers on both address families, not just the one your machine prefers.
curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ -
See address, subnet, router and DHCP or manual setup for one network service.
networksetup -getinfo Wi-Fi -
Log each missing reply with a timestamp, so short outages become visible.
ping -O -D 192.0.2.1 -
See which subnet a Docker network uses and which address each container got.
docker network inspect $(docker network ls -q) \ -
Try a smaller or larger MTU on one interface without editing any network config.
ip link set dev eth0 mtu 1400 -
Make a fast link behave like a slow or lossy one to see how your app copes.
tc qdisc add dev eth0 root netem delay 100ms -
Send both the request and the name lookup through a SOCKS proxy, so DNS does not leak.
curl -x socks5h://localhost:1080 https://intranet.example.com/ -
Check when each WireGuard peer last completed a handshake to see if the tunnel works.
sudo wg show wg0 -
Find which process listens on a local socket file, like the Docker or database socket.
ss -xlp -
Let one curl command resolve names through a DoH server instead of the system resolver.
curl --doh-url https://dns.quad9.net/dns-query \ -
Get the local IP address of one interface as a single line, ready for scripts.
ipconfig getifaddr en0 -
Stop a WireGuard tunnel from going deaf when the router forgets the UDP mapping.
PersistentKeepalive = 25 -
Create a WireGuard key pair without leaving the private key readable for other users.
umask 077 -
Make the port light blink so you know which physical socket belongs to eth1.
ethtool -p eth1 10 -
Run a command with its own interfaces and routes, apart from the rest of the system.
ip netns add test1 -
Shorten the wait when the first name server in resolv.conf is down.
options timeout:2 attempts:2 rotate -
Read the card's own statistics to spot CRC errors, missed packets and other link trouble.
ethtool -S eth0 | grep -iE 'err|drop|miss|crc' -
See whether a connection is waiting on keepalive, retransmits or a closing timer.
ss -tno state established -
See the active system proxy configuration when some apps cannot reach the internet.
scutil --proxy -
Trace the route to a web server with packets that a firewall lets through.
traceroute -T -p 443 -n example.com -
Find out which driver and firmware a network card uses before you search for a bug.
ethtool -i eth0 -
Route a single network via a different router without touching your config files.
ip route add 198.51.100.0/24 via 192.0.2.1 -
Expose a service on another port or address for a quick test, no proxy config needed.
socat TCP-LISTEN:8080,fork,reuseaddr TCP:127.0.0.1:3000 -
Find out which router and which network interface carry your Mac's traffic.
route -n get default -
Make programs try the IPv4 address first when a host has both.
precedence ::ffff:0:0/96 100 -
Prints totals of open sockets per protocol and TCP state in a few lines.
ss -s -
Prints network information as JSON so a script can read an address reliably.
ip -j addr show eth0 | jq -r '.[0].addr_info[] | select(.family=="inet") | .local' -
Records packets to a file with a filter and a packet limit for later analysis.
sudo tcpdump -i any -n -c 500 -w /tmp/capture.pcap host 192.168.1.50 and port 443 -
Shows the order in which Linux asks /etc/hosts, DNS and other sources for a name.
grep ^hosts /etc/nsswitch.conf -
Lists the records systemd-resolved has cached, grouped by network scope.
resolvectl show-cache -
Raises systemd-resolved's log level on the fly so lookups show up in the journal.
sudo resolvectl log-level debug -
Shows what systemd-resolved knows about each DNS server and makes it probe again.
resolvectl show-server-state -
Prints resolvectl lookup results without headers, so scripts get only the answers.
resolvectl query -4 --legend=no example.com -
Tells systemd-resolved to skip DNSSEC validation for one domain on one link.
sudo resolvectl nta eth0 example.com -
Makes NetworkManager use a random or per-network MAC address for a Wi-Fi connection.
nmcli connection modify "Cafe Wifi" 802-11-wireless.cloned-mac-address random -
Prints all DNS resolvers macOS uses, with their domains, interfaces and order.
scutil --dns -
Clears the cached DNS answers on a Mac with one command.
sudo killall -HUP mDNSResponder -
Queries the Mac's Directory Service cache for a host name or an IP address.
dscacheutil -q host -a name example.com -
Prints the address, router, DNS servers and lease time your Mac got from DHCP.
ipconfig getpacket en0 -
Lists every process with its live network traffic, one line per app.
nettop -P -d -t external -
Saves separate sets of network settings and switches between them in one step.
networksetup -listlocations -
Decides which connection your Mac tries first when Ethernet and Wi-Fi are both active.
networksetup -listnetworkserviceorder -
Decides per network whether your Mac shows a fixed, rotating or real Wi-Fi address.
System Settings > Wi-Fi > Details > Private Wi-Fi address -
Asks the DHCP server for a fresh address and settings without switching Wi-Fi off and on.
System Settings > Network > Wi-Fi > Details > TCP/IP > Renew DHCP Lease -
Keeps a saved Wi-Fi network on your Mac but stops it from connecting by itself.
System Settings > Wi-Fi > Known Networks > (…) > Auto-Join -
Analyzes a flaky Wi-Fi connection on your Mac and saves a report for support.
Option-click the Wi-Fi icon > Open Wireless Diagnostics -
Shows which devices on your local network advertise SSH, file sharing or web pages.
dns-sd -B _ssh._tcp -
Streams link, address and route changes live, so short network hiccups leave a trace.
ip -ts monitor label link address route -
Shows per-interface error and drop counters so you can spot a bad cable or port.
ip -s -s link show eth0 -
Shows round trip time, MTU and congestion window for each TCP connection to a host.
ss -tin dst 192.168.1.30 -
Checks whether another device on your LAN already answers to an IP address.
sudo arping -D -I eth0 -c 3 192.168.1.50 -
Adds an extra IP address to an interface right away, and the address disappears again after a reboot.
sudo ip addr add 192.0.2.10/24 dev eth0 -
Sends pings with the don't-fragment flag set, so you can find the largest packet that passes.
ping -M do -s 1472 -c 3 example.com -
Shows the negotiated speed, duplex mode and link status of a network interface.
sudo ethtool eth0 -
Asks the kernel which gateway, interface and source address it would pick for a destination, without sending anything.
ip route get 1.1.1.1 -
Traces the route to a host like traceroute and reports the path MTU, as a normal user.
tracepath -n example.com -
Makes curl send a request for a hostname to an IP address you choose, ignoring DNS.
curl -I --resolve example.com:443:203.0.113.10 https://example.com/ -
Prints one line per network interface with its state and IP addresses.
ip -br address -
Lists the devices that are online on a network without scanning their ports.
nmap -sn 192.168.1.0/24 -
Tests whether a TCP port accepts connections, without sending any data.
nc -zv example.com 443 -
Shows which program is using which network port, useful when a port is already taken.
-
Makes a Linux machine use the subnet routes that other tailnet devices advertise.
sudo tailscale set --accept-routes -
Shows the address a normal program gets, including hosts file entries that dig ignores.
getent hosts nas.local -
Empties the local DNS cache of systemd-resolved without restarting the service.
resolvectl flush-caches -
Resolves a name through systemd-resolved and shows which link, protocol and cache gave the answer.
resolvectl query example.com -
Captures a few DNS packets from the command line and prints them.
sudo tcpdump -ni any port 53 -c 20 -
Lists only the TCP connections in a chosen state that match a port, instead of every socket.
ss -tn state established '( dport = :443 )' -
Scans all TCP ports of your own server from outside and lists only the open ones.
nmap -Pn -p- --open 192.168.1.10 -
Keeps the DHCP address but replaces the DNS servers with your own.
network: {version: 2, ethernets: {enp3s0: {dhcp4: true, dhcp4-overrides: {use-dns: false}, nameservers: {addresses: [192.168.1.2]}}}} -
Parses the netplan YAML and reports errors without changing the network.
sudo netplan generate -
Shows the negotiated HTTP version and lets you force an older one for comparison.
curl -sI --http2 -o /dev/null -w '%{http_version}\n' https://example.com/ -
Forces a request to leave through a specific interface or source address.
curl --interface wg0 -s -o /dev/null -w '%{local_ip}\n' https://example.com/ -
Requests compressed content and shows from the headers whether the server provides it.
curl -s --compressed -D - -o /dev/null https://example.com/ | grep -i '^content-encoding'
Nothing in this filter yet.