Chapter 7 · Lesson 9 of 11
Linux for servers

Linux networking commands for servers (ip, ss, curl, dig, nc)

Linux networking troubleshooting works in layers: ip shows addresses and routes, ss shows listening ports, ping and traceroute test reachability, dig tests DNS, and curl and nc test services.

By the end you will

Work from the interface up to the application with ip, ss, ping, dig, curl and nc to find where a connection fails.

Time
30 min
Level
Intermediate
Check
4 questions
You need
  • An Ubuntu or Debian server you can reach over SSH
  • A user with sudo access

When a site or API stops responding, the cause sits in one of a few layers: the network interface, the route, DNS, a firewall, or the service itself. The commands below let you test each layer in order, so you stop guessing. Run them on the server itself, then from your laptop, and compare.

How it works#

Work from the bottom up. First, does the machine have an address and a route out? Second, does a name resolve to the right address? Third, can you reach the target host? Fourth, is the port open? Fifth, does the service answer correctly? A failure at one layer makes the layers above it meaningless, so find the first one that breaks.

Modern Ubuntu and Debian use the iproute2 tools (ip, ss) instead of the older ifconfig and netstat. A few extras, such as dig, mtr and nc, need packages: sudo apt install dnsutils mtr-tiny netcat-openbsd. Package installs are covered in package management with apt. Concepts: an IP address identifies a host, a port identifies a service on it, and DNS turns names into addresses.

Step 1: Addresses and routes#

bash
ip a
ip -br a
ip route
ip -4 route get 1.1.1.1

ip a lists interfaces and their addresses. Look for the inet line, such as inet 203.0.113.10/24, and a state of UP. -br gives a brief table. ip route prints the routing table, and the default via ... line is the gateway. ip route get ADDRESS shows which interface and gateway the kernel would use to reach it. If there is no default route, nothing outside the local network works.

Step 2: What is listening#

bash
sudo ss -ltnp
sudo ss -lunp
ss -tn state established

-l listening, -t TCP, -u UDP, -n numbers instead of names, -p process. The Local Address:Port column tells you who can connect: 127.0.0.1:5432 accepts only local clients, while 0.0.0.0:80 or *:80 accepts anyone who reaches the host. This is the exact reason an app is “up” but unreachable, see bind address and find what is using a port.

Step 3: Reachability#

bash
ping -c 4 example.com
traceroute example.com
tracepath example.com
mtr -rwc 10 example.com

ping -c 4 sends four ICMP echoes and reports loss and round-trip time. Some hosts block ICMP, so no reply does not always mean down. traceroute (or tracepath, which needs no extra package on Ubuntu) lists the hops to the target, and stars (* * *) mean a hop did not answer, which is normal for some routers. mtr -rwc 10 combines both and prints loss per hop in a report, which helps show where packet loss begins.

Step 4: DNS#

bash
dig example.com
dig +short example.com A
dig +short example.com AAAA
dig @1.1.1.1 example.com
resolvectl status
resolvectl query example.com
cat /etc/hosts

dig shows the question, the answer and which server replied. +short trims the output. @1.1.1.1 asks a specific resolver, which separates your local resolver from the public view. resolvectl status shows the DNS servers systemd-resolved uses for each interface, and resolvectl query resolves through the same path your applications use.

/etc/hosts is checked before DNS for most programs. A stale line there can send traffic to the wrong place, and dig ignores it while curl and ping do not. For public DNS debugging, see check DNS with dig.

Step 5: Services and ports#

bash
curl -I https://example.com
curl -v https://example.com
curl -sS -o /dev/null -w "%{http_code} %{time_total}s\n" https://example.com
curl --resolve example.com:443:203.0.113.10 -I https://example.com
nc -zv 203.0.113.10 443
nc -zv 203.0.113.10 20-25

curl -I sends a HEAD request and prints the response headers. -v shows the connection steps, the TLS handshake and the request, which helps with certificate or redirect problems. The -w example prints just the status code and total time. --resolve host:port:ip forces a name to a chosen IP, so you can test a new server before DNS changes.

