Please enable JavaScript to use CodeHS

Network security

What Is a Port Scan?Ports, services, and network scanning explained

A port scan is a way to check which "doors" on a computer are open to the network. Security teams scan their own networks to find forgotten services before attackers do. Here you will learn what network ports are, knock on some doors yourself, and run a safe scan on a pretend home lab.

Cybersecurity ~12 min Middle / High school
The basics

What are network ports?

A port scan checks which network ports on a computer are open and listening. To understand that, you first need to know what a port is.

Every device on a network has an IP address, a number that tells other devices where to send data. An address like 10.0.0.5 works a lot like a building's street address. It gets a delivery to the right building.

But one computer runs many programs at once. A web server program, an email program, and a remote login program might all live on the same machine. So how does a message reach the right one? That is the job of a port: a number from 0 to 65535 that points to one program on the computer. If the IP address is the building, the port is the apartment number.

An IP address and a port together A message labeled 10.0.0.5 colon 443 travels to a building labeled 10.0.0.5. Inside, the door numbered 443 leads to the web server, door 22 leads to SSH, and door 25 leads to email. a message for 10.0.0.5:443 IP 10.0.0.5 443 web (HTTPS) 22 SSH 25 email (SMTP) address : port
The IP address picks the building. The port number picks the door. Written together, they look like 10.0.0.5:443.

A program that waits at a port for messages is called a service. When a service is running and ready, we say it is listening on that port. A door with someone home behind it is an open port.

Your own device uses ports too. When your browser loads a website, it sends the request to the server's port 443 and picks a temporary port on your side so the reply can find its way back to the right browser tab.

Who lives where

Common ports and the services behind them

Any service could use any port, but the internet runs smoother when everyone agrees on defaults. Ports 0 through 1023 are called well-known ports, and an organization called IANA keeps the official list of which service usually goes where. Here are some you will see again and again:

Common network ports and their services
PortServiceWhat it does
22SSHSecure Shell. Lets an admin log in to a computer from far away and type commands, with everything encrypted.
25SMTPSimple Mail Transfer Protocol. Moves email between mail servers.
53DNSDomain Name System. Turns names like codehs.com into IP addresses. Mostly uses UDP, a faster, no-handshake way to send data.
80HTTPWebsites without encryption. Anyone on the path can read the traffic.
443HTTPSWebsites with encryption. This is the padlock in your browser.
23TelnetAn old remote login that sends everything, even passwords, as plain text. Should almost never be open today.
3389RDPRemote Desktop Protocol. Shows a Windows computer's screen to someone far away.

A protocol is a set of rules two programs agree to follow, like the rules of a card game. HTTP, SSH, and DNS are all protocols. Underneath them, most traffic travels using TCP (Transmission Control Protocol), which makes sure data arrives complete and in order. You can learn more about how this layering works in Networking: How Devices Talk, and about the padlock in What Is HTTPS?

Knock, knock

The TCP handshake, in plain words

Before two computers trade real data over TCP, they do a quick greeting called the three-way handshake. Each greeting is a small chunk of data called a packet, and each packet has little flags that say what kind of message it is.

  • SYN ("synchronize"): "Hi, can we talk on port 443?"
  • SYN-ACK ("synchronize-acknowledge"): "Yes, I'm listening. Can you hear me?"
  • ACK ("acknowledge"): "I hear you. Let's go."

Here is the key idea for port scanning: how a computer answers that first SYN tells you what is behind the door. Step through all three cases below.

handshake.vizstep through a TCP connection
Diagram of packets between a client and a server YOUR COMPUTER SERVER 10.0.0.5 :443 FIREWALL SYN → ← SYN-ACK ACK →
Ready Press Next to send the first packet.
step 0 / 3

Notice the three different endings. An open port finishes the handshake. A closed port replies with a quick RST ("reset"), which means "nobody here." A port behind a firewall often gives no reply at all, so your computer waits, gives up, and has to guess.

