Networking commands
When "the site is down", the first questions are about the network: Is the server reachable? Is the app listening on its port? Does the domain point to the right IP? Is a firewall in the way? This lesson gives you the commands that answer them.
Your addresses: ip
ip -brief address show up
echo "---"
ip routelo UNKNOWN 127.0.0.1/8 ::1/128 eth0@if328 UP 172.17.0.2/16 --- default via 172.17.0.1 dev eth0 172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2
lois the loopback interface:127.0.0.1(and::1in IPv6) always means "this machine".eth0(orens5,enp0s3…) is the real network card, with its IP address and prefix (/16means the first 16 bits are the network part).- The default route says where to send traffic for everywhere else: the gateway (router).
On a cloud server you'll usually see a private address (10.x.x.x, 172.16–31.x.x, 192.168.x.x). The public IP belongs to the cloud's router, which forwards traffic in. Ask an outside service to see it: curl -s https://ifconfig.me.
💡 Older tutorials use
ifconfigandnetstat(packagenet-tools). They still work where installed, butipandssare the modern replacements and are always there.
Name resolution: DNS
People use names; computers use IP addresses. DNS turns one into the other.
getent hosts localhost
dig +short example.com A | head -2
dig +short gmail.com MX | sort -n | head -2
host -t NS example.com | head -2::1 localhost ip6-localhost ip6-loopback 172.66.147.243 104.20.23.154 5 gmail-smtp-in.l.google.com. 10 alt1.gmail-smtp-in.l.google.com. example.com name server hera.ns.cloudflare.com. example.com name server elliott.ns.cloudflare.com.
| Command | Use |
|---|---|
dig +short name |
The answer only |
dig name |
Full details: TTL, which server answered |
dig @1.1.1.1 name |
Ask a specific DNS server (to compare with your own) |
dig -x 93.184.215.14 |
Reverse lookup: IP → name |
getent hosts name |
Resolve exactly the way programs on this machine do, including /etc/hosts |
Record types you'll set up for a website: A (name → IPv4), AAAA (→ IPv6), CNAME (alias to another name), MX (mail servers), TXT (verification codes, SPF).
/etc/hosts overrides DNS on one machine, which is handy for testing a new server before switching DNS:
echo "127.0.0.1 shop.test" | sudo tee -a /etc/hosts > /dev/null
getent hosts shop.test
cat /etc/resolv.conf | grep -v "^#" | grep .127.0.0.1 shop.test nameserver 192.168.65.7
/etc/resolv.conf shows which DNS server this machine asks.
Is it reachable? ping and traceroute
ping -c 3 -q 127.0.0.1 | tail -23 packets transmitted, 3 received, 0% packet loss, time 2034ms rtt min/avg/max/mdev = 0.041/0.073/0.108/0.027 ms
ping sends small "are you there?" packets and times the replies. No reply doesn't always mean "down": many servers and cloud firewalls block ping. traceroute name (or mtr name, which keeps updating) shows every router on the way, which helps find where the connection breaks.
Is it listening? ss
A server program listens on a port. ss -tlnp lists them:
sudo systemctl start nginx
sudo ss -tlnp | awk 'NR > 1 { split($6, p, "\""); print $4, p[2] }' | sort | column -t0.0.0.0:22 systemd 0.0.0.0:80 nginx [::]:22 systemd [::]:80 nginx
| Option | Meaning |
|---|---|
-t / -u |
TCP / UDP |
-l |
Listening sockets only |
-n |
Show numbers (ports, IPs) instead of names |
-p |
Show the process (needs sudo for other users' processes) |
(Port 22 shows systemd because Ubuntu 24.04 starts the SSH server on demand: systemd listens on the port and launches sshd when someone connects.)
The local address tells you who can connect:
| Listening on | Reachable from |
|---|---|
0.0.0.0:80 or *:80 / [::]:80 |
Every network interface: the internet, if the firewall allows |
127.0.0.1:5432 |
This machine only |
10.0.1.5:6379 |
Only through that one interface |
A classic bug: your app listens on 127.0.0.1:3000, so it works with curl localhost:3000 on the server but not from outside. Configure it to listen on 0.0.0.0 (and put a firewall or reverse proxy in front). Databases should stay on 127.0.0.1 or a private network.
Well-known ports: 22 SSH, 80 HTTP, 443 HTTPS, 3306 MySQL, 5432 PostgreSQL, 6379 Redis, 27017 MongoDB. Ports below 1024 need root to listen on, which is why apps run on 3000 or 8080 behind nginx.
Talking HTTP: curl
curl is the Swiss army knife for testing web servers and APIs:
curl -s http://localhost/ | grep -o "<title>.*</title>"
curl -sI http://localhost/ | grep -iE "^(HTTP|server|content-type)"
curl -s -o /dev/null -w "status %{http_code}, %{size_download} bytes in %{time_total}s\n" http://localhost/ | sed -E 's/in [0-9.]+s/in 0.00Xs/'
curl -s -o /dev/null -w "%{http_code}\n" http://localhost/nope<title>Welcome to nginx!</title> HTTP/1.1 200 OK Server: nginx/1.24.0 (Ubuntu) Content-Type: text/html status 200, 615 bytes in 0.00Xs 404
| Option | Does |
|---|---|
-s |
Silent (no progress bar) |
-I |
Headers only (a HEAD request) |
-i |
Include the response headers |
-v |
Verbose: see the whole conversation, including TLS |
-L |
Follow redirects |
-o file / -O |
Save the body to a file |
-w '%{http_code}' |
Print details after the request |
-X POST -H 'Content-Type: application/json' -d '{"a":1}' |
Send a JSON POST |
-f |
Exit with an error on HTTP 4xx/5xx (great in scripts) |
--resolve shop.com:443:1.2.3.4 |
Connect to a specific IP without changing DNS |
Is the port open? nc
nc (netcat) checks whether you can open a TCP connection at all, before HTTP is involved:
nc -zv -w 2 127.0.0.1 80 2>&1
nc -zv -w 2 127.0.0.1 5432 2>&1Connection to 127.0.0.1 80 port [tcp/http] succeeded! nc: connect to 127.0.0.1 port 5432 (tcp) failed: Connection refused
"Connection refused" means the machine answered but nothing listens on that port. A timeout usually means a firewall silently dropped the packets. That difference tells you where to look.
Firewalls
Ubuntu's friendly firewall front-end is ufw. A typical web-server setup:
ufw default deny incoming > /dev/null
ufw default allow outgoing > /dev/null
ufw allow OpenSSH > /dev/null
ufw allow 80,443/tcp > /dev/null
ufw show addedAdded user rules (see 'ufw status' for running firewall): ufw allow OpenSSH ufw allow 80,443/tcp
Then sudo ufw enable turns it on and sudo ufw status verbose shows it.
⚠️ On a remote server, always allow SSH before enabling the firewall, or you'll lock yourself out. Cloud servers also have a firewall outside the machine (AWS security groups, GCP firewall rules). Traffic must be allowed by both.
Under the hood, ufw writes rules for the kernel's packet filter (nftables on modern systems; older tools: iptables). RHEL-family systems use firewalld (firewall-cmd --add-service=http --permanent).
A troubleshooting checklist
When a website doesn't respond, work from the bottom up:
| Step | Command | If it fails |
|---|---|---|
| 1. DNS points to the right IP? | dig +short shop.com |
Fix the DNS record; wait for the TTL |
| 2. Server reachable? | ping, mtr |
Network, cloud firewall, or server down |
| 3. Port open from outside? | nc -zv shop.com 443 |
Firewall (ufw, security group) |
| 4. Something listening? | sudo ss -tlnp (on the server) |
Start the service; check it listens on 0.0.0.0 |
| 5. App answering correctly? | curl -v https://shop.com |
Check the app's and web server's logs |
Practice
- Find your machine's private IP address and default gateway.
- Look up the
AandMXrecords of a domain you use, withdig +short. - List every listening TCP port and the program behind it.
- Use
curl -wto print only the HTTP status code of three URLs in a loop.
1.An app listens on 127.0.0.1:3000. Who can connect?
2.Which command lists listening TCP ports with their programs?
3.nc -zv host 443 says "Connection refused". What does that tell you?
4.What does dig +short example.com show?