Nmap (Network Mapper) is the first tool most penetration testers reach for. This guide covers host discovery, port scanning, service and OS detection, the Nmap Scripting Engine, timing and output — and a repeatable workflow you can use on every internal or external engagement you are authorized to run.
Only scan systems you own or have explicit, written permission to test. Unauthorized scanning can breach computer-misuse laws and provider terms of service. Everything below assumes an agreed scope.

Installing Nmap
# Debian / Ubuntu
sudo apt update && sudo apt install nmap
# Fedora
sudo dnf install nmap
# macOS (Homebrew)
brew install nmap
nmap --version
Host discovery
Before scanning ports, find which hosts are alive. On an internal /24 this is fast and polite to the network.
# Ping sweep (no port scan), greppable output
nmap -sn 10.0.0.0/24 -oG hosts_up.txt
# When ICMP is filtered, probe common ports for liveness
nmap -sn -PS22,80,443 -PA3389 10.0.0.0/24
-sn disables the port scan so you only learn which hosts respond. In environments that block ICMP, TCP SYN/ACK discovery probes (-PS/-PA) are more reliable.
Port scanning basics
Nmap’s default is a SYN scan (-sS) when run as root, which never completes the TCP handshake and is relatively quiet, and a full-connect scan (-sT) otherwise.
# Default top 1000 TCP ports
nmap TARGET
# All 65535 TCP ports at a steady rate
nmap -p- --min-rate 2000 -oA allports TARGET
# A specific set of ports
nmap -p 22,80,443,8080 TARGET
# UDP scan of the most common services (slow — keep it focused)
sudo nmap -sU --top-ports 50 TARGET
| Flag | Scan type | Notes |
|---|---|---|
-sS |
TCP SYN (half-open) | Needs root; fast; does not complete the handshake. |
-sT |
TCP connect() | No root needed; uses the OS stack; more likely logged. |
-sU |
UDP | Slow; essential for DNS, SNMP, VPN discovery. |
-sn |
No port scan | Host discovery only. |
Service and OS detection
# Version detection + default scripts on the open ports you found
nmap -p 22,80,443 -sV -sC TARGET
# Add OS detection (needs root)
sudo nmap -p- -sV -O TARGET
# Aggressive: -sV, -O, default scripts and traceroute in one
sudo nmap -A TARGET
-sV fingerprints the service and version behind each open port; -O guesses the operating system from TCP/IP stack behaviour. Record versions carefully — they feed your vulnerable-components analysis.
The Nmap Scripting Engine (NSE)
NSE scripts extend Nmap with discovery, version and vulnerability checks. Categories let you stay within a safe boundary.
# Safe, non-intrusive scripts
nmap -sV --script "default,safe" TARGET
# Enumerate a specific service (example: HTTP)
nmap -p 80,443 --script "http-title,http-headers,http-methods" TARGET
# Update the script database
sudo nmap --script-updatedb
Scripts in the intrusive, exploit, dos and brute categories can disrupt services or attempt logins. Only use them when your rules of engagement explicitly allow it, and never on production without agreement.
Timing and performance
# Timing templates: -T0 (paranoid) .. -T5 (insane). -T4 is a common default.
nmap -T4 -p- TARGET
# Explicit rate control for fragile networks
nmap --min-rate 500 --max-rate 1500 --max-retries 2 TARGET
Higher timing is faster but noisier and can overwhelm fragile devices. On OT or legacy networks, drop to -T2 and cap the rate.
Output formats
# Save in all formats at once (normal, XML, greppable)
nmap -sV -sC -oA engagement_scan TARGET
# Convert XML to a readable HTML report
xsltproc engagement_scan.xml -o engagement_scan.html
Always keep machine-readable output (-oX/-oG). It feeds other tools and gives you an evidence trail for the report.
A repeatable workflow
- Discover live hosts with
-snacross the scope. - Sweep all TCP ports fast (
-p- --min-rate) and save with-oA. - Enumerate only the open ports with
-sV -sC(and-Owhere allowed). - Target interesting services with specific NSE scripts.
- UDP scan the top ports separately — don’t forget DNS, SNMP and VPN.
- Record everything and move findings into your notes with timestamps.
Key takeaways
- Scan in phases: discovery, fast port sweep, then targeted service enumeration.
-sSis quiet and needs root;-sTworks without privileges but is more likely logged.- Use NSE categories to stay within scope — avoid
intrusive/bruteunless allowed. - Always save machine-readable output with
-oAfor evidence and tool chaining.
Frequently asked questions
Is it legal to scan with Nmap?
Scanning systems you own or have written permission to test is legal in most jurisdictions. Scanning third-party networks without authorization can violate computer misuse laws and provider terms of service. Always work under a signed scope.
What is the difference between -sS and -sT?
-sS performs a half-open SYN scan that needs raw socket privileges and never completes the TCP handshake. -sT uses the operating system connect() call, completes the handshake, works without root and is more likely to be logged.
How do I scan all 65535 ports quickly?
Use -p- with a reasonable –min-rate (for example 1000–5000 on a stable network) and -T4, then run a second, slower scan with -sV -sC only against the open ports you found.