nc -zv host port tries a TCP connection without sending data. succeeded! means the port is open. Connection refused means the host answered and nothing listens. A hang followed by timed out usually points to a firewall or wrong route, see set up a firewall with ufw.

Check it worked#

Walk the layers for a single failing site:

bash
ip route | head -n 1
dig +short example.com
nc -zv "$(dig +short example.com | head -n 1)" 443
curl -sSI https://example.com | head -n 1

You should see a default route, an address, succeeded, and an HTTP/2 200 (or HTTP/1.1 200 OK) line. The first one that fails is your lead.

Edge cases and gotchas#

  • ping is not proof. A host can answer ping but have the web port closed, or block ping while serving traffic.
  • Refused and timed out differ. Refused means a reachable host with no listener. A timeout means packets were dropped or lost on the way.
  • IPv4 and IPv6 differ. A name can have both A and AAAA records, and a broken IPv6 path makes curl hang before it falls back. Test with curl -4 and curl -6.
  • Cloud firewalls are outside the server. ss can show a listener while the provider’s security group blocks it. Check both.
  • Docker publishes ports through iptables. A published container port can bypass ufw rules, see publish container ports safely.
  • DNS caches. Resolvers keep answers until the TTL expires. After a change, compare dig @1.1.1.1 with the local answer.
  • Don’t scan hosts you do not own. Use nc and traceroute on your own servers.

Troubleshooting#

SymptomLikely causeFix
curl: (7) Failed to connect to 203.0.113.10 port 443: Connection refusedNo process listens on that portCheck sudo ss -ltnp, then start the service
curl: (28) Connection timed out after 5001 millisecondsFirewall or routing dropCheck ufw and cloud rules, then traceroute
curl: (6) Could not resolve host: example.comDNS failuredig example.com, resolvectl status, check /etc/resolv.conf
ping: example.com: Temporary failure in name resolutionNo working resolverCheck resolvectl status and the network config
connect: Network is unreachableNo default routeip route, restore the gateway or interface
curl: (60) SSL certificate problem: certificate has expiredExpired or wrong certificatecurl -v, then renew, see the certificate lessons
nc: connect to 203.0.113.10 port 443 (tcp) failed: Connection refusedNothing is listeningStart the service or fix the port
Command 'dig' not foundPackage not installedsudo apt install dnsutils

Quick reference#

bash
ip -br a                      # addresses
ip route                      # default gateway
sudo ss -ltnp                 # listening TCP ports
ping -c 4 example.com         # reachability
mtr -rwc 10 example.com       # path and loss
dig +short example.com A      # DNS answer
resolvectl status             # resolver config
curl -I https://example.com   # headers
curl -v https://example.com   # full detail
nc -zv 203.0.113.10 443       # port test

For a networking failure in a deploy, see health check passes but the container is dead.

Next#

Installing these tools needs the package manager, so continue with package management with apt.

Check what you learned4 questions
1Which command lists TCP ports a server is listening on, with process names?
2curl to a server prints Connection refused instantly. What does that usually mean?
3Which command shows the default gateway your server uses?
4What does dig +short example.com A return?

Frequently asked questions

How do I see my IP address on Linux?

Run ip a (short for ip address show). Look for the inet line under your interface, such as eth0 or ens3. Run curl ifconfig.me if you want the public address that others see.

How do I see which ports are listening on Linux?

Run sudo ss -ltnp. It lists listening TCP sockets with numeric ports and the owning process. Add -u for UDP.

How do I test if a port is open from the command line?

Run nc -zv host 443 to try a TCP connection and report success or failure, or curl -v telnet://host:443. A refused connection means no listener, a timeout usually means a firewall.

What is the difference between ping working and curl failing?

ping uses ICMP and only proves the host answers. curl needs a TCP connection to the right port and a working service, so a firewall, wrong port or app error can fail even when ping succeeds.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios