Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 36 ยท Scanning Basics

Specifying Targets in Nmap

Learn every way to tell Nmap what to scan: host names, IPs, CIDR blocks, octet ranges, target files with -iL, random targets and --exclude, with Python helpers.

Before Nmap can scan anything, you must tell it where. A target can be one machine, a list, a block of addresses or a file with hundreds of entries. Nmap accepts all of these in a flexible syntax, and learning it saves a lot of typing. It also protects you: a typo in a CIDR prefix can turn a scan of ten machines into a scan of a million.

Commands here are read-only examples. Use only addresses you own or may test. The invented examples use the reserved documentation ranges 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24.

Single targets

Terminal
nmap 192.0.2.10              # an IPv4 address
nmap web01.example.net       # a host name (resolved through DNS)
nmap -6 2001:db8::10         # an IPv6 address needs -6

If a name resolves to several addresses, Nmap scans the first IPv4 address only and prints a note about the others. You can scan them by listing the addresses yourself.

Several targets

Separate targets by spaces. They can mix forms:

Terminal
nmap 192.0.2.10 192.0.2.20 web01.example.net

CIDR blocks

Add a slash and the number of fixed network bits:

Terminal
nmap 192.0.2.0/24      # 256 addresses: .0 to .255
nmap 192.0.2.64/26     # 64 addresses: .64 to .127
nmap 192.0.2.10/32     # exactly one address

The prefix length is the whole trick. /24 has 256 addresses, /16 has 65,536, /8 has 16.7 million. Double-check it before you press Enter; scanning a /8 takes hours and is almost never authorized.

Octet ranges and lists

Each of the four numbers in an IPv4 address (an octet) can hold a range, a list, or both:

Terminal
nmap 192.0.2.1-50              # .1 to .50
nmap 192.0.2.1,5,10-12         # .1, .5, .10, .11, .12
nmap 192.0.2-3.1-20            # 192.0.2.1-20 and 192.0.3.1-20
nmap 192.0.2.*                 # the whole last octet (quote the * on some shells)
nmap 192.0.2,3.*               # two /24 networks

Ranges can appear in more than one octet, so 10.0.0-3.1-254 is valid and produces 4 x 254 = 1,016 addresses. They do not work for IPv6 or CIDR names, and a range like 1-50 is inclusive.

Targets from a file: -iL

For long or generated lists, put one target per line in a file. Targets in the file can use any syntax above, separated by spaces, tabs or new lines. Lines beginning with # are comments.

Terminal
nmap -iL targets.txt
TEXT
# targets.txt - the lab servers
192.0.2.10
192.0.2.20-30
198.51.100.0/28
db01.example.net

Use -iL - to read targets from standard input, which lets you pipe output from another command into Nmap.

Random targets: -iR

nmap -iR 100 scans 100 random public addresses. Do not use this: it scans strangers. It exists for large-scale research by people with the resources and legitimate purpose to do so, and it is exactly the kind of use that gets you complaints. Mentioning it here so you recognize and avoid it.

Excluding targets: --exclude and --excludefile

Exclusions carve targets out of a scan, which is the safe way to honor the exclusions in your scope document (Legal and Ethical Scanning):

Terminal
nmap 192.0.2.0/24 --exclude 192.0.2.1,192.0.2.200-210
nmap -iL targets.txt --excludefile do-not-scan.txt

Both accept the same syntax as normal targets, including CIDR and ranges. Exclusions beat inclusions: an address in both lists is not scanned.

Check before you scan: -sL

The list scan (-sL) only lists what would be scanned and resolves names; it sends no packets to the targets. It is the best way to check a target expression:

Terminal
nmap -sL 192.0.2.0/29 --exclude 192.0.2.3
TEXT
Nmap scan report for 192.0.2.0
Nmap scan report for 192.0.2.1
Nmap scan report for 192.0.2.2
Nmap scan report for 192.0.2.4
Nmap scan report for 192.0.2.5
Nmap scan report for 192.0.2.6
Nmap scan report for 192.0.2.7
Nmap done: 7 IP addresses (0 hosts up) scanned in 0.01 seconds

Example output. 0 hosts up is normal for a list scan because nothing was probed. See List Scan and DNS.

A stand-in: expand Nmap octet ranges

Python can reproduce the octet-range syntax. This helper expands strings such as 192.0.2.1-3 or 192.0.2,3.1,5 into addresses so you can count them before scanning. It supports numbers, ranges, lists and *.

Python Expand Nmap-style octet ranges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stand-in: dry-run with CIDR and exclusions

Like -sL, a dry run catches mistakes. This version uses ipaddress to build a CIDR target list, apply exclusions and report the final count and a few examples.

Python Dry run: targets minus exclusions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Wrong prefix length: /16 instead of /26. Run -sL first.
  • Unquoted *: some shells expand 192.0.2.* against files in the current folder. Quote it or use 1-254.
  • Forgetting -6 for IPv6 targets.
  • Scanning the network and broadcast addresses: harmless but wasteful; Nmap scans every address you give it, including .0 and .255.
  • Hidden duplicates: the same host named twice is scanned once by default, but a name plus a different alias address may be scanned twice. Check with -sL.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many addresses does nmap 192.0.2.64/26 cover?

    A /26 leaves 6 host bits: 2^6 = 64 addresses.
  2. 2. What does -iL targets.txt do?

    -iL means input from list; -oN and friends write output.
  3. 3. An address appears in both the target list and in --exclude. What happens?

    Exclusions always win over inclusions.
  4. 4. Which option safely previews what a target expression will scan, without probing the targets?

    -sL is the list scan: it resolves and lists targets but sends no packets to them.

Summary

  • Targets can be names, IPs, CIDR blocks, octet ranges, lists, or a file read with -iL.
  • --exclude and --excludefile carve addresses out; exclusions win.
  • -sL previews the target list without scanning.
  • Always verify the prefix length; avoid -iR and anything you are not authorized to scan.
  • Dry runs in code (ranges, CIDR, exclusions) catch scope mistakes early.

Next: Reading Nmap Output