Skip to content
Log In Sign Up info@muvcloud.com

Test Whether a Port Is Open

nc -zv 203.0.113.10 22

-z only checks the connection and sends no data, and -v prints the result. A success message means the port accepts connections. "Connection refused" means the host answered but nothing listens there. No answer until a timeout usually means a firewall drops the traffic.

Use nc only against hosts you own or administer. Scanning other people's systems can break their rules and the terms of your provider.

Install nc

There are several netcat programs with slightly different options:

sudo apt install netcat-openbsd   # Debian and Ubuntu
sudo dnf install nmap-ncat        # AlmaLinux and Rocky Linux

Both provide the nc command. On AlmaLinux and Rocky Linux it is ncat, from the Nmap project. Run nc -h to see which one you have.

Add a Timeout

nc -zv -w 3 203.0.113.10 443

-w 3 gives up after three seconds instead of waiting for the system timeout. Use it in scripts, together with the exit code: 0 for success, non-zero for failure.

nc -z -w 3 example.com 443 && echo open || echo closed

Several Ports and UDP

nc -zv 203.0.113.10 80 443
nc -zv 203.0.113.10 8000-8010
nc -zvu 203.0.113.10 53

Several ports and port ranges work in OpenBSD netcat; ncat takes one port per command, so run one test for each port. -u switches to UDP. UDP has no handshake, so a UDP test is less reliable: a port can look open while nothing listens, unless the host sends back an error. To test DNS, for example, a real query with dig tells you more; see Linux network commands.

Listen on a Port

nc can act as a simple server, which helps you test a firewall rule before the real service exists. On the server:

nc -l 8080

From your computer, connect and type a line; it appears on the server:

nc 203.0.113.10 8080

If the connection fails, the firewall blocks the port. Open it only for the test, and close it again afterward. Press Ctrl+C to stop listening. -k keeps listening after the first client leaves. Old "traditional" netcat needs nc -l -p 8080. To see what is already listening, use lsof or ss.

Talk to a Service by Hand

printf 'HEAD / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n' | nc example.com 80
nc -v example.com 25

The first line sends a minimal HTTP request and prints the response headers. The second shows the greeting line of a mail server, which tells you that the server answers and what name it gives. Type QUIT to close the session.

Move a File Between Two Servers

On the receiving server:

nc -l 9000 > backup.tar.gz

On the sending server, with OpenBSD netcat:

nc -N 203.0.113.10 9000 < backup.tar.gz

-N closes the connection when the file is sent. With ncat, use --send-only instead. Compare the files with sha256sum on both sides. The data is not encrypted and anyone who reaches the port can connect, so use this only on a private network and close the port afterward. Over the internet, use scp.

OpenBSD netcat vs ncat

TaskOpenBSD ncncat
Listennc -l 8080nc -l 8080
Port rangeYesNo
Close after sending-N--send-only
TLSNo (Debian, Ubuntu)--ssl

If a port test fails on SSH after a change, check how to change the SSH port for the firewall and SELinux steps.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations