Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Networking

Linux nmap command

Reference for the Linux nmap command: scan hosts and ports, detect services and OS, choose scan types and output formats, and scan only systems you are allowed to.

nmap ("network mapper") discovers which hosts are alive on a network, which ports they have open, and which services and versions run behind those ports. System administrators use it to audit their own servers and firewalls; attackers use it too, which is why you must only scan systems you own or have written permission to test. Scanning other people's networks can break laws and acceptable-use policies.

Terminal
nmap [SCAN TYPE] [OPTIONS] TARGET...

Try it

nmap is not installed in our sandbox, so these commands are read-only. Use only your own hosts (or scanme.nmap.org, which the Nmap project provides for polite testing):

CONSOLE
$ nmap -p 22,80,443 192.168.1.10
Nmap scan report for 192.168.1.10
Host is up (0.0021s latency).

PORT    STATE  SERVICE
22/tcp  open   ssh
80/tcp  open   http
443/tcp closed https

$ nmap -sn 192.168.1.0/24          # which hosts are up (no port scan)
$ nmap -sV -p 80 192.168.1.10      # detect the service version

A "connect scan" is nothing magic: it just tries to open a TCP connection to each port. Bash can do that against your own machine, with a tiny server on one port:

Bash A connect scan in pure Bash

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 read or cat), it gets the next line from this box.

Syntax

Terminal
nmap [-sS|-sT|-sU|-sn] [-p PORTS] [-F] [-sV] [-O] [-A] [-Pn] [-T0-5] [-oN|-oX|-oG FILE] TARGET

Targets can be a name, an address, a CIDR range (10.0.0.0/24), a dash range (10.0.0.1-50) or a list in a file (-iL hosts.txt).

Option Description
-sT TCP connect scan: works without root, easy to log.
-sS SYN ("half-open") scan: the default as root, faster and quieter.
-sU UDP scan: slow, because closed ports answer slowly and open ones often not at all.
-sn Host discovery only, no port scan ("ping scan").
-p PORTS Ports: -p 22, -p 1-1024, -p- (all 65535), -p T:80,U:53.
-F Fast: scan only the 100 most common ports.
-sV Probe open ports to find service and version.
-O Guess the operating system (needs root).
-A Aggressive: -sV -O plus default scripts and traceroute.
-Pn Skip host discovery; treat every host as up (for hosts that block pings).
-T0 to -T5 Timing template; -T4 is common on fast links, -T3 is the default.
--script NAME Run NSE scripts (for example --script http-title).
-oN, -oX, -oG FILE Save normal, XML or grepable output.

Port states: open (a service answers), closed (reachable, nothing listening), filtered (a firewall drops the probes, so Nmap cannot tell), plus open|filtered and unfiltered in special scans.

Usage notes

  • Permission first. Scanning is not harmless on networks you do not control. Get written approval for work, and prefer scanning from inside your own network.
  • Root changes behaviour. As root, nmap uses raw packets (SYN scan, OS detection, ARP discovery on the local network). Without root it falls back to connect scans.
  • A scan is a snapshot. Services start and stop; repeat scans and compare output (ndiff compares two XML results).
  • filtered is a clue. It usually means a firewall; compare with the firewall rules (iptables and nft).
  • Check yourself with ss. To list what your own machine listens on, ss -tlnp is faster and more precise than scanning localhost.
  • Slow scans. -p- and UDP scans can take a long time; use -T4, limit ports, or add --max-retries 1.
  • Do not script around blocks. Evasion options exist, but if a firewall blocks your scan on a network you do not run, stop.