The main idea

What is a port scan?

A port scan is just that first knock, repeated across many doors. A program called a port scanner sends a small packet to port after port on a computer and writes down how each one responds. The result is a list of every door and what was behind it. Each port usually lands in one of three groups:

  • Open: a service answered. Something is listening and ready to talk.
  • Closed: the computer answered, but said nothing is listening on that port.
  • Filtered: no clear answer came back, usually because a firewall (a security filter that decides which traffic is allowed) dropped the packet. The scanner can't tell whether anything is behind that door.

Try it yourself. The building below is a pretend computer at 10.0.0.5. Each door is a port. Click a door (or tab to it and press Enter) to knock, or knock on all of them at once like a scanner would.

knock.simpretend host 10.0.0.5, safe to explore
10.0.0.5 · 12 doors shown of 65,535

Response

Pick a door

Each knock sends one SYN packet. Watch how the computer answers.

0open
0closed
0filtered

Did you spot the pattern? The open doors are services this computer actually offers, like a website on 80 and 443. The filtered ones are being guarded by a firewall. A real computer has 65,535 TCP ports (plus the same number for UDP), so real scanners usually check the most common ones first.

A port scan does not break into anything. It only asks, "Is anyone there?" But the list it produces is useful for whoever reads it, which is why who is scanning, and whether they have permission, matters so much.

Two sides

Defenders, attackers, and firewalls

The same scan can protect a network or help attack it. The difference is who runs it and why.

Defenders scan to

  • Take inventory. You can't protect what you don't know about. A scan shows every service running on every device.
  • Find forgotten services. An old test server or a remote login someone turned on "just for a day" can sit open for years.
  • Check their firewall. After a change, a scan confirms that blocked ports really are blocked.
  • Shrink the attack surface, which is the total set of ways an attacker could try to get in.

Attackers scan to

  • Do reconnaissance, or "recon": gathering information before an attack, like a burglar walking past houses to see which windows are open.
  • Spot weak services, such as an old program with a known bug, or a login page that might still use a default password.
  • Pick a target. Automated tools scan huge parts of the internet constantly, looking for anything left open.

Because attackers are scanning anyway, the smart move is to scan yourself first and close what you don't need. A firewall is the main tool for that. It sits between a network and the outside world and follows rules like "allow port 443, block everything else." Even better than blocking a port is turning off the service entirely if nobody uses it. Read more in What Is a Firewall?

Try it on a pretend home lab: a small practice network with three devices that you own. First confirm you have permission, then scan, close the risky ports, and scan again.

homelab-scanpretend network 10.0.0.0/24, 3 devices
home-router10.0.0.1
family-pc10.0.0.12
smart-camera10.0.0.31
Scanner ready. Check the permission box, then press Run scan.

Firewall rules

Block the ports nobody needs, then press Run scan again to check your work.

Risky ports reachable: 0

Real defenders follow the same loop: scan, decide what each open port is for, close or update what's not needed, and scan again. Some ports, like the router's DNS service, need to stay open for the network to work. The goal is not "close everything." It's "only open what you can explain."

Ethics and law

Scan only with permission

Port scanning is a normal, everyday security skill. Network administrators do it constantly. But scanning someone else's network without permission is a different story. It can set off security alarms, get your device blocked, break your school's technology rules, and in some places break computer misuse laws. Laws differ from place to place, so security professionals follow one simple habit: get permission first, in writing.

  1. Scan only what you own or are allowed to test. Your own home lab, a virtual machine on your own computer, or a practice target that clearly says scanning is welcome.
  2. Never scan school, library, café, or other public networks. Even if you are "just curious," you don't own them, and the people who run them will see it as an attack.
  3. Get written permission for anything else. Professional penetration testers (people hired to test security) sign an agreement that says exactly what they may scan and when.
  4. Practice in labs built for it. Capture the flag competitions and classroom cyber ranges give you real machines that are meant to be scanned.

What about nmap?

