Networking Commands
7 pages · ~5 min✓ Reviewed
Builds on Processes and Signals.
Mental Model and Debug Order
1. Layers and Sockets
- IP on interfaceip
- Routegateway
- DNSdig
- Portss, curl
- Firewallallows it
7 pages · ~5 min✓ Reviewed
Builds on Processes and Signals.
32768-60999| Bind | Reachable from |
|---|---|
127.0.0.1 | local only |
0.0.0.0 | all IPv4 interfaces |
:: | all IPv6 interfaces |
Usually a loopback bind.
ip addrhave an IP?ip routedefault gateway?ping 8.8.8.8internet by IP?dig example.comDNS?curl -vservice answers?| Command | Shows |
|---|---|
ip -br a | one line per interface: address, UP/DOWN |
ip link | layer 2: state, MAC |
ip route | the routing table |
ip route get 8.8.8.8 | route and source IP a packet would use |
default via X dev eth0 = the gateway, used when no other route matchesss replaces netstat-tulpn = TCP, UDP, Listening, Process, Numericsudo ss -tulpn
ss -tn state established
ss -tn 'sport = :443'listeners, live connections, filter by port
tcp LISTEN 127.0.0.1:5432 users:(("postgres",pid=812)) tcp LISTEN 0.0.0.0:8080 users:(("node",pid=2210))
| Local Address:Port | Reachable from |
|---|---|
127.0.0.1:5432 | local only |
0.0.0.0:5432 or *:5432 | public |
sudo ss -tulpn | grep :8080ip route get IPping -c 4 host sends 4 ICMP echo requestsICMP can be blocked: a failed ping does not prove the host is down.
| Flag | Does |
|---|---|
-v | DNS, connect, TLS, headers |
-I | headers only |
-s | no progress meter |
-o file | save to file |
-L | follow redirects |
-k | skip cert check, testing only |
-X -H -d | method, header, JSON body |
-w '%{http_code}' | print status code |
--connect-timeout 5 | cap a hung connect |
curl -s -X POST \ -H 'Content-Type: application/json' \ -d '{"a":1}' \ -w '%{http_code}' --connect-timeout 5 URL
JSON POST, prints status code
| curl | wget | |
|---|---|---|
| Use | single requests, APIs | download files |
| Output | stdout by default | disk |
| Extras | -o saves | -c resumes, -r mirrors |
dig example.com A +short: just the answerdig @8.8.8.8 example.com: bypasses the local resolverdig -x IP: reverse lookup-p 2222 -i key~/.ssh/config entries let you type ssh webchmod 600 ~/.ssh/key ssh user@host -p 2222 -i ~/.ssh/key
Host web HostName 10.0.0.5 User deploy Port 2222 IdentityFile ~/.ssh/key ProxyJump bastion
~/.ssh/config
| Task | Command |
|---|---|
| Up | scp file user@host:/path |
| Down | scp user@host:/path/file . |
| Directory | scp -r |
| Port | -P capital (ssh uses -p) |
| Repeat sync | rsync -av |
ssh -L 8080:localhost:80 user@hostssh -v debugs auth failures| Tool | What it is |
|---|---|
| ufw | Ubuntu front end: ufw allow 22/tcp |
| firewalld | RHEL zones: firewall-cmd |
| iptables/nftables | Kernel rules, first match wins |
Timeout = packets dropped (firewall). Refused = host answered, nothing listens on that port.
| Symptom | Means | Check |
|---|---|---|
| Refused | Host answered, nothing listens | ss -tulpn |
| Timeout | Firewall or security group drops packets, or bad route | ip route get IP |
ufw allow 8080 alone: cloud security group or router still blocks127.0.0.1: unreachable either wayufw allow 22 locks you outdig @8.8.8.8 and the TTL| Wrong | Right |
|---|---|
scp -p 2222 | scp -P 2222 |
ss without sudo | sudo ss: shows process names |
curl -k in prod | Testing only; fix the cert |
nc -zv host 22Connection to host 22 port [tcp/ssh] succeeded!Use nc -zv host port, not ping.
| Command | Shows |
|---|---|
ip -br a | brief addresses, UP/DOWN |
ip link | L2 state, MAC |
ip route | routing table, gateway |
ip route get IP | route + source IP |
ss -tulpn | listeners + process |
ss -tn state established | live connections |
sudo lsof -i :PORT | process on port |
ping -c 4 host nc -zv host port curl -v URL curl -sI URL curl -w '%{http_code}' -o /dev/null -s URL
| Command | Answers |
|---|---|
dig example.com +short | bare answer |
dig @8.8.8.8 example.com | specific resolver |
dig -x IP | reverse lookup |
dig example.com MX | mail records |
-L forwards local 8080 to remote port 80ssh -i key -p PORT user@host scp -P PORT -r dir user@host:/path ssh -L 8080:localhost:80 host sudo ufw allow 22/tcp sudo ufw enable sudo ufw status numbered
Run ufw allow 22/tcp before ufw enable.
ss -tulpn shows 127.0.0.1:5432 LISTEN. A client on another host connects to port 5432. What happens, and what is the fix?0.0.0.0, then check the firewallping 8.8.8.8 works but ping example.com fails. Which layer is broken, and what do you run next?dig example.com and compare with dig @8.8.8.8 example.comscp -p 2222 app.tar user@host:/tmp. Why does it fail?-p preserves timestamps, so 2222 is read as a file name-P 2222 for the port