Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux nmap command

Linux nmap command

Linux Command Reference Linux Command Reference


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:

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:


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:

TCP connect scan (-sT)

The standard TCP connect scan:

nmap -sT 192.168.1.1

Characteristics:

UDP scan (-sU)

Scan UDP ports:

nmap -sU 192.168.1.1

Characteristics:

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

  1. Lawful use: Only scan networks and systems that you have permission to scan
  2. Obtain authorization: Before using on a corporate network, ensure you have obtained written authorization
  3. Avoid Overuse: Large-scale rapid scanning may be regarded as an attack
  4. 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

  1. Official documentation:man nmap
  2. Official Nmap book: "Nmap Network Scanning"
  3. Online tutorials:https://nmap.org/book/toc.html
  4. 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 Linux Command Reference