Skip to content

How to Check If a Port Is Open in Linux (6 Ways)

Check if a port is open in Linux with nc, Bash /dev/tcp, nmap and telnet on a remote host, or ss and lsof locally, plus a script and troubleshooting tips.

Category
Networking
Covers
Firewall
On this page

When a service doesn’t respond, the first question is usually “is the port open?” The answer tells you where to look next: at the service itself, at the firewall, or at the network in between.

This guide shows six ways to check if a port is open in Linux. Four of them test a port on a remote host from your machine (nc, Bash’s built-in /dev/tcp, nmap and telnet). Two check which ports are listening on the local machine (ss and lsof). It also includes a reusable shell script and explains what each kind of failure means.

Quick Answer

GoalCommand
Test a remote TCP portnc -zv -w 3 192.168.1.100 22
Test without installing anythingtimeout 3 bash -c '</dev/tcp/192.168.1.100/22' && echo open
Test several ports and see filtered onesnmap -p 22,80,443 192.168.1.100
Is anything listening on port 80 locally?sudo ss -tlnp 'sport = :80'
Which process uses port 80?sudo lsof -i :80

Open, Refused or Timed Out: What the Result Means

Every remote check below ends in one of three results. Each one points to a different cause:

ResultWhat it meansWhere to look
Connected / succeeded / openA service is listening and the network path allows the connection.The port is fine. If the app still fails, check the app.
Connection refused / closedThe host answered, but nothing listens on that port.Start the service, or check it listens on the right IP and port.
Timed out / no reply / filteredPackets are dropped on the way, usually by a firewall.The host firewall (UFW, firewalld), a cloud security group, or a network firewall.

Check If a Port Is Open on a Remote Host

Run these commands from the client, the machine that needs to connect. That way you test the whole path, including every firewall in between.

Method 1: Using nc (Netcat)

nc is the quickest way to test a single TCP port. It is installed on most systems; if not, install it with sudo apt install netcat-openbsd on Ubuntu/Debian or sudo dnf install nmap-ncat on RHEL, Rocky Linux, AlmaLinux and Fedora.

nc -zv -w 3 192.168.1.100 22
  • -z: only check if the port accepts a connection; send no data.
  • -v: print the result.
  • -w 3: give up after 3 seconds instead of waiting for the system timeout (about two minutes).

On Ubuntu and Debian (OpenBSD netcat), an open port prints:

Output
Connection to 192.168.1.100 22 port [tcp/ssh] succeeded!

A closed port prints nc: connect to 192.168.1.100 port 22 (tcp) failed: Connection refused, and a filtered port prints ... failed: Connection timed out or nothing after the timeout. On RHEL-based systems (Ncat) the messages are Ncat: Connected to 192.168.1.100:22. and Ncat: Connection refused.

Check a range of ports in one command (OpenBSD netcat):

nc -zv -w 2 192.168.1.100 20-25

Method 2: Using Bash /dev/tcp (No Extra Tools)

On a minimal server or inside a container, nc, nmap and telnet are often missing. Bash can open a TCP connection by itself through the special path /dev/tcp/HOST/PORT:

timeout 3 bash -c '</dev/tcp/192.168.1.100/443' && echo "Port is open" || echo "Port is closed or filtered"

timeout 3 stops the test after 3 seconds if packets are being dropped. This works in Bash only, not in sh or dash, which is why the command runs through bash -c.

Method 3: Using nmap

nmap is a port scanner. It is the best tool when you need to check several ports at once, or when you need to tell a closed port from a filtered one. Install it with sudo apt install nmap or sudo dnf install nmap, then run:

nmap -p 22,80,443 192.168.1.100
Output
Starting Nmap 7.98 ( https://nmap.org ) at 2026-11-15 10:20 UTC Nmap scan report for 192.168.1.100 Host is up (0.00042s latency). PORT STATE SERVICE 22/tcp open ssh 80/tcp open http 443/tcp filtered https Nmap done: 1 IP address (1 host up) scanned in 1.32 seconds

Here SSH and HTTP are open, and something drops traffic to port 443, most likely a firewall. If Nmap says “Host seems down”, the host blocks ping; add -Pn to skip that check.

Nmap can also test UDP ports, which /dev/tcp and telnet can’t:

sudo nmap -sU -p 53,123 192.168.1.100

For full scans, service versions and whole subnets, see how to scan open ports with Nmap.

Method 4: Using telnet

telnet is an old remote-login client, but it still works for testing a TCP port and is installed on many systems. Use it only for testing, never for logging in: it sends everything unencrypted.

telnet 192.168.1.100 80

If the port is open, you see:

Output
Trying 192.168.1.100... Connected to 192.168.1.100. Escape character is '^]'.

The cursor then waits for input. Press Ctrl + ], type quit and press Enter to exit. A closed port prints telnet: Unable to connect to remote host: Connection refused. A filtered port stays at Trying 192.168.1.100... until it times out. If telnet is missing, install it with sudo apt install telnet or sudo dnf install telnet, or use one of the methods above.

