Nmap is the most important tool in network testing. Almost everyone can run nmap target; professionals know exactly what each option sends on the wire, how long it'll take, and what could go wrong. That knowledge is what lets you scan a hospital network without taking anything down.
Run every command in this lesson against your own lab, or against systems you have written permission to test.
Before the first packet
- Confirm scope. Put the in-scope ranges in a file and exclude anything out of scope:
nmap -iL scope.txt --excludefile exclusions.txt ...- Know where you're scanning from. Note your source IP in your log. If the client's SOC sees your traffic, they'll ask.
- Root or not? SYN scans, UDP scans and OS detection need raw sockets, so run Nmap with
sudoon Linux. - Log everything. Every scan you run should be saved (see the workflow section).
Host discovery
Before scanning ports, Nmap works out which hosts are up. The default probes differ depending on whether you're on the same segment.
| Option | What it does | When to use it |
|---|---|---|
-sn |
Discovery only, no port scan | A quick inventory of live hosts |
-PR |
ARP discovery | Automatic on the local segment; finds hosts that block ping |
-PE |
ICMP echo (ping) | Networks that allow ping |
-PS22,80,443,445,3389 |
TCP SYN to chosen ports | Hosts that drop ping but run services |
-PA80,443 |
TCP ACK probe | Getting past some stateless filters |
-PU53,161 |
UDP probe | Finding UDP-only devices |
-Pn |
Skip discovery, treat every host as up | External tests where firewalls block all probes |
# Internal: who is alive on my VLAN?
sudo nmap -sn 10.20.0.0/24 -oA discovery/vlan20
# External: combine probes so ping-blocking hosts are still found
sudo nmap -sn -PE -PS22,80,443,3389 -PA80,443 -iL external.txt -oA discovery/externalOn external tests, -Pn is common because perimeter firewalls drop everything. It's slower, since Nmap scans every address whether or not it's alive, so budget time for it.
Port scan types
# SYN scan (default when root): fast and reliable
sudo nmap -sS 10.20.0.15
# Connect scan: no root needed, completes the handshake, more likely to be logged
nmap -sT 10.20.0.15
# UDP scan of the most useful ports
sudo nmap -sU --top-ports 50 10.20.0.15Which ports?
| Option | Ports scanned |
|---|---|
| (default) | The 1,000 most common TCP ports |
--top-ports 100 |
The 100 most common |
-p 22,80,443 |
Exactly those |
-p- |
All 65,535 TCP ports |
-p U:53,161,T:1-1024 |
Mixed UDP and TCP lists |
A reliable pattern is two passes: a fast full-range scan to find open ports, then a deep scan of only those ports.
# Pass 1: every TCP port, just find what's open
sudo nmap -sS -p- --min-rate 1000 10.20.0.15 -oA scans/host15-allports
# Pass 2: detailed service detection on the ports that answered
sudo nmap -sS -sV -sC -p 22,80,139,445,3306 10.20.0.15 -oA scans/host15-detailUnderstanding the states
Beyond open, closed and filtered, Nmap reports open|filtered (common with UDP: no reply, so it can't tell) and unfiltered (from ACK scans: reachable, state unknown). Never report a UDP port as open from an open|filtered result alone; confirm it with a protocol-specific probe.
Timing and performance
Timing templates run from -T0 (paranoid) to -T5 (insane). -T3 is the default; -T4 is common on modern, stable networks.
| Option | Effect |
|---|---|
-T4 |
Shorter timeouts, more parallelism |
--min-rate 1000 |
Send at least 1,000 packets per second |
--max-rate 100 |
Never exceed 100 packets per second |
--max-retries 2 |
Fewer retransmissions; faster but may miss ports |
--host-timeout 30m |
Give up on hosts that take too long |
Fragile environments need the opposite of speed. Old printers, industrial controllers, medical devices and some VoIP phones can crash under heavy scanning. On those segments use --max-rate, avoid -T4 and above, skip OS detection and UDP floods, and agree the approach with the client in advance.
Service, version and OS detection
sudo nmap -sV --version-intensity 5 -p 21,22,80,445 10.20.0.15
sudo nmap -O 10.20.0.15
sudo nmap -A 10.20.0.15 # -sV -sC -O plus traceroute-sVsends probes and matches the replies against a database to name the software and version, for exampleOpenSSH 7.4orApache httpd 2.4.29. This is what you'll later map to vulnerabilities.-Oguesses the operating system from TCP/IP quirks. Treat it as a hint, not proof.- Banners can lie. Admins sometimes change them, and Linux distributions backport security fixes without changing the version string. You'll deal with that in the vulnerability validation lesson.
The Nmap Scripting Engine (NSE)
NSE scripts extend Nmap with protocol-specific checks. They're grouped into categories:
| Category | Contents | Use on client systems? |
|---|---|---|
default (-sC) |
Useful, generally safe scripts | Yes |
safe |
Won't crash services or use many resources | Yes |
discovery |
Gather more information | Usually |
vuln |
Check for specific known vulnerabilities | Carefully; read what each does |
auth / brute |
Credential checks and guessing | Only if authorised; risk of lockouts |
intrusive |
May crash or disrupt services | Only with explicit approval |
dos |
Denial of service | Never on client systems |
# Read what a script does before you run it
nmap --script-help smb-security-mode
# Safe, high-value checks
sudo nmap -p 445 --script "smb-security-mode,smb2-security-mode,smb-os-discovery" 10.20.0.0/24
sudo nmap -p 443 --script ssl-enum-ciphers,ssl-cert example-in-scope-host
sudo nmap -sU -p 161 --script snmp-info 10.20.0.0/24Avoid lazy category runs like --script all or --script vuln against production. Pick scripts by name, read their help, and you'll always know what you sent.
A repeatable workflow
mkdir -p discovery scans
# 1. Discovery
sudo nmap -sn -iL scope.txt --excludefile exclusions.txt -oA discovery/live
grep Up discovery/live.gnmap | cut -d' ' -f2 > live.txt
# 2. Fast full TCP sweep
sudo nmap -sS -p- --min-rate 1000 -iL live.txt -oA scans/tcp-all
# 3. Detailed scan of open ports (example for one host)
sudo nmap -sS -sV -sC -p 22,80,443 10.20.0.15 -oA scans/10.20.0.15
# 4. Top UDP ports
sudo nmap -sU --top-ports 25 -iL live.txt -oA scans/udp-top-oA namewrites normal (.nmap), grepable (.gnmap) and XML (.xml) output in one go. XML imports into reporting tools.ndiff old.xml new.xmlshows what changed between two scans, which is perfect for retests.- Faster sweepers like masscan and RustScan can find open ports on huge ranges quickly, but they're aggressive. Use them only where the client agrees, and confirm their results with Nmap.
Lab
In your isolated lab, against Metasploitable 2:
- Find it with an
-snsweep of your host-only network. - Run a full TCP sweep with
-p-and note how many ports are open. - Run
-sV -sCagainst the open ports and list each service and version. - Run a UDP scan of the top 25 ports. Which results are
open|filtered, and how could you confirm them? - Save all output with
-oAand write three lines summarising what you'd investigate first, and why.
Checkpoint
- When would you use
-Pn, and what does it cost you? - What's the practical difference between
-sSand-sT? - Why scan in two passes instead of running
-A -p-on everything? - How would you change your approach for a network full of medical devices?
- Why should you avoid
--script vulnagainst production systems?
Next: service enumeration, where you'll dig into what each open port actually exposes.