Nmap is an open source network scanner that answers three questions about a range of addresses: which hosts are up, which ports they expose and which services answer on those ports. It is a common first step of an assessment and a routine tool for administrators. Test only networks and systems you own or are authorised to assess. The Nmap project allows a few scans a day of scanme.nmap.org. Part of Penetration Testing Field Notes.
How Nmap decides a host is up
Before it scans ports, Nmap runs host discovery. Run as a privileged user with no options, it sends an ICMP echo request, a TCP SYN to port 443, a TCP ACK to port 80 and an ICMP timestamp request, the same as -PE -PS443 -PA80 -PP. A reply from the target, even a TCP reset, shows the host is up. Targets on a local Ethernet segment get ARP requests instead (Neighbor Discovery on IPv6). An unprivileged scan only tries TCP connections to ports 80 and 443 through the connect system call, on any network.
Three options control this stage:
-snruns host discovery and stops, so you get live hosts without a port scan. Older guides call it-sP.-Pnskips discovery and treats every address as up. Use it when a firewall drops probes; every address then gets the full, slow port scan.-sLlists the targets and their reverse DNS names without sending them packets, a check that your range is the one you meant.
The probe options -PS, -PA, -PU, -PE, -PP and -PM replace the defaults, though ARP still runs on a local segment. The TCP and UDP ones take a port list with no space after the option, for example -PS22,80,443.
Scan types and why privileges matter
With raw packet access, the default scan is the SYN scan, -sS. It sends a SYN: a SYN/ACK means open, a reset means closed, and silence after retransmissions or an ICMP unreachable error means filtered. It never completes the connection. Raw packets need root on Unix; on Windows an administrator account is recommended, and Nmap uses Npcap. Without that access Nmap falls back to the connect scan, -sT, which asks the operating system to open a full connection. It is slower, and on open ports the target completes the handshake and may log it.
UDP is scanned with -sU, which also needs raw packet access and combines with a TCP scan type. A closed UDP port normally returns an ICMP port unreachable. A silent port is reported as open|filtered, because Nmap cannot tell an open service from a dropped probe. Rate limited ICMP errors make UDP scans slow, so start with --top-ports.
Choosing ports
By default Nmap scans the 1,000 most common ports for each protocol. -F reduces that to 100, -p- scans 1 to 65535, and -p 22,80,443 scans exactly those. With -sU and a TCP scan type together, a protocol prefix splits the list: -sS -sU -p T:80,443,U:53. --top-ports <n> takes the <n> most common, and --exclude-ports removes ports.
The six port states
open: an application is accepting connections or packets.closed: the port is reachable but nothing is listening.filtered: a firewall or filter stops Nmap telling whether it is open.unfiltered: reachable, but open or closed is undetermined. Only the ACK scan,-sA, reports this.open|filtered: Nmap cannot separate open from filtered. UDP, FIN, NULL, Xmas and IP protocol scans use it.closed|filtered: Nmap cannot separate closed from filtered. Only the idle scan uses it.
Service, version and OS detection
-sV probes open ports to identify the service and version. --version-intensity takes 0 to 9 (default 7), and --version-light and --version-all set it to 2 and 9. -O adds OS detection, which works best with at least one open and one closed TCP port. -A turns on OS detection, version detection, script scanning and traceroute. Nmap Scripting Engine scripts are covered in the NSE guide.
Timing: fast is not always better
-T0 to -T5 pick a timing template: paranoid, sneaky, polite, normal (the default), aggressive and insane. -T4 assumes a reasonably fast, reliable network and is what the reference guide recommends on one. -T5 trades accuracy for speed: it caps the TCP scan delay at 5 ms, allows two retries and skips any host not finished within 15 minutes. A response that arrives after a short timeout is lost, so an open port can be reported as filtered or a host missed. -T0 waits five minutes between probes and -T1 fifteen seconds, which suits only a few ports. -T2 can take ten times longer than the default. Rerun a surprising result more slowly.
Output: save every scan
Nmap still prints to the screen when you save. -oN writes normal text, -oX writes XML, the format to feed other tools, and -oG writes grepable output, which the guide marks as deprecated. -oA <basename> writes all three in one go, as <basename>.nmap, <basename>.xml and <basename>.gnmap. Use it every time, so you can compare later scans. --reason shows why each port has its state, and --open hides every port that is not open, open|filtered or unfiltered.
Commands
Checked against Nmap 7.993 (released 23 October 2026) and its reference guide. 192.0.2.0/24 is a documentation range, so replace it with your own lab.
# list targets, then discovery only (as root, so a local segment gets ARP)
nmap -sL 192.0.2.0/24
sudo nmap -sn -oA lab-discovery 192.0.2.0/24
# privileged: all TCP ports, then top 50 UDP
sudo nmap -sS -p- -T4 --reason -oA lab-tcp 192.0.2.10
sudo nmap -sU --top-ports 50 -oA lab-udp 192.0.2.10
# services and OS, open ports only
sudo nmap -sS -sV -O --open -oA lab-services 192.0.2.10
# unprivileged: connect scan, discovery skipped
nmap -sT -Pn -p 1-1023 -oA lab-connect 192.0.2.10To learn these with guided labs, work through the Nmap: Network Discovery and Port Scanning course.
Detection and defence
In firewall logs, a SYN scan is many SYNs from one source to many ports on one host within seconds, most of them dropped or reset. Nmap randomises port order by default, so count distinct destination ports per source, not a sequence. A privileged sweep of a routed subnet shows as ICMP echo and timestamp requests with a TCP SYN to port 443 and ACK to port 80, and on the local segment as a burst of ARP requests. A connect scan completes connections and closes them without sending data, which many services log. UDP scans return a stream of ICMP port unreachable messages.
In Suricata 8.0.8, the current stable release, the threshold keyword with type both alerts at most once per window when one source reaches a count of matches within it (thresholding documentation). This rule fires on 20 SYNs from one source in 10 seconds:
alert tcp any any -> $HOME_NET any (msg:"Possible port scan: 20 or more SYNs from one source in 10s"; flow:stateless; tcp.flags:S,CE; threshold: type both, track by_src, count 20, seconds 10; classtype:attempted-recon; sid:1000001; rev:1;)It counts connection attempts, not distinct ports, so a busy legitimate client can trip it: tune the count and exclude known hosts. Counting distinct ports with unique_on dst_port appears only in the development documentation (9.0.0-dev), and a rule using it will not load on Suricata 8. A slow scan (-T1 or -T2) stays under this rule, so use a longer window and watch ARP and UDP volume separately.
To reduce what a scan finds, close services you do not need, filter the rest with a host or network firewall, and segment the network so a scanner on one segment cannot reach every port elsewhere. A filtered port tells an attacker less than a closed one. Patch what must stay open, because version detection reports what is running.
Where to go next
Practise host discovery, port scanning and service detection in the Nmap: Network Discovery and Port Scanning course, or place scanning in a full assessment with the Network and Active Directory Ultimate Course.
This article replaces the KSEC ARK notes first published in 2019.
More from this author
- One capture, three questions2 December, 2026
- Payload channels versus timing channels2 December, 2026
