← Networking and Remote Servers from Zero
Ports and connections: ss, lsof, nc and curl

Lesson 2 of 6

Ports and connections: ss, lsof, nc and curl

Understand ports with an apartment-building analogy, then see what is listening on your machine, find which program owns a port, and test whether a server answers.

The idea in plain words

Think of an apartment building. The IP address is the street address of the building, and a port is the apartment number: each service lives behind its own number. A service that is listening is like someone at home, ready to open the door.

Some numbers are famous: 22 for SSH, 80 for web pages and 443 for secure web pages. Ports run from 0 to 65535, so a computer has plenty of doors, and a tool like ss shows you which ones are open.

Commands in this lesson

sudo ss -tulpn
List the listening TCP and UDP ports with the program that owns each one.
lsof -i :3000
Show which program is using port 3000. Useful for the error "address already in use".
nc -zv 203.0.113.10 22
Knock on a port and report whether anything answers, without sending data.
curl localhost:3000
Make a web request from the terminal and print the response.
curl -I https://example.com
Request only the headers: the status code, server and content type.

Written notes from the video

Why ports matter

One computer can run a website, a database and a login service at the same time. How does it keep them apart? This is Networking and Remote Servers from zero, episode two: ports and connections. Five commands to see what is listening, and to test who can connect.

The apartment building analogy

Think of an apartment building. The IP address is the street address of the building. A port is the apartment number. Each service lives behind its own number. A service that is listening is like someone at home, ready to open the door. Some numbers are famous: 22 for SSH, 80 for web pages, and 443 for secure web pages. Ports run from 0 to 65535.

Command 1: ss

Command one: ss. It lists the connections and listening ports on your machine. The flags -tulpn mean: TCP, UDP, listening, program and numbers. Each row is a door: port 22 is SSH, port 80 is a web server, and port 3000 is a developer app. Use sudo to see the program names for every service.

Command 2: lsof

Command two: lsof. lsof -i :3000 tells you which program is using port 3000. This is the fix for the classic error: address already in use. Note the PID. Use kill, and that number, to stop it.

Command 3: nc

Command three: nc, short for netcat. With -zv it knocks on a port, and reports whether anyone answers. Succeeded means a service is listening, and nothing is blocking you. Connection refused means the door is closed. A long wait with no answer often means a firewall is blocking you.

Command 4: curl

Command four: curl. It makes a web request from the terminal, and prints what comes back. Try it against your own app, on localhost, port three thousand. Great for checking whether a service is really working, without opening a browser.

Command 5: curl -I

Command five: curl -I. A capital I asks for the headers only, not the whole page. The first line is the status code. 200 means OK. 404 means not found, and 500 means a server error. Headers also tell you the server software, and the type of content.

Recap

Recap. ss lists listening ports. lsof finds which program owns a port. nc knocks on a port. curl makes a web request, and curl -I shows only the headers.

Next episode

Next episode: firewalls. You will learn to decide which doors are open to the world, and which stay shut. Subscribe so you do not miss it, and find written notes for every lesson at changecv.com/skills.

Common questions

What do 0.0.0.0 and 127.0.0.1 mean in the ss output?

0.0.0.0 means the service listens on every network interface, so other computers can reach it. 127.0.0.1 means it only listens on this computer, so nothing outside can connect.

What does "address already in use" mean?

Another program is already listening on that port. Use lsof -i with the port number to find it, then stop that program or pick a different port.

What is the difference between connection refused and a timeout?

Refused means your message reached the machine, but nothing is listening on that port. A long wait with no answer usually means something, often a firewall, is dropping your traffic, or the machine cannot be reached.

What is the difference between TCP and UDP?

TCP sets up a connection and checks that data arrives, which is why web pages and SSH use it. UDP just sends messages without a connection, which suits things like DNS lookups and live video.

Why do I need sudo for ss -p?

Without sudo, ss only shows the program names for processes you own. With sudo it shows the program behind every listening port.