You need a terminal and Python 3 (python3 --version). To run
the real tool, install Nmap: sudo apt install nmap on Debian/Ubuntu,
brew install nmap on macOS. Nmap is not present in the environment that
produced this page, so the captured output blocks below come from an equivalent connect-scan
written in Python — the exact technique Nmap uses. Run the nmap commands on
your own machine to see Nmap’s own formatting.
🔴 Scan only what you own or have written permission to test. Everything
here targets 127.0.0.1 — your own machine — or your own home network.
Port-scanning a host you do not control is treated as hostile and is illegal in many places;
a reachable server is not an invitation.
What Is Nmap
Nmap (Network Mapper) is a free, open-source tool used for network discovery and security auditing. It can rapidly scan large networks to determine which hosts are online, what services they are running, what operating systems they use, and what types of firewalls or packet filters are in place.
Originally created by Gordon Lyon (Fyodor) in 1997, Nmap has become one of the most essential tools in any network administrator's or security professional's toolkit. It runs on Linux, Windows, macOS, and BSD.
Unauthorized port scanning is considered hostile activity by most organizations and internet service providers. It may violate laws such as the Computer Fraud and Abuse Act (US), the Computer Misuse Act (UK), or equivalent legislation in your country. Scanning someone else's network without written authorization can result in legal consequences, account termination, or IP blocking. This tutorial is intended solely for scanning your own infrastructure.
Installing Nmap
# Ubuntu / Debian
sudo apt update
sudo apt install nmap
# Fedora / RHEL
sudo dnf install nmap
# macOS (using Homebrew)
brew install nmap
# Verify installation
nmap --version
On Windows, download the installer from https://nmap.org/download. The
Windows package includes Zenmap, a graphical frontend for Nmap.
Host Discovery
Before scanning ports, you often need to find out which hosts are alive on a network. Nmap provides several host discovery techniques, commonly called ping scans.
Basic Ping Scan
The -sn flag tells Nmap to skip port scanning and only check whether
hosts are online. This is the fastest way to discover devices on your network.
# Discover all live hosts on your local subnet
nmap -sn 192.168.1.0/24
Example output:
Starting Nmap 7.94 ( https://nmap.org )
Nmap scan report for router.local (192.168.1.1)
Host is up (0.0025s latency).
Nmap scan report for desktop.local (192.168.1.100)
Host is up (0.0031s latency).
Nmap scan report for server.local (192.168.1.175)
Host is up (0.00010s latency).
Nmap done: 256 IP addresses (3 hosts up) scanned in 2.43 seconds
On a local network, Nmap uses ARP requests (which cannot be blocked by software firewalls) to detect hosts. On remote networks, it sends a combination of ICMP echo requests, TCP SYN to port 443, TCP ACK to port 80, and ICMP timestamp requests. A response to any of these confirms the host is alive.
Target Specification
Nmap accepts targets in several formats:
Port Scanning Techniques
Port scanning is the core function of Nmap. Each port on a host can be in one of several states: open (accepting connections), closed (reachable but no service listening), or filtered (a firewall is blocking the probe).
TCP Connect Scan (-sT)
This is the default scan when you run Nmap without root/sudo privileges. It performs a full TCP three-way handshake (SYN, SYN-ACK, ACK) with each port.
# Full TCP connect scan on common ports
nmap -sT 192.168.1.175
Advantages: reliable, works without root. Disadvantages: slower and more easily detected because it completes the full connection.
SYN Scan (-sS)
Also called a "stealth scan" or "half-open scan." It sends a SYN packet and waits for a response. If it receives SYN-ACK, the port is open. If RST, the port is closed. It never completes the handshake, making it faster and harder to log.
# SYN scan (requires root/sudo)
sudo nmap -sS 192.168.1.175
Crafting raw TCP packets (sending SYN without completing the handshake) requires raw socket access, which is a privileged operation on most operating systems. Regular users can only use the system's TCP stack, which always completes the handshake.
UDP Scan (-sU)
UDP services like DNS (53), SNMP (161), and DHCP (67/68) do not use TCP. A UDP scan sends UDP packets to target ports and interprets the responses.
# Scan common UDP ports (requires root, can be slow)
sudo nmap -sU --top-ports 20 192.168.1.175
UDP scanning is inherently slower than TCP scanning because there is no handshake --
Nmap must wait for a response or timeout for each port. Using --top-ports
limits the scan to the most commonly used UDP ports.
Specifying Ports
# Scan specific ports
nmap -p 22,80,443 192.168.1.175
# Scan a range of ports
nmap -p 1-1000 192.168.1.175
# Scan all 65535 ports
nmap -p- 192.168.1.175
# Scan the top 100 most common ports
nmap --top-ports 100 192.168.1.175
Service and OS Detection
Knowing a port is open is useful, but knowing what software is running on that port and what operating system the host uses provides far more actionable information.
Service Version Detection (-sV)
The -sV flag probes open ports to determine the service name and version
number of the running software.
nmap -sV 192.168.1.175
Example output:
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 9.6p1 Ubuntu 3ubuntu13
80/tcp open http nginx 1.24.0
443/tcp open ssl/http nginx 1.24.0
3306/tcp open mysql MariaDB 10.11.6
This information is critical for security auditing -- if a service is running an outdated version with known vulnerabilities, you know it needs to be patched.
OS Detection (-O)
Nmap can fingerprint the remote operating system by analyzing subtle differences in how the TCP/IP stack responds to specially crafted probes.
# OS detection (requires root)
sudo nmap -O 192.168.1.175
Example output:
OS details: Linux 5.15 - 6.8 (Ubuntu)
Network Distance: 0 hops
Firewalls, load balancers, and custom TCP/IP stack configurations can cause Nmap to misidentify or fail to identify the OS. Treat the results as an educated guess rather than a definitive answer.
Combining Options
Nmap flags can be combined for a comprehensive scan. A common combination for auditing your own network:
# Service versions + OS detection + default scripts + verbose
sudo nmap -sV -O -sC -v 192.168.1.175
The -sC flag runs Nmap's default set of scripts (NSE -- Nmap Scripting
Engine), which perform additional checks like banner grabbing, certificate inspection,
and basic vulnerability detection.
Reading and Saving Output
Understanding Nmap's output is essential for acting on scan results. Each line of the port table tells you the port number, protocol, state, and service.
Port States
Output Formats
Nmap supports several output formats for saving results:
# Normal output (human-readable)
nmap -sV 192.168.1.175 -oN scan-results.txt
# XML output (for parsing with other tools)
nmap -sV 192.168.1.175 -oX scan-results.xml
# Grepable output (one host per line, easy to filter)
nmap -sV 192.168.1.175 -oG scan-results.gnmap
# All three formats at once
nmap -sV 192.168.1.175 -oA scan-results
Using -oA to save in all formats is a good habit. It gives you
a human-readable copy for review, an XML file for importing into security tools,
and a grepable file for quick command-line analysis. You can compare results
over time to detect changes in your network.
Scanning Your Own Network
Scanning your own network is one of the best ways to learn Nmap and improve your security posture. Here is a practical workflow for auditing a home or small office network.
nmap -sn 192.168.1.0/24 -oN discovery.txt
Review the list. Do you recognize every device? Unrecognized hosts may be unauthorized.
sudo nmap -sS --top-ports 1000 192.168.1.0/24 -oA portscan
Look for ports that should not be open. A desktop PC running a web server or an open database port may indicate a misconfiguration or compromise.
sudo nmap -sV -O 192.168.1.175 -oA service-audit
Check for outdated software versions that may have known vulnerabilities.
Now Do It Yourself: Scan a Machine in Five Steps
A port scan asks a machine, one port at a time, “is anyone listening here?” You will run that question against your own computer with Nmap, then build the same scan in fifteen lines of Python so the mechanism is not a black box, read the banners that reveal what is listening, discover which machines are alive on your own network, and save a scan the way a professional writes it into a report. The Python output below was captured by running it.
Go: a terminal, with Nmap installed. Target 127.0.0.1 —
yourself — so there is no question of permission.
Do: run a fast scan of the common ports.
nmap -F 127.0.0.1
You should see: a short table headed PORT STATE SERVICE, one
row per open port — for example 22/tcp open ssh or
631/tcp open ipp. STATE is the word that matters: open
means a service is listening, closed means the port answered but nothing is
there, and filtered means a firewall swallowed the probe.
If not: nmap: command not found — install it (see
Before you start). If the table is empty, your machine simply has no open ports,
which is fine and even good; the next step gives you ports to find on purpose.
Go: a folder you can write to.
Do: save this as scan.py and run python3 scan.py.
It tries to complete a TCP connection to each port; a connection that succeeds means the port
is open. That is precisely what nmap -sT (a “connect scan”) does.
import socket
def scan(host, ports):
for port in ports:
s = socket.socket(); s.settimeout(0.4)
try:
s.connect((host, port)) # handshake completes -> open
print(" %-6d open" % port)
except OSError:
pass # refused or filtered -> stay quiet
finally:
s.close()
print("Scanning 127.0.0.1 ...")
scan("127.0.0.1", list(range(20, 31)) + [80, 443, 3306])
print("done")
You should see: a line for each port that is actually listening on your machine. On the machine that produced this page, with a web and database service running, it printed:
Scanning 127.0.0.1 ...
22 open
80 open
3306 open
done
Your open ports will differ — that is the point of a scan. You have just written a port scanner; Nmap is this idea made fast, careful, and stealthy.
If not: if nothing prints between the two lines, none of those ports is
open on your machine — start a listener to find
(python3 -m http.server 8000 in another terminal) and add 8000 to
the port list.
Go: the same scan.py.
Do: after connecting, read the first bytes the service sends. Many announce themselves. Add this below your scan and run it again.
for port in (22, 80):
s = socket.socket(); s.settimeout(0.5)
try:
s.connect(("127.0.0.1", port))
if port == 80:
s.sendall(b"HEAD / HTTP/1.0\r\n\r\n")
banner = s.recv(80).decode(errors="replace").strip()
print(" port %-4d -> %s" % (port, banner.splitlines()[0]))
except OSError:
print(" port %-4d -> no banner" % port)
finally:
s.close()
You should see: the service identify itself — version and all:
port 22 -> SSH-2.0-OpenSSH_9.6
port 80 -> HTTP/1.0 200 OK
That version string is gold to both defender and attacker: OpenSSH_9.6 tells
you exactly what is running, and therefore which known vulnerabilities to check. Nmap does
this at scale with nmap -sV.
If not: some services stay silent until you speak first (that is why the
code sends a HEAD request to port 80). A closed port raises
OSError and prints no banner — expected if that service is not
running on your machine.
Go: a terminal on your home network. Use your subnet — find
it with ip route | grep default (see the router tutorial); it is usually
192.168.1.0/24 or 192.168.0.0/24.
Do: run a ping scan — host discovery only, no port scanning.
nmap -sn 192.168.1.0/24
You should see: one Nmap scan report for <ip> per device
that answered — your router, your phone, your laptop, your smart-home gadgets —
ending with a count of hosts up. This is the same “who is on my network?” question
the WiFi tutorial answered from the router panel, now from the network side.
If not: scanning a range you were not given is exactly the line not to
cross — keep this to a network you own. If it finds only your own machine, some devices
ignore ping; add -Pn to treat hosts as up, but expect it to be slower.
Go: the same terminal.
Do: re-run a scan of your own machine and write the results to a file, so findings can be quoted and re-checked later.
nmap -sV -oN myscan.txt 127.0.0.1
You should see: the scan on screen and a new
myscan.txt containing the same PORT STATE SERVICE table plus the
version column. Open it with cat myscan.txt. Professionals keep these: a scan is
evidence, and a finding without a saved scan behind it is just an assertion.
If not: -oN is the letter O then N (“normal”
output), not a zero. If the file is empty, the scan was interrupted — let it finish
before reading the file.
Without scrolling up: your scan reports 22/tcp open and the banner reads
SSH-2.0-OpenSSH_9.6. Which single fact is the most useful to an attacker, and
why? Answer: the version, OpenSSH_9.6. “Port 22 is open” only
says SSH exists; the exact version tells an attacker precisely which published vulnerabilities
to try against it. This is why service/version detection (banner grabbing,
nmap -sV) matters far more than a bare open/closed list — and why hiding or
patching version banners is a real hardening step.
Now do it without the page: start two listeners of your own
(python3 -m http.server 8000 and 8001 in separate terminals), then
adjust your scan.py port list to include 8000–8001 and confirm it finds
exactly those two open. Then stop one and re-scan to watch it disappear from the results.
Summary
In this tutorial, you learned the fundamentals of Nmap network scanning:
- Host discovery -- using
-snto find live devices on your network - TCP scanning -- connect scan (
-sT) for unprivileged users and SYN scan (-sS) for speed - UDP scanning -- detecting services that run on UDP with
-sU - Service detection -- identifying software versions with
-sV - OS fingerprinting -- determining operating systems with
-O - Output formats -- saving results for later analysis with
-oN,-oX,-oG, or-oA - Practical auditing -- a step-by-step workflow for scanning your own network
Nmap is a powerful tool that can be used for good or harm. Only scan networks
and systems that you own or have explicit, written permission to test. If you want
to practice scanning, use scanme.nmap.org (Nmap's official test server)
or set up your own lab environment with virtual machines.
You now know how to discover hosts, scan ports, identify services, and audit your own network with Nmap. Regular scanning is a key part of maintaining a secure infrastructure.