To quickly check if a port is open on a host in Linux, you can use several commands. Here are some TL;DR versions of commonly used commands:
Using nc (netcat)
nc -zv <host> <port>
-z: Zero-I/O mode, used for scanning.
-v: Verbose mode.
Replace <host> with the hostname or IP address.
Replace <port> with the port number.
Example:
nc -zv 192.168.1.1 80
Output:
If the port is open: Connection to 192.168.1.1 80 port [tcp/http] succeeded!
If the port is closed: nc: connect to 192.168.1.1 port 80 (tcp) failed: Connection refused
Using telnet
telnet <host> <port>
Replace <host> with the hostname or IP address.
Replace <port> with the port number
Example:
telnet 192.168.1.1 80
Output:
If the port is open: You will see a blank screen or HTTP response, type Ctrl+] then quit to exit.
If the port is closed: Trying 192.168.1.1... telnet: Unable to connect to remote host: Connection refused
Using ss
ss -tuln | grep :<port>
-t: Show TCP sockets.
-u: Show UDP sockets
-l: Show listening sockets.
-n: Show numerical addresses instead of resolving hostnames
grep :<port>: Filter for the specific port. Prepend the port with a colon :
Example:
ss -tuln | grep :80
Output:
If the port is open: You will see an entry for the port.
LISTEN 0 128 *:80 *:*
If the port is closed: There will be no output.
Using nmap
nmap -p <port> <host>
-p: Specify the port to scan.
Replace <host> with the hostname or IP address.
Replace <port> with the port number.
Example:
nmap -p 80 192.168.1.1
Output:
Starting Nmap 7.91
Nmap scan report for 192.168.1.1
Host is up
PORT STATE SERVICE
80/tcp open http
If the port is open: It will show open next to the port.
If the port is closed: It will show closed next to the port.
Using curl
curl -Is http://<host>:<port> | head -n 1
-I: Fetch the HTTP headers only.
-s: Silent mode.
Replace <host> with the hostname or IP address.
Replace <port> with the port number.
Example:
curl -Is http://192.168.1.1:80 | head -n 1
Output:
If the port is open: You will see an HTTP status line, e.g., HTTP/1.1 200 OK
If the port is closed: curl: (7) Failed to connect to 192.168.1.1 port 80: Connection refused
Choose the command that best suits your needs. nc and nmap are typically more versatile for port scanning, while ss is useful for checking locally open ports. telnet and curl can also be used but are more specific to TCP and HTTP respectively.