On this page
- Which netcat do you get on each system?
- How do you check whether a TCP port is open?
- How do you test from the outside before a service exists?
- How do you test UDP ports?
- How do you send a raw request to a service?
- How do you copy a file between two servers?
- How do you keep netcat use safe?
- What do common netcat errors mean?
- Questions
Which netcat do you get on each system?
Several programs share the name netcat, and their flags differ a little. On Ubuntu and Debian the default is the OpenBSD version; on Rocky Linux and AlmaLinux it is Ncat from the Nmap project, which also installs itself as nc:
# Ubuntu 24.04 / 22.04, Debian 12 / 13
sudo apt update && sudo apt install -y netcat-openbsd
# Rocky Linux 9, AlmaLinux 9
sudo dnf install -y nmap-ncat
nc -h 2>&1 | head -n 3The help header tells you which flavour you have: OpenBSD netcat prints "OpenBSD netcat", Ncat prints its own name and version. The examples below use OpenBSD syntax first and mention the Ncat form where it differs.
How do you check whether a TCP port is open?
Use -z (connect without sending data) and -v (report the result). Add -w to give up after a few seconds instead of waiting for the system timeout:
nc -zv -w 3 198.51.100.24 22
nc -zv -w 3 example.net 443
nc -zv -w 3 2001:db8:4f2a::24 443"succeeded" or "Connected to" means something accepted the connection. "Connection refused" means the packet reached the host but nothing listens on that port, so check the service with ss. A timeout means a firewall dropped the packet somewhere on the way, either on the target, in between or on your own side. OpenBSD netcat also scans small ranges with nc -zv -w 2 example.net 20-25; Ncat accepts only one port per call, so loop over a list in the shell or use nmap for larger scans. Only scan systems you run yourself.
How do you test from the outside before a service exists?
Start a temporary listener on the server, then connect from your own computer. This proves that the network path and firewall allow the port before you install the real software:
# on the server
sudo ufw allow 9000/tcp # Ubuntu, Debian
nc -l 9000 # OpenBSD netcat
ncat -l 9000 # Ncat on Rocky and Alma
# on your computer
nc -v 198.51.100.24 9000Type a line on either side and press Enter: it appears on the other side. Press Ctrl+C to stop, then remove the temporary rule with sudo ufw delete allow 9000/tcp. On Rocky and Alma open the port with sudo firewall-cmd --add-port=9000/tcp (without --permanent, so a reload removes it). Ports below 1024 need root to listen. To test for blocked ports in the other direction, see blocked ports checks.
How do you test UDP ports?
Add -u on both sides. UDP has no handshake, so a port scan with -z cannot tell open from filtered reliably; send real data instead:
# on the server
nc -u -l 5000
# on your computer
echo "hello over udp" | nc -u -w 2 198.51.100.24 5000If the text appears on the server, UDP traffic gets through. Nothing appearing means a firewall drops it or the listener is not running. This test is useful before setting up WireGuard, game servers or anything else that uses UDP, because those services often fail silently. Ncat users write ncat -u -l 5000 on the listening side.
How do you send a raw request to a service?
Pipe the protocol text into nc and read what comes back. For HTTP, the request needs CRLF line endings and an empty line at the end:
printf 'HEAD / HTTP/1.1\r\nHost: example.net\r\nConnection: close\r\n\r\n' | nc example.net 80The status line and headers come back, which shows exactly what the web server sends before any redirect or browser cache gets involved. For HTTPS, netcat cannot speak TLS; use curl -I or openssl s_client instead, as covered in curl requests. Many text protocols greet you with a banner, so a plain connect already tells you something:
nc -v -w 5 smtp.example.net 587A line starting with 220 confirms that a mail server answers on the submission port. Type QUIT to leave. Outbound port 25 may be restricted, so ask support by ticket before planning to send mail directly, and use an authenticated relay on port 587 as explained in SMTP relay for apps.
How do you copy a file between two servers?
Start a listener that writes to a file on the receiving server, then feed the file into nc on the sending server:
# receiver (OpenBSD netcat)
nc -l 9000 > backup.tar.gz
# receiver (Ncat)
ncat -l 9000 > backup.tar.gz
# sender (OpenBSD netcat closes when the file ends thanks to -N)
nc -N 198.51.100.24 9000 < backup.tar.gz
# sender (Ncat)
ncat --send-only 198.51.100.24 9000 < backup.tar.gz
# compare on both sides
sha256sum backup.tar.gzMatching checksums prove the copy is complete. You can stream a whole directory the same way with tar czf - /srv/data | nc -N 198.51.100.24 9000 and nc -l 9000 | tar xzf - on the other end. Keep in mind that the data travels unencrypted and anyone who reaches the port first can connect. For anything sensitive, or over the public internet in general, use rsync or scp over SSH, or send the stream through a WireGuard tunnel.
How do you keep netcat use safe?
Treat every netcat listener as a temporary hole in your firewall. A few habits keep it harmless:
- Open the test port only for your own address when you can, for example
sudo ufw allow from 203.0.113.5 to any port 9000 proto tcp. - Stop the listener and remove the rule right after the test; check with
sudo ss -tlnpthat nothing remains. - Never connect a shell to a listener (the
-eoption some builds offer). That creates a remote shell without a password. - Run listeners as a normal user on a high port rather than as root.
What do common netcat errors mean?
"Connection refused" means the host is reachable but nothing listens on the port. A hang followed by a timeout means packets are dropped, usually by a firewall. "Address already in use" on the listening side means another process holds the port; find it with sudo ss -tlnp 'sport = :9000'. "invalid option" errors almost always come from mixing OpenBSD and Ncat flags, so check which one you are running with nc -h. When a connection works from one network but not another, trace the path with mtr to see where it stops.
Questions about Test ports and transfer data with netcat
Is netcat installed by default?
Not always. Some images include it, others do not. Install netcat-openbsd on Ubuntu and Debian or nmap-ncat on Rocky and Alma; both are small.
Can netcat test HTTPS?
It can test that port 443 accepts TCP connections, but it cannot complete the TLS handshake. Use curl -I https://example.net or openssl s_client -connect example.net:443 for that.
Does netcat work over IPv6?
Yes. Give it an IPv6 address directly, or force the family with -6 (or -4) when a name has both A and AAAA records.
Why does nc not exit after sending a file?
OpenBSD netcat keeps the connection open after the input ends unless you pass -N. With Ncat, use --send-only on the sender.
Is netcat a security risk on my server?
The tool itself is harmless. The risk comes from leaving a listener running on an open port. Close listeners and firewall rules as soon as the test is done.
More in Networking and diagnostics
- FreeRADIUS with PostgreSQL
- IPv6 on your server
- The ip command
- DNS lookups with dig
- Listening ports with ss and netstat
- Trace network paths with mtr
- Capture packets with tcpdump
- Live bandwidth with iftop and nethogs
- HTTP requests with curl
- Dynamic DNS updates from a server
- Latency graphs with SmokePing
- Traffic analysis with ntopng
- Check which ports are reachable