How to: Check open ports quickly in Linux

Nmap

sudo nmap -sT -p- 10.0.0.1

-sT: Scan TCP ports

-p-: All 65535 ports

(Without -p-, by default Nmap will scan 1000 ports)

Output

Starting Nmap 7.80 ( https://nmap.org ) at 2020-03-22 23:10 CEST
Nmap scan report for 10.0.0.1
Host is up (0.00010s latency).
Not shown: 998 closed ports
PORT   STATE SERVICE
22/tcp open  ssh
80/tcp open  http
MAC Address: xx:xx:xx:xx:xx:xx
Nmap done: 1 IP address (1 host up) scanned in 0.50 seconds

The above scan shows that only ports 22, 80 are open.

To scan UDP ports use -sU instead of -sT

sudo nmap -sU -p- 10.0.0.1

Netcat

nc -zv 10.0.0.1 20-443

-z: Only scan for open ports without sending any data

-v: Set verbosity level (can be used several times)

Scan host 10.0.0.1 for open ports from 20 to 443

Output

nc: connect to 10.0.0.1 port 20 (tcp) failed: Connection refused
nc: connect to 10.0.0.1 port 21 (tcp) failed: Connection refused
Connection to 10.0.0.1 22 port [tcp/ssh] succeeded!
…
Connection to 10.0.0.1 80 port [tcp/http] succeeded!
Connection to 10.0.0.1 443 port [tcp/https] succeeded!

-u: Scan UDP ports

nc -z -v -u 10.0.0.1 20-443

Only display open ports with grep

nc -z -v 10.0.0.1 20-80 2>&1 | grep succeeded
 
# Output
Connection to 10.0.0.1 22 port [tcp/ssh] succeeded!
Connection to 10.0.0.1 80 port [tcp/http] succeeded!
Connection to 10.0.0.1 443 port [tcp/https] succeeded!