Skip to content
Visual explainer 6h to learn 4 steps

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.

Topic 9 of 32

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.

FlowScanning with nmap
  1. Step 1
    Discover hosts

    Find live machines in scope.

  2. Step 2
    Scan ports

    Find open TCP ports.

  3. Step 3
    Read states

    Open, closed, filtered.

  4. Step 4
    Save output

    Keep evidence for the report.

FlowChoosing an nmap command for the job
  1. 1
    Who is alive?

    nmap -sn -iL scope.txt

  2. 2
    Quick look

    nmap -F live hosts

  3. 3
    Full TCP sweep

    nmap -p- --open on interesting hosts

  4. 4
    Top UDP ports

    nmap -sU --top-ports 100

  5. 5
    Save 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.

SequenceA SYN scan of one open and one closed port
Kali (nmap)
Lab target
  1. 1SYN to port 22
    Kali (nmap) to Lab target: SYN to port 22
  2. 2SYN/ACK: port 22 is open
    Lab target to Kali (nmap): SYN/ACK: port 22 is open
  3. 3RST: handshake never completes
    Kali (nmap) to Lab target: RST: handshake never completes
  4. 4SYN to port 23
    Kali (nmap) to Lab target: SYN to port 23
  5. 5RST: port 23 is closed
    Lab target to Kali (nmap): RST: 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.

CompareScanner view and defender view
What the tester sees
  • Hosts up or down
  • Port states with reasons
  • Fewer open ports behind a firewall
  • Timing and retries
What the defender sees
  • 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.

  1. 1
    Run host discovery on a lab subnet~1h 30m
  2. 2
    Choose TCP scan types~1h 30m
  3. 3
    Scan and interpret port states~1h 30m
  4. 4
    Save 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

bash
# 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 evidence

All 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.

bash
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-top50

Why 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.

bash
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.20

Quick 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.

Question 1

1In a SYN scan, the target replies to your SYN with a RST. What is the port state?

Question 2

2Why is ARP host discovery so reliable on a local Ethernet network?

Question 3

3A UDP port gives no reply at all. How does nmap usually report it?

Question 4

4Which flag saves output in normal, grepable and XML formats at once?

Question 5

5What is the best defence against port scans revealing too much?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Find every live host on your lab network and list only their IP addresses.

  2. Exercise 2medium

    Scan one lab target with -sS while capturing the packets with tcpdump. Find one open and one closed port in the capture.

  3. 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.

Go deeper

ShareXLinkedInWhatsApp