Linux nmap command
nmap (Network Mapper) is one of the most powerful network exploration and security auditing tools on Linux. It can help system administrators and network security experts:
- Discover active hosts on a network
- Scan open ports and services
- Identify operating system types and versions
- Detect vulnerabilities in network services
Thanks to its flexibility, powerful features, and cross-platform nature, nmap is widely used in network security assessment, system administration, network monitoring, and other fields.
Basic Syntax
The basic command format of nmap is as follows:
nmap [扫描类型] [选项] {目标规范}
Where:
扫描类型: Specify the scan technique used by nmap选项: Configure various parameters of the scanning behavior目标规范: Can be an IP address, host name, or IP range
Common scan types
TCP SYN scan (-sS)
The most commonly used and default scan type, also known as "half-open scanning":
nmap -sS 192.168.1.1
Characteristics:
- Fast and stealthy
- Does not complete the TCP three-way handshake
- Requires root privileges
TCP connect scan (-sT)
The standard TCP connect scan:
nmap -sT 192.168.1.1
Characteristics:
- Does not require root privileges
- Establishes a full TCP connection
- Slower and easier to detect
UDP scan (-sU)
Scan UDP ports:
nmap -sU 192.168.1.1
Characteristics:
- UDP scanning is slower
- Many UDP services do not respond
- Requires root privileges
OS detection (-O)
Identify the operating system of the target host:
nmap -O 192.168.1.1
Commonly used options
Port specification (-p)
Scan specific ports or port ranges:
Example
nmap
-p
80
,
443
192.168.1.1
Scan ports 80 and 443
nmap
-p
1
100
192.168.1.1
Scan ports 1-100
nmap
-p-
192.168.1.1
Scan all 65535 ports
Service version detection (-sV)
Probe detailed version information of services:
nmap -sV 192.168.1.1
Scan speed (-T)
Control the scan speed (0-5; higher numbers are faster):
nmap -T4 192.168.1.1 # 较快的扫描速度
Output formats
Multiple output format options:
Example
nmap
-oN
result.txt 192.168.1.1
Plain text format
nmap
-oX
result.xml 192.168.1.1
XML format
nmap
-oG
result.gnmap 192.168.1.1
grepable format
Practical Examples
Basic network scan
nmap 192.168.1.1
Example output:
Starting Nmap 7.80 ( https://nmap.org ) at 2023-05-01 10:00 UTC
Nmap scan report for 192.168.1.1
Host is up (0.045s latency).
Not shown: 998 closed ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
443/tcp open https
Comprehensive scan (OS + service version)
nmap -A 192.168.1.1
Scan an entire subnet
nmap 192.168.1.0/24
Read the target list from a file
nmap -iL targets.txt
Interpreting scan results
Meanings of port states in nmap output:
| State | Description |
|---|---|
| open | The port is open and an application is listening |
| closed | The port is closed (the host is reachable but no application is listening) |
| filtered | The port is filtered by a firewall/network, so the state cannot be determined |
| unfiltered | The port is accessible but cannot be determined as open or closed (used in ACK scans) |
| open|filtered | Cannot determine whether the port is open or filtered (common in UDP scans) |
| closed|filtered | Cannot determine whether the port is closed or filtered |
Security and legal considerations
- Lawful use: Only scan networks and systems that you have permission to scan
- Obtain authorization: Before using on a corporate network, ensure you have obtained written authorization
- Avoid Overuse: Large-scale rapid scanning may be regarded as an attack
- Respect privacy: Do not scan network resources that do not belong to you
Advanced Tips
bypass firewalls
Example
nmap
-f
--mtu
24
192.168.1.1
Use fragmentation
nmap
--data-length
100
192.168.1.1
Add random data
nmap
-D
RND:
5
192.168.1.1
Decoy scanning
Timing scan scripts
Example
#!/bin/bash
DATE
=$
(
date
%
Y
%
m
%
d
)
nmap
-sS
-p-
-T4
-oN
scan_
$DATE
.log 192.168.1.0
/
24
Compare results
ndiff scan1.xml scan2.xml
Frequently Asked Questions
Q: Why does nmap scanning require root privileges? A: Some scan types (such as SYN scans) need to manipulate network packets directly, which requires root privileges.
Q: How can I speed up scanning? A: Use -T4 or -T5 option, reduce the timeout, or limit the port range to scan.
Q: Can nmap scans be detected by a firewall? A: It depends on the scan type and firewall configuration. SYN scans are stealthier than full-connect scans.
Q: How do I scan IPv6 addresses? A: Just use the IPv6 address directly:nmap 2001:db8::1
Recommended learning resources
- Official documentation:
man nmap - Official Nmap book: "Nmap Network Scanning"
- Online tutorials:https://nmap.org/book/toc.html
- Interactive learning:https://tryhackme.com/room/furthernmap
By mastering nmap, you will gain powerful network exploration capabilities and lay a solid foundation for system administration and network security work. It is recommended to start with simple scans and gradually try more advanced options and techniques.
Linux Command Reference