Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Scanning Basics

Choosing Ports to Scan

Control which ports Nmap scans with -p, -F, --top-ports and -p-, mix TCP and UDP port specs, use service names and ranges, and parse port expressions in Python.

By default Nmap scans the 1,000 most common ports. That is a smart compromise, but it is not enough when a service hides on an unusual port, and it is too much when you only care about one. The -p family of options lets you choose precisely. This lesson covers every form, how port lists interact with protocols, and how to count what you are about to scan.

Commands are read-only examples; run them only against machines you own. The Python editors model the syntax with ordinary string handling.

The default: 1,000 ports

Nmap ships a file called nmap-services that lists ports with how often each was seen open across the internet. The default scan takes the 1,000 most frequent, not "ports 1-1000". So the default includes ports like 3389 and 8080 but skips many low numbers nobody uses.

-p: a specific list

Terminal
nmap -p 80 192.0.2.10             # one port
nmap -p 22,80,443 192.0.2.10      # a list
nmap -p 1-1024 192.0.2.10         # a range (inclusive)
nmap -p 20-25,80,8000-8100 192.0.2.10
nmap -p- 192.0.2.10               # all 65,535 ports (same as -p 1-65535)
nmap -p-100 192.0.2.10            # from 1 to 100
nmap -p 1000- 192.0.2.10          # from 1000 to 65535

-p- is the standard way to check for services on unusual ports; it takes longer but misses nothing. Port 0 is only scanned if you ask for it explicitly with -p0-.

Service names

You can use names from nmap-services instead of numbers, including wildcards:

Terminal
nmap -p http,https 192.0.2.10
nmap -p "ht*" 192.0.2.10          # quote wildcards for the shell

Names are convenient but follow the same lookup file, so they are only as accurate as the port-to-service mapping.

TCP and UDP in one command

Prefix a port list with T: or U: to choose the protocol. You need both a TCP and a UDP scan type for this to be meaningful:

Terminal
sudo nmap -sS -sU -p T:22,80,443,U:53,161 192.0.2.10

Without a prefix, ports apply to the protocol of the scan. -p U:53 alone with a SYN scan only sets the UDP list, so no TCP port is scanned. A common surprise.

Fast and top ports

Terminal
nmap -F 192.0.2.10                # fast: the 100 most common ports
nmap --top-ports 20 192.0.2.10    # the 20 most common
nmap --top-ports 5000 192.0.2.10  # a broader sample

-F equals --top-ports 100. This is handy for quick checks. Use --port-ratio 0.1 to scan every port that was open on at least 10% of hosts in Nmap's data, a different way to express "popular".

Excluding ports: --exclude-ports

When you want almost everything but one noisy or fragile port:

Terminal
nmap -p- --exclude-ports 9100 192.0.2.10   # skip a printer port

Port 9100 is a raw print port that some printers react to by printing the garbage they receive: a classic reason to exclude it. Always review port exclusions in your scope.

Scan order: -r

By default Nmap scans ports in random order to even out load and slightly confuse simple detectors. Use -r for sequential order, which is easier to follow when watching a scan or debugging.

flowchart LR
  A["No -p option"] --> B["1000 most common ports"]
  C["-F"] --> D["100 most common"]
  E["--top-ports N"] --> F["N most common"]
  G["-p- or -p 1-65535"] --> H["All ports"]
  I["-p list"] --> J["Exactly the listed ports"]

Figure: how the main options pick ports.

The cost of scanning more ports

Time grows roughly with the number of ports, and filtered ports are much slower than closed ones because Nmap waits for replies that never come. A full TCP scan of a host that answers everything takes seconds; the same scan of a host behind a dropping firewall can take many minutes. Plan accordingly: a first fast pass (-F or the default), then a targeted -p- on important hosts.

A stand-in: parse a port expression

Writing a parser for the -p syntax shows what Nmap does. This helper expands lists and ranges, handles open-ended ranges and T:/U: prefixes, and counts the result.

Python Expand an Nmap -p expression

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first examples apply to both protocols because no prefix appears. In real Nmap a scan type decides which protocol is used, but the counting idea is the same.

A stand-in: how long will it take?

A rough estimate helps you decide whether -p- is reasonable. This model assumes each closed port costs a little time, each filtered port costs much more because of retransmissions, and the work runs in parallel batches.

Python Estimate scan time by port count

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The numbers are a toy model, not a benchmark, but the shape is right: a firewall that silently drops probes makes a full scan far slower. Timing options in Evasion, Timing and Ethics reduce that cost.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the default scan (no -p) check?

    The default list is ranked by how often ports are open across the internet, not simply 1-1000.
  2. 2. Which option scans all TCP ports?

    -p- is shorthand for -p 1-65535.
  3. 3. You run nmap -sS -p U:53 target. What happens to TCP?

    The U: prefix sets only the UDP list, so a TCP SYN scan has no ports to try.
  4. 4. What does -F do?

    -F is the fast scan, equal to --top-ports 100.

Summary

  • Default: the 1,000 most common ports; -F is 100; --top-ports N is the N most common.
  • -p accepts numbers, ranges, open-ended ranges, names, wildcards, and T: / U: prefixes.
  • -p- covers every port and is the way to find hidden services.
  • --exclude-ports skips fragile ports; -r scans in order.
  • Filtered ports make scans slow; plan time and start with a fast pass.

Next: Verbosity and Reasons