Nmap (Network Mapper) is the standard open-source tool for finding which ports are open on a host or a whole network. System administrators use it to audit firewalls, check that only the expected services are reachable, and find forgotten services before an attacker does.
This guide shows how to install Nmap on Linux and use it to scan open ports: a single port, a range, all 65,535 TCP ports, UDP ports, whole subnets, and service versions. Every command has a short explanation, and a cheat sheet at the end collects the most useful options.
Only scan systems you own or have written permission to test. Port scanning other people’s networks can break the terms of service of your ISP or cloud provider, and is illegal in some countries. To practise, scan your own machine (
localhost), your own LAN, orscanme.nmap.org, a host the Nmap project provides for testing.
Prerequisites
- A Linux system (the examples use Ubuntu 26.04 and 24.04, but the commands are the same on every distribution).
- A user with sudo privileges. Several scan types (SYN, UDP, OS detection) need root.
- The IP address or hostname of the system you want to scan.
Step 1 of 13Install Nmap
Nmap is in the default repositories of all major distributions. Install it with your package manager:
- Ubuntu, Debian and Linux Mint
sudo apt updatesudo apt install nmap -y - RHEL, Rocky Linux, AlmaLinux and Fedora
sudo dnf install nmap -y - Arch Linux and Manjaro
sudo pacman -S nmap - macOS (with Homebrew)
brew install nmap
On Windows, download the installer from nmap.org/download. It includes the Zenmap graphical interface.
Check the installed version:
nmap --version
Ubuntu 26.04 ships Nmap 7.98 and Ubuntu 24.04 ships 7.94. The newest upstream release is 7.99 (March 2026). Any 7.x version works for everything in this guide.
Step 2 of 13Understand Port States
Nmap reports every port it checks in one of these states. Knowing what they mean is the key to reading the results:
| State | Meaning |
|---|---|
| open | A service is listening and accepting connections on this port. |
| closed | The host is reachable and answered, but nothing is listening on the port. |
| filtered | No answer, or an ICMP “unreachable” reply. A firewall is most likely dropping the probes, so Nmap can’t tell if the port is open. |
| unfiltered | The port is reachable, but Nmap can’t tell if it is open or closed (only seen with the ACK scan, -sA). |
| open|filtered | Nmap got no reply and can’t tell which of the two it is. Common in UDP scans. |
| closed|filtered | Nmap can’t tell if the port is closed or filtered (only with the rarely used idle scan). |
Step 3 of 13Run a Basic Port Scan
The simplest scan is nmap followed by a hostname or IP address. By default Nmap checks the 1,000 most common TCP ports:
sudo nmap 192.168.1.10
OutputStarting Nmap 7.98 ( https://nmap.org ) at 2026-11-15 10:12 UTC Nmap scan report for 192.168.1.10 Host is up (0.00031s latency). Not shown: 996 closed tcp ports (reset) PORT STATE SERVICE 22/tcp open ssh 80/tcp open http 443/tcp open https 3306/tcp open mysql MAC Address: 52:54:00:3A:91:C4 (QEMU virtual NIC) Nmap done: 1 IP address (1 host up) scanned in 0.21 seconds
Four ports are open: SSH, HTTP, HTTPS and MySQL. The SERVICE column is Nmap’s guess from the port number only; Step 8 shows how to detect the real service and its version.
To scan your own machine, use localhost or 127.0.0.1. Keep in mind that this shows ports listening on the loopback interface too, which other machines can’t reach. To see what the network sees, scan the server’s real IP from another machine.
Why use sudo?
Run as root, Nmap uses a TCP SYN scan (-sS): it sends a SYN packet and never completes the handshake, which is fast and leaves fewer log entries. Without sudo it falls back to a TCP connect scan (-sT), which opens a full connection through the operating system. Both find open ports; SYN is faster and is the better default.
Step 4 of 13Scan Specific Ports or a Port Range
Use -p to choose the ports. Scan a single port:
sudo nmap -p 22 192.168.1.10
Scan several ports, separated by commas:
sudo nmap -p 22,80,443 192.168.1.10
Scan a range of ports:
sudo nmap -p 1-1024 192.168.1.10
You can also scan ports by service name, or mix TCP and UDP ports with the T: and U: prefixes (UDP ports need -sU as well):
sudo nmap -p http,https,ssh 192.168.1.10sudo nmap -sS -sU -p T:80,443,U:53 192.168.1.10
Step 5 of 13Scan All 65,535 Ports
The default scan skips 64,535 ports, so a service on an unusual port (for example a web panel on 8443 or an app on 9000) can be missed. To scan every TCP port, use -p-:
sudo nmap -p- 192.168.1.10
-p- is short for -p 1-65535. A full scan of a single host on a LAN takes from a few seconds to a few minutes. Over the internet, or through a firewall that drops packets, it is much slower. Speed it up with a faster timing template and minimum packet rate:
sudo nmap -p- -T4 --min-rate 1000 192.168.1.10
-T4 is safe on most modern networks. Avoid -T5 and very high rates on unreliable links: Nmap may give up on slow replies and miss open ports.
Step 6 of 13Fast Scans and the Top Ports
To get a quick overview, scan fewer ports. -F (fast mode) checks the 100 most common ports instead of 1,000:
sudo nmap -F 192.168.1.10
Or choose exactly how many of the most common ports to scan with --top-ports:
sudo nmap --top-ports 20 192.168.1.10
Step 7 of 13Show Only Open Ports
On a network scan the output is full of closed and filtered ports. Add --open to list only the ports that are open (or possibly open):
sudo nmap --open 192.168.1.0/24
Hosts that have no open ports are left out of the report entirely, which makes this the quickest way to answer “what is listening on my network?”
Step 8 of 13Detect Service Versions and the OS
Knowing that port 80 is open isn’t enough for an audit: you need to know which software answers there and which version, to check it for known vulnerabilities. -sV probes each open port to identify the service:
sudo nmap -sV 192.168.1.10
OutputPORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 9.6p1 Ubuntu 3ubuntu13 (Ubuntu Linux; protocol 2.0) 80/tcp open http nginx 1.24.0 (Ubuntu) 443/tcp open ssl/http nginx 1.24.0 (Ubuntu) 3306/tcp open mysql MySQL 8.0.43-0ubuntu0.24.04.1 Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel
This scan shows a real problem: MySQL is reachable from the network. Unless other servers need it, it should listen only on 127.0.0.1 or be blocked in the firewall.
To guess the operating system, add -O (needs root and at least one open and one closed port to be accurate):
sudo nmap -O 192.168.1.10
-A (“aggressive”) turns on version detection, OS detection, the default scripts and traceroute in one go. It is the most informative single option, but also slower and noisier:
sudo nmap -A 192.168.1.10
Step 9 of 13Scan UDP Ports
Services such as DNS (53), DHCP (67), NTP (123), SNMP (161) and WireGuard (51820) use UDP, which a TCP scan never sees. Use -sU:
sudo nmap -sU -p 53,123,161 192.168.1.10
UDP scanning is slow because closed ports often don’t answer and Linux limits ICMP replies to about one per second. Scan specific ports or the top ports rather than all 65,535, and combine with -sV to resolve open|filtered results:
sudo nmap -sU -sV --top-ports 50 192.168.1.10
Step 10 of 13Scan Multiple Hosts and Whole Subnets
Nmap accepts many targets in one command. Separate hosts with spaces:
sudo nmap 192.168.1.10 192.168.1.20 server1.example.com
Scan a whole subnet with CIDR notation, a range, or a wildcard:
sudo nmap 192.168.1.0/24sudo nmap 192.168.1.10-50sudo nmap 192.168.1.*
Read targets from a file (one per line) and skip some hosts:
sudo nmap -iL hosts.txt --exclude 192.168.1.1
To find which hosts are online without scanning their ports, run a ping scan:
sudo nmap -sn 192.168.1.0/24
Step 11 of 13Scan Hosts That Block Ping
Before it scans ports, Nmap checks that a host is up. Many servers and cloud firewalls block ping, so Nmap reports “Host seems down” and stops. Skip host discovery with -Pn to scan the ports anyway:
sudo nmap -Pn -p 22,80,443 203.0.113.25
For an IPv6 address, add -6:
sudo nmap -6 -p 22,80,443 2001:db8::10
Step 12 of 13Save the Scan Results
Save results so you can compare them later or process them in a script. -oN writes normal text, -oG a greppable one-line-per-host format, -oX XML, and -oA all three at once:
sudo nmap -p- --open -oA lan-scan 192.168.1.0/24
The greppable file makes it easy to list every host with an open port in one line:
grep "/open/" lan-scan.gnmap
Run the same scan again after a firewall change and compare the XML files with ndiff (on Ubuntu and Debian it is a separate package, sudo apt install ndiff) to see exactly which ports opened or closed:
ndiff lan-scan-old.xml lan-scan.xml
Step 13 of 13Close the Ports You Don’t Need
A scan is only useful if you act on it. For each open port you don’t recognise:
- Find the process that owns it on the server with
sudo ss -tulpn | grep :3306. See how to check listening ports in Linux for more ways. - If the service isn’t needed, stop and disable it:
sudo systemctl disable --now service-name. - If it is only used locally, bind it to
127.0.0.1in its configuration file. - If other machines need it, allow it only from their addresses in the firewall. On Ubuntu, see how to set up UFW; on RHEL-based systems, see how to open a specific port in firewalld.
Then run the same Nmap scan again from another machine to confirm the port is now closed or filtered.
Nmap Port Scanning Cheat Sheet
| Command | What it does |
|---|---|
nmap 192.168.1.10 | Scan the 1,000 most common TCP ports |
nmap -p 22 192.168.1.10 | Scan one port |
nmap -p 22,80,443 192.168.1.10 | Scan a list of ports |
nmap -p 1-1024 192.168.1.10 | Scan a port range |
nmap -p- 192.168.1.10 | Scan all 65,535 TCP ports |
nmap -F 192.168.1.10 | Fast scan (top 100 ports) |
nmap --top-ports 20 192.168.1.10 | Scan the N most common ports |
nmap --open 192.168.1.0/24 | Show only open ports |
nmap -sS 192.168.1.10 | TCP SYN scan (default as root) |
nmap -sT 192.168.1.10 | TCP connect scan (default without root) |
nmap -sU -p 53,161 192.168.1.10 | Scan UDP ports |
nmap -sV 192.168.1.10 | Detect service names and versions |
nmap -O 192.168.1.10 | Detect the operating system |
nmap -A 192.168.1.10 | Versions, OS, default scripts and traceroute |
nmap -sn 192.168.1.0/24 | Find live hosts without scanning ports |
nmap -Pn 203.0.113.25 | Skip the ping check (host blocks ping) |
nmap -iL hosts.txt | Read targets from a file |
nmap -T4 --min-rate 1000 -p- host | Faster full scan |
nmap -oA scan host | Save results in normal, greppable and XML formats |
Prefix the commands with sudo for SYN, UDP and OS scans. For more options, see the nmap command cheat sheet.
Frequently Asked Questions
How do I scan all ports with Nmap?
Use sudo nmap -p- target to scan all 65,535 TCP ports. Add -sU to include UDP, but expect a UDP scan of every port to take a very long time.
How do I check if a specific port is open with Nmap?
Run nmap -p PORT target, for example nmap -p 443 example.com. The STATE column shows open, closed or filtered. For quick checks without Nmap, see 5 ways to check if a port is open.
Why does Nmap show a port as filtered?
A firewall between you and the host drops the probe packets, so Nmap gets no reply and can’t tell if a service is listening. Check the firewall on the host and any cloud security group or network firewall in front of it.
Why does Nmap say “Host seems down” when the server is up?
The server or its firewall blocks the ping probes Nmap uses for host discovery. Add -Pn to skip discovery and scan the ports directly.
Is it legal to scan ports with Nmap?
Scanning your own systems, or systems you have written permission to test, is legal. Scanning other networks without permission can break your provider’s terms of service and the law in some countries, so always get permission first.
Conclusion
Nmap answers the most basic security question about any server: what can the network reach? Start with a default scan, use -p- to catch services on unusual ports, add -sV to see what is actually running, and use --open and -oA when you audit a whole subnet. Close or firewall every port you don’t need, then scan again to confirm. Running the same scan regularly and comparing the results with ndiff is a simple way to spot new services as soon as they appear.
How did it make you feel?
