Nmap: A Complete Guide to Network Exploration and Security Auditing
Nmap: A Complete Guide to Network Exploration and Security Auditing
Nmap ("Network Mapper") is the industry-standard open-source tool for network discovery and security auditing. It uses raw IP packets to determine what hosts are available on a network, what services they offer, what operating systems they run, what type of packet filters or firewalls are in use, and dozens of other characteristics. While commonly associated with security audits, it is equally valuable for routine tasks like network inventory, service upgrade scheduling, and uptime monitoring.
Where Masscan is a "flamethrower" for raw speed across massive IP ranges, Nmap is a "scalpel" for precise, in-depth service analysis. Nmap maintains full connection state, performs version detection via probe-response matching, and runs NSE scripts for vulnerability assessment — capabilities Masscan does not have.
⚠️ Legal and ethical notice: Nmap sends real packets to real machines. Only scan systems you own, systems you have explicit written authorization to test, or bug bounty program assets that are clearly in scope. Unauthorized scanning can be illegal.
Table of Contents
- Installation
- Target Specification
- Host Discovery
- Scan Types
- Port Scanning
- Service & Version Detection
- OS Detection
- Nmap Scripting Engine (NSE)
- Output Formats
- Timing & Performance
- Firewall & IDS Evasion
- Nmap vs Masscan vs RustScan vs ZMap
- Real-World Workflow Examples
- Command Reference
- Final Tips
1. Installation
Nmap can be installed on Linux, Windows, and macOS.
Linux (Debian/Ubuntu)
sudo apt updatesudo apt install nmap -ynmap --version
For RHEL/Fedora-based systems:
sudo dnf install -y nmap
Linux (Arch)
sudo pacman -S nmap
macOS
Via Homebrew:
brew install nmap
Alternatively, download the official macOS installer from nmap.org/download.html.
Windows
Download the latest stable installer (e.g., nmap-7.x-setup.exe) from nmap.org/download.html and run it. The installer includes Npcap for raw packet capture, which Nmap requires. After installation, run Nmap from PowerShell, CMD, or the Zenmap GUI.
Verify Installation
nmap --version
The latest stable version as of mid-2025 is 7.96, which introduced faster parallel DNS lookups and improved Windows compatibility.
Npcap (Windows Dependency)
Windows installations require Npcap for raw packet capture. The Nmap installer bundles it, but if you install Nmap separately, download Npcap from npcap.com.
2. Target Specification
Nmap accepts targets in several formats:
| Format | Example |
|---|---|
| Single IP | nmap 192.168.1.1 |
| Multiple IPs | nmap 192.168.1.1 192.168.1.2 |
| IP range | nmap 192.168.1.1-254 |
| CIDR subnet | nmap 192.168.1.0/24 |
| Domain name | nmap scanme.nmap.org |
| IPv6 target | nmap -6 fe80::1 |
| List from file | nmap -iL targets.txt |
| Random hosts | nmap -iR 100 |
| Exclude a target | nmap 192.168.1.0/24 --exclude 192.168.1.1 |
| Exclude via file | nmap --excludefile blocklist.txt 192.168.1.0/24 |
3. Host Discovery
Host discovery determines which targets are alive before port scanning begins. This is the first phase of any Nmap scan.
Ping Sweep (No Port Scan)
nmap -sn 192.168.1.0/24
The -sn flag performs host discovery only — no port scanning. This is the fastest way to map live hosts on a subnet.
Common Host Discovery Techniques
| Technique | Flag | Description |
|---|---|---|
| ICMP Echo | -PE | Standard ping |
| ICMP Timestamp | -PP | Timestamp request |
| ICMP Address Mask | -PM | Address mask request |
| TCP SYN to port 80 | -PS80 | SYN ping (default port 80) |
| TCP ACK to port 80 | -PA80 | ACK ping |
| UDP to port 40125 | -PU40125 | UDP ping |
| ARP (local network) | -PR | ARP scan (most reliable on LAN) |
| Disable discovery | -Pn | Skip host discovery, treat all hosts as online |
Example: Comprehensive Host Discovery
sudo nmap -PE -PP -PS80,443 -PA80,443 -PU40125 -sn 192.168.1.0/24
When to Use Each
- Local network (LAN): Use
-PR(ARP) — it is the most reliable and fastest on local segments. - Across the internet: Use
-PE(ICMP echo) with-PSand-PAas fallbacks, since many hosts block ICMP. - Hosts that block all discovery probes: Use
-Pn— this skips discovery entirely and attempts port scans directly.
4. Scan Types
Nmap supports a wide range of scan techniques, each with different trade-offs between speed, stealth, and accuracy.
TCP SYN Scan (Stealth) — -sS
sudo nmap -sS 192.168.1.1
The default when running as root/admin. It sends a SYN packet, receives SYN-ACK for open ports, and sends RST to tear down the connection without completing the handshake. Fast and relatively stealthy — never completes the three-way handshake.
TCP Connect Scan — -sT
nmap -sT 192.168.1.1
The default for non-root users. It completes the full three-way handshake, making it more detectable but usable without raw socket privileges.
UDP Scan — -sU
sudo nmap -sU 192.168.1.1
Slower and less reliable than TCP scans, but necessary for services like DNS (53), SNMP (161), and DHCP. UDP scanning sends empty packets and waits for ICMP port-unreachable responses.
TCP ACK Scan — -sA
sudo nmap -sA 192.168.1.1
Maps firewall rule sets — determines whether ports are filtered or unfiltered, not open vs. closed. Useful for identifying stateful firewall behavior.
TCP Window Scan — -sW
sudo nmap -sW 192.168.1.1
Similar to ACK scan, but inspects the TCP window size in RST replies. On some stacks, this reveals open vs. closed ports.
Stealth Scans: FIN, Xmas, NULL — -sF, -sX, -sN
sudo nmap -sF 192.168.1.1 # FIN scansudo nmap -sX 192.168.1.1 # Xmas scan (FIN, PSH, URG flags)sudo nmap -sN 192.168.1.1 # NULL scan (no flags)
These send TCP packets with unusual flag combinations. Closed ports respond with RST; open ports ignore them. Effective against stateless firewalls but unreliable against Windows systems.
FTP Bounce Scan — -b
nmap -b ftp.example.com 192.168.1.1
Uses an FTP server to proxy scan packets. Rarely useful today since most FTP servers disable this feature.
Scan Type Comparison
| Scan Type | Flag | Root Required | Speed | Stealth | Reliability |
|---|---|---|---|---|---|
| TCP SYN | -sS | Yes | Fast | High | High |
| TCP Connect | -sT | No | Moderate | Low | High |
| UDP | -sU | Yes | Slow | Moderate | Variable |
| TCP ACK | -sA | Yes | Fast | High | N/A (firewall mapping) |
| FIN/Xmas/NULL | -sF/-sX/-sN | Yes | Fast | Very High | Low (Windows ignores) |
| Window | -sW | Yes | Fast | High | Moderate |
5. Port Scanning
Default Port Set
Without any port specification, Nmap scans the top 1,000 most common TCP ports.
Specify Ports
# Single portnmap -p 80 192.168.1.1# Multiple portsnmap -p 22,80,443 192.168.1.1# Port rangenmap -p 1-1024 192.168.1.1# All 65,535 portsnmap -p- 192.168.1.1# Top N portsnmap --top-ports 100 192.168.1.1
The -p- flag scans all 65,535 TCP ports — essential for thorough assessments where non-standard ports may hide services.
Fast Port Scan
nmap -F 192.168.1.1
The -F flag scans only the 100 most common ports, significantly reducing scan time.
Protocol-Specific Ports
# TCP ports onlynmap -p T:80,443 192.168.1.1# UDP ports onlynmap -p U:53,161 192.168.1.1# Both TCP and UDPnmap -p T:80,U:53 192.168.1.1
6. Service & Version Detection
Version detection interrogates open ports to determine what is actually running on them.
Basic Version Detection
nmap -sV 192.168.1.1
This probes each open port with service-specific payloads and matches the responses against a database of over 10,000 signatures.
Aggressive Service Detection
nmap -sV --version-intensity 9 192.168.1.1
The --version-intensity flag (0–9) controls how many probes are sent. Higher values are slower but more accurate. The default is 7.
Light Version Detection
nmap -sV --version-light 192.168.1.1
The --version-light flag uses fewer probes for faster results at the cost of some accuracy.
Combined with Aggressive Scan
nmap -A 192.168.1.1
The -A flag enables OS detection, version detection, script scanning, and traceroute all at once.
RPC Service Detection
When RPC services are discovered, Nmap automatically uses its RPC grinder to determine the RPC program and version numbers.
7. OS Detection
Nmap's OS fingerprinting compares TCP/IP stack behavior against a database of known signatures.
Basic OS Detection
sudo nmap -O 192.168.1.1
OS Detection with Version Detection
sudo nmap -O -sV 192.168.1.1
Aggressive OS Detection
sudo nmap -O --osscan-guess 192.168.1.1
The --osscan-guess flag makes Nmap more aggressive in its guesses when it cannot make a perfect match.
Limitations
OS detection requires at least one open and one closed port on the target. It works best when run as root (or with administrator privileges on Windows). It is not 100% accurate and should be treated as a best-effort estimate.
8. Nmap Scripting Engine (NSE)
The Nmap Scripting Engine (NSE) is a Lua-based framework built into Nmap that automates workflows and extends the tool's functionality beyond standard discovery scans. It can perform vulnerability audits, malware detection, and custom penetration testing tasks.
Running Default Scripts
nmap -sC 192.168.1.1
The -sC flag runs the default set of scripts, which are considered safe and useful for most scans.
Running Specific Scripts
nmap --script=http-title 192.168.1.1
Running Script Categories
nmap --script=default,safe 192.168.1.1nmap --script=vuln 192.168.1.1nmap --script=discovery 192.168.1.1
NSE Script Categories
| Category | Purpose |
|---|---|
auth | Examines authentication credentials |
brute | Attempts brute-force authentication |
default | Standard set of safe scripts |
discovery | Queries additional resources for more detailed target info |
dos | Tests for denial-of-service vulnerabilities (use with caution) |
exploit | Attempts to exploit known vulnerabilities |
external | Uses external services for additional information |
fuzzer | Sends unexpected information to test capabilities |
intrusive | Scripts that may crash the target |
malware | Checks for malware infections |
safe | Scripts not designed to disrupt services |
version | Extends version detection |
vuln | Checks for and reports known vulnerabilities without disrupting services |
Source:
Common NSE Scripts
| Script | Purpose |
|---|---|
http-title | Grabs the HTML title of web servers |
http-headers | Retrieves HTTP response headers |
smb-os-discovery | Discovers OS information via SMB |
smb-vuln-ms17-010 | Checks for EternalBlue (MS17-010) |
ssl-heartbleed | Checks for Heartbleed (CVE-2014-0160) |
ftp-anon | Checks for anonymous FTP access |
ssh-brute | Brute-forces SSH credentials |
mysql-info | Retrieves MySQL server information |
dns-zone-transfer | Attempts DNS zone transfer |
Custom NSE Scripts
Custom scripts are written in Lua and placed in the Nmap scripts directory (typically /usr/share/nmap/scripts/). For Nmap to recognize them, they must be added to the script database with:
nmap --script-updatedb
Source:
Example: Vulnerability Scanning with NSE
nmap -sV --script=vuln -p- 192.168.1.1
This runs all vulnerability-checking scripts against every open port.
9. Output Formats
Nmap supports five output formats. The XML format is the most important for programmatic processing and is preferred by most applications that interface with Nmap.
Interactive Output (Default)
Displayed on screen — human-readable but not easily parsed.
Normal Output — -oN
nmap -oN scan.txt 192.168.1.1
Human-readable text file.
XML Output — -oX
nmap -oX scan.xml 192.168.1.1
Machine-readable XML. This is the preferred format for programmatic processing, database import, and visualization tools.
Grepable Output — -oG
nmap -oG scan.gnmap 192.168.1.1
Each host's information is contained on a single line, making it easy to process with grep, awk, and cut.
Script Kiddie Output — -oS
nmap -oS scan.txt 192.168.1.1
A humorous format that replaces letters with numbers and symbols. Rarely used.
All Formats — -oA
nmap -oA scan_results 192.168.1.1
This stores results in normal, XML, and grepable formats simultaneously: <basename>.nmap, <basename>.xml, and <basename>.gnmap.
Verbosity and Debugging
nmap -v 192.168.1.1 # Increase verbosity (use -vv for more)nmap -d 192.168.1.1 # Debug modenmap --reason 192.168.1.1 # Show why a port is in a particular statenmap --open 192.168.1.1 # Show only open (or possibly open) ports
10. Timing & Performance
Nmap's timing templates control the trade-off between speed and stealth.
Timing Templates — -T0 through -T5
nmap -T4 192.168.1.1
| Template | Name | Description |
|---|---|---|
-T0 | Paranoid | Extremely slow, for IDS evasion (one probe every 5 minutes) |
-T1 | Sneaky | Quite slow, for IDS evasion |
-T2 | Polite | Slows down to use less bandwidth and target resources |
-T3 | Normal | Default; balanced speed and reliability |
-T4 | Aggressive | Fast; assumes a reasonably fast and reliable network |
-T5 | Insane | Very fast; assumes an extremely fast network |
For most scans, -T4 is the recommended setting. Use -T2 or lower when scanning fragile or production systems.
Fine-Grained Timing Controls
| Flag | Description |
|---|---|
--min-rate <n> | Send packets no slower than n per second |
--max-rate <n> | Send packets no faster than n per second |
--min-parallelism <n> | Minimum number of parallel probes |
--max-parallelism <n> | Maximum number of parallel probes |
--host-timeout <time> | Give up on a host after this time (e.g., 30m) |
--scan-delay <time> | Delay between probes (e.g., 1s) |
--max-retries <n> | Cap number of retransmissions |
Example: Fast Scan
nmap -T4 --min-rate 1000 --max-retries 1 192.168.1.0/24
11. Firewall & IDS Evasion
Nmap offers several techniques for evading firewalls and intrusion detection systems.
Fragment Packets — -f
sudo nmap -f 192.168.1.1
Splits TCP headers over several packets, making it harder for simple packet filters to detect the scan.
Specify MTU — --mtu
sudo nmap --mtu 16 192.168.1.1
Sets a custom Maximum Transmission Unit size (must be a multiple of 8).
Decoy Scanning — -D
sudo nmap -D RND:10 192.168.1.1
Makes the scan appear to come from multiple sources by sending packets from decoy IP addresses alongside the real source. The RND:10 syntax generates 10 random decoy IPs.
Spoof MAC Address — --spoof-mac
sudo nmap -sS --spoof-mac 0 192.168.1.1
Spoofs the source MAC address. The 0 argument generates a random MAC address.
Idle/Zombie Scan — -sI
sudo nmap -sI zombie.example.com 192.168.1.1
Uses a "zombie" host to perform the scan, keeping your IP completely hidden.
Source Port Spoofing — --source-port
sudo nmap --source-port 53 192.168.1.1
Spoofs the source port to a trusted port (e.g., 53 for DNS), which some firewalls allow through.
Bypass Host Discovery — -Pn
nmap -Pn 192.168.1.1
Skips host discovery entirely, treating the target as online. Useful when ICMP is blocked.
Randomize Target Order — --randomize-hosts
nmap --randomize-hosts 192.168.1.0/24
Scans hosts in random order to avoid triggering sequential-scan detection rules.
Evasion Technique Comparison
| Technique | Flag | Effectiveness | Detectability |
|---|---|---|---|
| Packet Fragmentation | -f | Moderate | Low |
| MTU Manipulation | --mtu | Moderate | Low |
| Decoy Scanning | -D | High | Moderate |
| MAC Spoofing | --spoof-mac | High (LAN only) | Low |
| Idle Scan | -sI | Very High | Very Low |
| Source Port Spoofing | --source-port | Moderate | Low |
12. Nmap vs Masscan vs RustScan vs ZMap
| Feature | Nmap (v7.94) | Masscan (v1.3.2) | RustScan (v2.1.1) | ZMap (v3.0.0) |
|---|---|---|---|---|
| Max Packets Per Second | ~1,000 (stable) | 25,000,000 | ~100,000 (burst) | 1,440,000 (1GbE) |
| Scan Technique | Stateful (SYN/Connect) | Stateless (TCP SYN) | Adaptive (SYN/Connect) | Stateless (TCP SYN/UDP) |
| Service Detection | Yes (Version Detection) | No | Yes (via Nmap) | No |
| Scripting Engine | NSE (Lua) | No | No | No |
| IPv6 Support | Yes | Yes | Yes | Yes (v3.0+) |
| Accuracy at High Rates | High | Medium (Packet Loss) | High | Medium (Network Noise) |
| Primary Use Case | Single Host Audit | Internet-wide Sweep | Fast Internal Recon | Global Research |
| Learning Curve | Moderate | High | Low | High |
Source:
Bottom line: Choose Nmap for precision and service enumeration, Masscan for raw speed across massive IP ranges, ZMap for academic internet-wide research, and RustScan for rapid initial discovery on large internal subnets.
The Ideal Workflow
- Masscan → Identify open ports across the entire scope in minutes
- Nmap → Deep-scan the confirmed open ports on each host
- httpx / Nuclei → Probe web services and check for known vulnerabilities
13. Real-World Workflow Examples
Example 1: Basic Host Discovery and Port Scan
# Step 1: Find live hostsnmap -sn 192.168.1.0/24 -oA discovery# Step 2: Port scan live hostsnmap -sS -sV -O -T4 -iL discovery.gnmap -oA portscan
Example 2: Aggressive Full Port Scan
sudo nmap -A -T4 -p- 192.168.1.1 -oA aggressive_scan
This runs OS detection, version detection, script scanning, and traceroute against all 65,535 ports.
Example 3: Vulnerability Scanning with NSE
nmap -sV --script=vuln -p 21,22,80,443,445,3306 192.168.1.1 -oA vuln_scan
Example 4: Web Server Discovery and Enumeration
# Discover web servers across a subnetnmap -p 80,443,8080,8443 192.168.1.0/24 --open -oG web_servers.gnmap# Enumerate HTTP titles and headersnmap -p 80,443 --script=http-title,http-headers -iL <(grep "Ports:" web_servers.gnmap | awk '{print $2}')
Example 5: Complete Recon Pipeline with Masscan
# Step 1: Fast port discovery with Masscansudo masscan 10.0.0.0/8 -p80,443,22,21,3306 --rate 10000 -oL masscan_results.txt# Step 2: Extract host:port pairsawk '{print $4":"$3}' masscan_results.txt | grep -v '^:' > targets_for_nmap.txt# Step 3: Deep enumeration with Nmapnmap -sV -sC -iL targets_for_nmap.txt -oA nmap_detailed
Example 6: Bug Bounty Recon Workflow
# 1. Subdomain discovery with Subfindersubfinder -d target.com -o subdomains.txt# 2. Resolve to IPs and scan with Nmapnmap -iL subdomains.txt -p 80,443,8080,8443 -sV -T4 -oA bugbounty_scan# 3. Run NSE scripts against discovered servicesnmap -sV --script=http-title,ssl-cert,http-headers -iL subdomains.txt -oA nse_results
Example 7: Stealth Scanning with Evasion
sudo nmap -sS -f --mtu 16 -D RND:5 -T2 --randomize-hosts -Pn 192.168.1.1 -oA stealth_scan
This combines packet fragmentation, MTU manipulation, decoy scanning, slow timing, and host randomization for maximum stealth.
14. Command Reference
Target Specification
| Flag | Description |
|---|---|
<target> | IP, range, CIDR, or domain |
-iL <file> | Read targets from file |
-iR <n> | Scan n random hosts |
--exclude <target> | Exclude targets |
--excludefile <file> | Exclude targets listed in file |
-6 | Enable IPv6 scanning |
Host Discovery
| Flag | Description |
|---|---|
-sn | Ping scan only (no port scan) |
-Pn | Treat all hosts as online (skip discovery) |
-PS <ports> | TCP SYN ping |
-PA <ports> | TCP ACK ping |
-PU <ports> | UDP ping |
-PE | ICMP echo ping |
-PR | ARP ping (LAN only) |
Scan Types
| Flag | Description |
|---|---|
-sS | TCP SYN scan (stealth, default as root) |
-sT | TCP connect scan (default as non-root) |
-sU | UDP scan |
-sA | TCP ACK scan (firewall mapping) |
-sW | TCP Window scan |
-sF | FIN scan |
-sX | Xmas scan |
-sN | NULL scan |
-sI <zombie> | Idle scan |
Port Specification
| Flag | Description |
|---|---|
-p <ports> | Scan specified ports (e.g., -p80, -p1-1024, -p-) |
-F | Fast scan (top 100 ports) |
--top-ports <n> | Scan top n ports |
-r | Scan ports sequentially (don't randomize) |
Service & OS Detection
| Flag | Description |
|---|---|
-sV | Version detection |
--version-intensity <0-9> | Set version detection intensity |
--version-light | Light version detection |
-O | OS detection |
--osscan-guess | Aggressive OS guessing |
-A | Aggressive scan (OS, version, scripts, traceroute) |
NSE
| Flag | Description |
|---|---|
-sC | Run default scripts |
--script=<script> | Run specified script(s) |
--script=<category> | Run scripts in category (e.g., vuln, safe) |
--script-args=<args> | Pass arguments to scripts |
--script-updatedb | Update script database |
Output
| Flag | Description |
|---|---|
-oN <file> | Normal output |
-oX <file> | XML output |
-oG <file> | Grepable output |
-oA <basename> | Output in all formats |
-v | Verbose |
-d | Debug |
--reason | Show why ports are in a given state |
--open | Show only open ports |
Timing & Performance
| Flag | Description |
|---|---|
-T0 to -T5 | Timing templates (paranoid to insane) |
--min-rate <n> | Minimum packets per second |
--max-rate <n> | Maximum packets per second |
--host-timeout <time> | Give up on host after timeout |
--scan-delay <time> | Delay between probes |
--max-retries <n> | Cap retransmissions |
Firewall Evasion
| Flag | Description |
|---|---|
-f | Fragment packets |
--mtu <n> | Set custom MTU |
-D <decoys> | Use decoy addresses |
--spoof-mac <mac> | Spoof MAC address |
-sI <zombie> | Idle scan |
--source-port <port> | Spoof source port |
--randomize-hosts | Randomize target order |
15. Final Tips
- Start with `-sn` to identify live hosts before committing to a full port scan. This saves time and reduces noise.
- Use `-sS` (SYN scan) when possible — it is faster and stealthier than
-sT(connect scan). - Combine `-sV` and `-sC` for service detection and default script execution in a single scan.
- Use `-oA` for all scans — it generates normal, XML, and grepable output simultaneously, giving you maximum flexibility for downstream processing.
- Pipe grepable output into `awk` for seamless integration with other tools:
awk -F" " '{print $4":"$3}' scan.gnmap. - Use `-T4` for most scans — it offers the best balance of speed and reliability on modern networks.
- Respect rate limits — do not use
-T5on production networks or fragile systems. - Use `--open` to reduce noise — it shows only open ports, filtering out closed and filtered results.
- Update NSE scripts regularly — new vulnerability checks are added frequently. Run
nmap --script-updatedbperiodically. - Combine Nmap with Masscan — use Masscan for wide discovery, Nmap for deep enumeration.
- Consult the official documentation at nmap.org/book/man.html for the complete reference guide.
Remember: Nmap is a powerful tool for authorized security testing. Always obtain written authorization before scanning any system you do not own. Unauthorized scanning can be illegal and unethical.
Author
Discussion · 0
No comments yet — start the discussion.
Sign in to join the discussion.