Check If a Port Is Listening on the Local Machine

If a remote test fails, log in to the server and check that the service is actually listening, and on which address.

Method 5: Using ss

ss is part of iproute2 and is installed on every modern Linux system. It replaces the old netstat command. List all listening TCP and UDP ports with the process that owns each one:

sudo ss -tulpn
Output
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process udp UNCONN 0 0 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=612,fd=14)) tcp LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1021,fd=3)) tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=1290,fd=6)) tcp LISTEN 0 151 127.0.0.1:3306 0.0.0.0:* users:(("mysqld",pid=1144,fd=24))

The options are -t TCP, -u UDP, -l listening only, -p show the process, and -n show numbers instead of service names. To check one port, use a filter instead of grep 80 (which would also match 8080 and 1180):

sudo ss -tlnp 'sport = :80'

Pay attention to the Local Address column. 0.0.0.0 or [::] means the port accepts connections on all interfaces. 127.0.0.1 means it is reachable only from the server itself: in the example above, MySQL is listening, but no other machine can connect to it, whatever the firewall says. Fix that in the service’s configuration (for example bind-address for MySQL), not in the firewall.

Method 6: Using lsof

lsof lists open files, and in Linux network sockets are files too. It is handy for finding which program holds a port:

sudo lsof -i :80
Output
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 1290 root 6u IPv4 23810 0t0 TCP *:http (LISTEN) nginx 1291 www-data 6u IPv4 23810 0t0 TCP *:http (LISTEN)

No output means nothing uses the port. Run it with sudo: without root, lsof only shows your own processes, so a port held by a system service looks free. To list only listening TCP ports, use sudo lsof -iTCP -sTCP:LISTEN -P -n. If the command is missing, install the lsof package.

For more ways to list local ports, see how to check listening ports in Linux.

Shell Script to Check If a Port Is Open

This script checks one or more ports on a host and works without nc, because it uses Bash’s /dev/tcp. It exits with status 1 if any port is closed, so you can use it in monitoring, cron jobs or deployment scripts.

Create the file check-port.sh:

#!/usr/bin/env bash
# Usage: ./check-port.sh HOST PORT [PORT...]

if [ "$#" -lt 2 ]; then
    echo "Usage: $0 HOST PORT [PORT...]"
    exit 2
fi

HOST="$1"
shift
TIMEOUT=3
STATUS=0

for PORT in "$@"; do
    if timeout "$TIMEOUT" bash -c "</dev/tcp/$HOST/$PORT" 2>/dev/null; then
        echo "$HOST:$PORT is open"
    else
        echo "$HOST:$PORT is closed or filtered"
        STATUS=1
    fi
done

exit $STATUS

Make it executable and run it:

chmod +x check-port.sh
./check-port.sh 192.168.1.100 22 80 443
Output
192.168.1.100:22 is open 192.168.1.100:80 is open 192.168.1.100:443 is closed or filtered

Port Is Closed? Troubleshooting Checklist

  1. Is the service running? Check it with systemctl status nginx (use your service name).
  2. Is it listening on the right address and port? Run sudo ss -tlnp on the server. If it shows 127.0.0.1, change the service to listen on 0.0.0.0 or the server’s IP.
  3. Does it work locally? On the server, test nc -zv 127.0.0.1 80. If that works but the remote test doesn’t, the problem is a firewall or the network.
  4. Is the host firewall blocking it? Check sudo ufw status on Ubuntu/Debian or sudo firewall-cmd --list-all on RHEL-based systems. See how to open a port in Linux.
  5. Is a cloud or network firewall blocking it? On AWS, Azure, Google Cloud and most VPS providers, the port must also be allowed in the security group or cloud firewall.

Frequently Asked Questions

How do I check if a port is open in Linux without nc or telnet?

Use Bash’s built-in /dev/tcp: timeout 3 bash -c '</dev/tcp/HOST/PORT' && echo open. It needs no extra packages.

How do I check if a UDP port is open?

Use sudo nmap -sU -p PORT HOST. UDP has no handshake, so a result of open|filtered is common; it means Nmap got no reply. nc -zuv HOST PORT also exists, but it often reports success even when nothing is listening.

Why does the port show as listening on the server but closed from outside?

Either the service listens only on 127.0.0.1, or a firewall blocks the port. Check the Local Address column in ss -tlnp, then the host firewall and any cloud security group.

What replaced netstat for checking ports?

ss. netstat comes from the old net-tools package, which many distributions no longer install by default. ss -tulpn gives the same information as netstat -tulpn.

How do I check if a port is open from Windows?

In PowerShell, run Test-NetConnection 192.168.1.100 -Port 22. TcpTestSucceeded : True means the port is open.

Conclusion

To test a port from another machine, use nc -zv, or Bash’s /dev/tcp when nothing else is installed, and nmap when you need to check many ports or tell a closed port from a filtered one. On the server itself, ss -tulpn and lsof -i show what is listening, on which address, and which process owns it. Comparing the two views (remote test and local listening state) quickly shows whether a problem is the service, the bind address, or a firewall.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top