Linux nmap command (network scan)
The nmap Command in Linux is a powerful tool for discovering open ports on local or remote hosts.
It can be installed with any package manager. Unlike the netstat tools, ss and lsof that check open sockets or files, nmap searches for open ports using the “trial and error” method, trying to connect to known ports, and if it finds an open one, it checks What type of door service does the door serve.
Using NMAP to Find Open Ports on a Network
To check open ports from 1 to 1000:
A short script can be written to compare open ports with respect to a previous file:
The preview file must first be created to serve as a comparison in the future:
The script below executes nmap, and compares the result with the “original” file. If there is a difference, an email will be sent:
Check reserved ports with nmap
The “-v” option checks all TCP ports reserved on the scanme.nWheelmap.org machine in detailed mode:
SYN scan with nmap
The “-sS” option initiates a stealth SYN scan on every machine in the /24 size network where the scanme.nWheelmap.org address resides.
The “-O” option will determine which operating system is running on each running host. This requires root privileges due to the SYN scan and operating system detection:
Check specific ports with nmap
The “-sV” option is the host enumeration and a TCP scan of the first half of the 198.116.0.0/16 subnet. This tests whether systems run SSH (22), DNS (53), POP3 (110), or IMAP (143) on their standard ports, or anything on port 4564. For any of these ports found open, version detection is used to determine which application is running:
Scan random hosts with nmap
In this example nmap chooses 100 hosts at random and checks for web servers (port 80). Host enumeration is disabled with -Pn:
Save nmap data in XML
In this example, nmap checks 4096 IPs for any web server (port 80 - without pinging) and saves the output in XML format:
Find out if the target uses a firewall
The “-sA” option scans whether the host uses a Linux firewall to filter packets:
Discover connected hosts with ping scan
The “-Sp” option performs a ping-type scan on a network:
This scan category is not always effective, as most networks protect their hosts from ping.
Scan possible open doors
The nmap “—open” option shows a host’s possible open ports:
Quick open port testing with nmap
The “-T5” option quickly scans possible open ports:
Be careful when using nmap
Be careful when running nmap on hosts hosted on Amazon AWS, Google Cloud, or Microsoft Azure, as they check the logs on their edge firewalls to verify that there are none suspicious activity of “PenTest” on your networks.
Nmap Quick Guide
Select targets with NMAP
Single IP scan | nmap 192.168.1.1 |
Scan from an nmap host | www.testhostname.com |
Scan a range of IPs | nmap 192.168.1.1-20 |
Scan from a subnet | nmap 192.168.1.0/24 |
Scan from from an | nmap text file -iL list-of-ips.txt |
Scan an nmap port | -p 22 192.168.1.1 |
Scan a range of nmap ports | -p 1-100 192.168.1.1 |
Scan the 100 common ports (fast) | nmap -F 192.168.1.1 |
Scan all 65535 ports | nmap -p- 192. 168.1.1 |
Scan using TCP connect | nmap -sT 192.168.1.1 |
Scan using TCP SYN (default) | nmap -sS 192.168.1.1 |
UDP port scan | nmap -sU -p 123,161,162 192.168.1.1 |
Port scan - ignore discovery | nmap -Pn -F 192.168.1.1 |
Detect OS and Services | nmap -A 192.168.1.1 |
Detect services | nmap -sV 192.168.1.1 |
Aggressively detect services | nmap -sV --version-intensity 5 192.168.1.1 |
Detect services through the banner | nmap -sV --version-intensity 0 192.168.1.1 |
Save to file | nmap -on outputfile.txt 192.168.1.1 |
Save in XML | nmap -oX outputfile.xml 192.168.1.1 |
Save in TXT for grep | nmap -oG outputfile.txt 192.168.1.1 |
Saves in all formats | nmap -oA outputfile 192.168. |
Get the titles of the HTML pages | nmap --script=http-title 192.168.1.0/24 |
Get the HTTP headers | nmap --script=http-headers 192.168.1.0/24 |
web apps | nmap --script=http-enum 192.168.1.0/24 |