The most famous port scanner is nmap, short for Network Mapper. It's free, open source, and has been around since 1997. IT teams use it to map their networks, and you'll see it in nearly every cybersecurity class and CTF. Under the hood it does what you just practiced: it sends packets to ports and reports open, closed, or filtered. If you want to try it, install it inside your own home lab or a CTF environment, and point it only at machines you're allowed to test.

"I was only scanning" is not a defense. If you are not sure whether you have permission, you don't. Ask the network's owner, or practice in a lab instead. Learn more in What Is Ethical Hacking?

Curious about other tools for looking at networks safely, like ping and traceroute? See Network Commands for Security.

Glossary

Key terms

IP address
A number that identifies a device on a network, like a building's street address.
Port
A number from 0 to 65535 that points network traffic to one program on a device, like an apartment number.
Service
A program that waits at a port for messages, like a web server or SSH.
Protocol
A set of rules that two programs agree to follow when they talk.
TCP
Transmission Control Protocol. Makes sure data arrives complete and in order. Starts with a handshake.
UDP
User Datagram Protocol. A faster way to send data with no handshake. DNS mostly uses it.
Packet
A small chunk of data sent across a network.
Three-way handshake
The SYN, SYN-ACK, ACK greeting that starts a TCP connection.
Port scan
Checking many ports on a device to see which ones are open, closed, or filtered.
Open / closed / filtered
Scan results: a service answered / the device said nothing is there / no clear answer, usually because of a firewall.
Firewall
A security filter that allows or blocks network traffic based on rules.
Reconnaissance
Gathering information about a target before acting. Often shortened to "recon."
Attack surface
All the ways an attacker could try to get into a system. Fewer open ports means a smaller attack surface.
nmap
A free, widely used tool for mapping networks and scanning ports.

FAQ

Quick answers

What is a port scan?

A port scan is a check that sends small messages to many ports on a computer to see which ones answer. Each answer shows whether a port is open (a program is listening), closed (nothing is listening), or filtered (a firewall is blocking the check). Defenders use port scans to find what is running on their own networks.

What are network ports?

A network port is a numbered door on a computer that lets network traffic reach the right program. If an IP address is like a building's street address, a port is like an apartment number. Ports are numbered from 0 to 65535. For example, secure websites usually use port 443 and SSH uses port 22.

Is port scanning illegal?

Scanning networks you own, or networks you have written permission to test, is normal security work. Scanning other people's networks without permission can break school rules and terms of service, and in some places it can break computer misuse laws. Laws are different in different places, so the safe rule is simple: only scan with permission, such as in your own home lab or a capture the flag practice lab.

Are open ports dangerous?

Not by themselves. A web server needs port 443 open so people can visit the site. An open port becomes a risk when nobody needs it, when the program behind it is out of date, or when it uses weak or default passwords. That is why defenders close ports they do not need.

What is the difference between a closed port and a filtered port?

A closed port answers right away to say nothing is listening there. A filtered port does not answer at all, or answers with an error, because a firewall is blocking the check. Closed is like a neighbor saying nobody lives here. Filtered is like a guard who will not let you near the door.

What is nmap?

Nmap, short for Network Mapper, is a free and widely used tool for finding devices on a network and checking which ports are open. Network administrators and security teams use it to keep track of their own systems. Like any scanner, it should only be pointed at networks you own or have permission to test.


Check yourself

Quick quiz

1. If an IP address is like a building's street address, what is a port like?

2. A scanner sends a SYN to port 8080 and immediately gets an RST back. What is the port's state?

3. Why would a school's IT team scan its own network?

4. You just learned about nmap and want to try it. Where is it okay to scan?

Keep learning

Keep learning

Learn how networks are defended

Ports and scans are one piece of the cybersecurity puzzle. CodeHS courses teach how networks work, how attackers think, and how defenders keep systems safe, with hands-on labs built for practice.

Part of the Cybersecurity Skills and Careers tutorial series.