Host discovery & port scanning
Find live lab hosts and their open ports with nmap, staying inside scope.
By Invicly Tech, , in Ethical Hacking & Penetration Testing. About 11 minutes to read.
In short
Scanning begins by finding which lab hosts are live, then which ports are open. nmap is the standard tool: host discovery identifies responsive machines, and port scanning reveals the services to examine, all kept inside the authorized scope.
Explained simply
Scanning is the first time you knock on the target's doors. A network is like a street of houses (hosts), and each house has 65,535 numbered doors called ports for TCP, and the same again for UDP. A service, such as a web server, waits behind a port, for example port 80 or 443.
nmap first asks "which houses are lived in?" This is host discovery. Then it asks "which doors open?" This is port scanning. Each answer comes back as a state: open (a service answered), closed (the host answered, but nothing listens there) or filtered (something, usually a firewall, blocked the question).
Scanning is active. It sends real packets to the target, it shows up in logs, and a careless scan can upset fragile devices. That is why you only scan hosts that are in your lab or in a signed scope, and why you save the results as evidence.
In your own lab you can scan as much as you like. Use that freedom to learn what each option does, and watch the traffic in Wireshark at the same time. Seeing the packets makes the port states much easier to understand.
Press Play on a diagram to walk through it one step at a time.
- Step 1Discover hosts
Find live machines in scope.
- Step 2Scan ports
Find open TCP ports.
- Step 3Read states
Open, closed, filtered.
- Step 4Save output
Keep evidence for the report.
- 1Who is alive?
nmap -sn -iL scope.txt
- 2Quick look
nmap -F live hosts
- 3Full TCP sweep
nmap -p- --open on interesting hosts
- 4Top UDP ports
nmap -sU --top-ports 100
- 5Save everything
-oA with a clear name
In depth
How nmap decides a host is up
With -sn, nmap does host discovery only, without a port scan. What it sends depends on where you are and who you are. On a local Ethernet network, run as root, it uses ARP requests: every live machine must answer ARP, even if its firewall drops everything else, so this is very reliable. Against hosts on other networks, a root user's default probes are an ICMP echo request, a TCP SYN to port 443, a TCP ACK to port 80 and an ICMP timestamp request. Without root, nmap can only try TCP connections to ports 80 and 443.
If a host blocks all of those, nmap reports it as down and skips it. -Pn tells nmap to skip discovery and treat every target as up, which is slower but finds hosts that hide from ping.
- -sn: discovery only, no port scan
- Local LAN as root: ARP requests
- Remote as root: ICMP echo, SYN 443, ACK 80, ICMP timestamp
- -Pn: assume all hosts are up
- -PS22,3389: add SYN probes to chosen ports
SYN, connect and UDP scans
The default scan for root is the SYN scan (-sS). nmap sends a SYN, the first packet of a TCP handshake. A SYN/ACK reply means open; nmap then sends a RST instead of finishing the handshake. A RST reply means closed. No reply, or certain ICMP unreachable errors, means filtered. Without root, nmap uses a connect scan (-sT), which asks the operating system to complete the full handshake; it is slower and more likely to be logged by the application.
UDP scanning (-sU) is harder because UDP has no handshake. No reply usually means open|filtered (nmap cannot tell). An ICMP port unreachable reply means closed. Many systems limit how many ICMP errors they send per second, so a full UDP scan can take hours; scan the top UDP ports (for example --top-ports 100) instead.
- Kali (nmap) to Lab target: SYN to port 221SYN to port 22
- Lab target to Kali (nmap): SYN/ACK: port 22 is open2SYN/ACK: port 22 is open
- Kali (nmap) to Lab target: RST: handshake never completes3RST: handshake never completes
- Kali (nmap) to Lab target: SYN to port 234SYN to port 23
- Lab target to Kali (nmap): RST: port 23 is closed5RST: port 23 is closed
All six port states
nmap uses six states. Open: a service accepts connections. Closed: the host is reachable and replied, but nothing listens. Filtered: no answer or an ICMP error, so a firewall probably dropped the probe. Unfiltered: only from an ACK scan (-sA); the port is reachable but nmap cannot tell if it is open. Open|filtered: nmap got no reply and cannot choose, common in UDP scans. Closed|filtered: rare, from the IP ID idle scan.
The --reason flag prints why nmap chose each state, for example syn-ack, reset or no-response. That makes your report much stronger, and it helps you spot firewall behaviour: a firewall that sends RST for blocked ports makes them look closed, not filtered.
- open: syn-ack received
- closed: reset received
- filtered: no-response or ICMP unreachable
- open|filtered: typical for silent UDP ports
- --reason shows the evidence for each state
Speed, scope safety and output
By default nmap scans the 1,000 most common TCP ports. -p- scans all 65,535, and -F only the top 100. Timing templates run from -T0 (very slow) to -T5 (very fast); -T3 is the default and -T4 is fine on a lab network. On real client networks, fragile devices such as old printers or industrial controllers can crash under fast scans, so agree limits in the RoE and use --max-rate if needed.
Feed scope files with -iL and exclusions with --excludefile, so a typo cannot hit the wrong range. -n skips reverse DNS lookups, which is faster and quieter. Always save output: -oA name writes .nmap (human), .gnmap (grep-friendly) and .xml (for tools and reports).
What defenders see and how they respond
A port scan is noisy. One source touching many ports or many hosts in a short time is easy to spot in firewall logs, and intrusion detection systems such as Suricata, Snort and Zeek ship with scan detection. A SYN scan leaves many half-open connections that the target never completes. In your lab, run tcpdump or Wireshark on the target side to see exactly what a defender would see.
The defences are simple. Use a default-deny firewall that only allows the ports a host really needs. Turn off services nobody uses. Separate networks so a scan from one segment cannot reach everything. And alert on scans from inside the network: an internal host scanning its neighbours is a strong sign of compromise.
- Hosts up or down
- Port states with reasons
- Fewer open ports behind a firewall
- Timing and retries
- Many SYNs from one IP
- Half-open connections
- IDS scan alerts
- Firewall deny logs per port
Learn it in 4 short steps
About 1h 30m each. Track them with a timer in your plan.
- 1Run host discovery on a lab subnet~1h 30m
- 2Choose TCP scan types~1h 30m
- 3Scan and interpret port states~1h 30m
- 4Save output for the report~1h 30m
Key points
- Host discovery finds which lab machines respond.
- Port scanning reveals open services to examine.
- Ports are open, closed or filtered.
- Save scan output as evidence for the report.
Key terms
Tap a card to flip it. 0/4 known
Examples
# Against a host you own in the lab (e.g. 192.168.56.10)
nmap -sn 192.168.56.0/24 # host discovery
nmap -sS -p- 192.168.56.10 # all TCP ports
nmap -oN scan.txt 192.168.56.10 # save output as evidenceAll commands target an isolated lab host inside the agreed scope.
Scan the whole lab, step by step
Lab: 192.168.56.0/24, with your own PC at .1 and the DHCP server at .100 out of scope. Step 1: discover live hosts and keep only their IPs. Step 2: run a full TCP scan of those hosts, showing only open ports and the reason. Step 3: run a short UDP scan of the top 50 ports. Step 4: everything is saved with -oA, so the report can quote it.
printf '192.168.56.1\n192.168.56.100\n' > out.txt
sudo nmap -sn -n 192.168.56.0/24 --excludefile out.txt -oA scans/disc
grep 'Status: Up' scans/disc.gnmap | awk '{print $2}' > live.txt
sudo nmap -sS -p- --open --reason -n -T4 -iL live.txt -oA scans/tcp-all
sudo nmap -sU --top-ports 50 -n -iL live.txt -oA scans/udp-top50Why does nmap say the host is down?
You know a lab VM is running, but nmap -sn from another subnet says 0 hosts up. Step 1: the VM's firewall drops ICMP and the default discovery ports. Step 2: try -Pn to skip discovery and scan ports anyway. Step 3: or add discovery probes to ports you expect, such as SSH or RDP. Step 4: compare the results with --reason. The defender lesson: blocking ping only hides a host from lazy scans, not from careful ones.
sudo nmap -sn 192.168.57.20 # Host seems down
sudo nmap -sn -PS22,80,443,3389 192.168.57.20
sudo nmap -Pn -p 22,80,443,3389 --reason 192.168.57.20Quick reference
- nmap -sn RANGE finds live hosts without port scanning
- nmap -Pn skips discovery and treats every target as up
- sudo nmap -sS is the default SYN scan; -sT is used without root
- -p- scans all 65,535 TCP ports; -F scans the top 100
- --open shows only open ports; --reason shows why each state was chosen
- -iL scope.txt --excludefile out.txt keeps scans inside the agreed scope
- -oA NAME saves .nmap, .gnmap and .xml in one go
- Practise freely on your lab; scanme.nmap.org allows light test scans
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Find every live host on your lab network and list only their IP addresses.
- Exercise 2medium
Scan one lab target with -sS while capturing the packets with tcpdump. Find one open and one closed port in the capture.
- Exercise 3hard
On a lab target, add a firewall rule that drops traffic to one open port, then show how nmap's state and reason change.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is nmap scanning illegal?
- nmap itself is a legal tool used by admins every day. Scanning systems you do not own or have permission to test can break computer misuse laws or contracts, so stick to your lab, practice platforms and signed scopes.
- Why does nmap need sudo for a SYN scan?
- A SYN scan builds raw packets by hand, which needs root privileges. Without root, nmap falls back to a connect scan through the operating system.
- Why is my UDP scan so slow?
- UDP has no handshake, so nmap waits for timeouts, and many systems rate-limit the ICMP errors that mark closed ports. Scan only the top UDP ports unless you really need all of them.
- What does filtered mean in nmap?
- nmap got no answer or an ICMP error, so it cannot tell whether a service is there. Usually a firewall is dropping the probes.