// DevOps
Netcat: reading and writing over the network — diagnostics, data transfer, tunnels
Published on 2026-09-22
Netcat (nc) reads and writes data over TCP and UDP connections. With one small utility it’s convenient to check a port, grab a service banner, transfer a file between machines, or create a temporary tunnel. Below — typical tasks.
There are several implementations of Netcat, and their flags differ. By default installed are:
- OpenBSD netcat — in Debian and Ubuntu (package
netcat-openbsd), Fedora, Arch, Alpine. Examples below are for it; - traditional (GNU) netcat — the old implementation, it has a
-eflag to run a program on the connection, which the OpenBSD version lacks; - Ncat from the Nmap package — a separate implementation with TLS, proxy support and handling multiple clients.
Check your version: nc -h outputs the list of supported flags.
Checking ports and services
ping uses only ICMP and doesn’t tell if the specific port is open. Netcat checks the actual connection:
nc -zv -w 3 example.ru 80 443 22-z— just check the connection, do not send data;-v— verbose output;-w 3— timeout 3 seconds.
Grab a service banner — send a request and read the response:
printf 'HEAD / HTTP/1.0
' | nc -w 3 example.ru 80Transferring files between machines
When you don’t have scp or rsync at hand. On the receiving side:
nc -l -p 9899 > backup.isoOn the sending side:
nc 192.0.2.10 9899 < backup.isoNetcat does not encrypt data or verify integrity, so use this method only within a trusted network. For large files data is compressed on the fly:
# receive
nc -l -p 9899 | gzip -dc > backup.iso
# send
gzip -c backup.iso | nc 192.0.2.10 9899After transfer, verify the checksum (sha256sum) on both sides.
Temporary TCP tunnel
You can forward a local port to an internal service using a named pipe:
mkfifo /tmp/backpipe
nc -l 8080 < /tmp/backpipe | nc db.internal 5432 > /tmp/backpipeNow connections to local port 8080 go to the internal PostgreSQL. This is a one-off unencrypted solution for debugging; for persistent access use an SSH tunnel or VPN.
When netcat isn’t enough
- Ncat (package
nmap) — TLS, proxy, multiple clients simultaneously, flag--exec; - socat — bidirectional transfer between almost any sources (sockets, files, devices, TLS), syntax is more complex.
sudo apt install nmap socatHow to defend against netcat in the wrong hands
The same properties make netcat an attacker tool: having gained access to a server, an attacker can open an outbound connection to bypass an incoming firewall. What reduces the risk:
- Filter outgoing traffic. By default a server is allowed to make any outgoing connections. Allow only the necessary directions and block the rest — a reverse connection to an arbitrary port will not succeed:
# only DNS, HTTP and HTTPS out, everything else — deny
ufw default deny outgoing
ufw allow out 53
ufw allow out 80
ufw allow out 443- Don’t keep netcat, compilers and unnecessary interpreters on a production server — their absence makes persistence harder.
- Watch for unexpected processes and connections. An outgoing connection from
nc,bashorshto an external address is a reason for concern:
ss -tnp | grep -E 'nc|bash|sh'- Set up auditing. An auditd rule notes creation of named pipes (
mkfifo,mknod) — a typical indicator of a reverse shell without the-eflag:
auditctl -a always,exit -F arch=b64 -S mknod -S mknodat -k reverse_shellHost-based detection systems (auditd with ready rules, Falco, EDR) detect such attacks comprehensively — they see suspicious combinations like “shell process + network connection”. An outbound firewall and a minimal set of software on the server mitigate the simplest scenarios.
Summary
Netcat moves bytes from stdin to the network and back, so it composes well with ordinary shell commands — gzip, dd, pipes. It’s enough for checking ports, one-off file transfer and quick tunneling; for encryption and persistent services use Ncat, socat, SSH or a VPN.
Sources:
// Reviews
Related reviews
I came with an expensive request to configure a VPS server, but during the consultation Mikhail suggested a much simpler, more affordable solution. In the end I saved time and money. Mikhail — a true expert who works for the client's result, not for the fee. I recommend him!
I came with an expensive request to configure a VPS server, but during the consultation Mikhail suggested a much simpler and more cost-effective solution. In the end I saved budget and time. Mikhail — a true expert who …
VPS setup, server setup
2026-05-12 · ★ 5/5
Excellent work! Set up the server very quickly, installed the control panel, and configured the IP. Definitely recommend!
Excellent work! Very quickly set up the server, installed the panel, configured the IP I can definitely recommend it!
Everything was excellent; helped promptly and professionally. Thank you — I recommend them to the community.
Everything's great, helped promptly and professionally, thank you, I recommend it to the community
VPS setup, server setup
2026-04-16 · ★ 5/5
There were several issues concerning both the technical side and overall understanding. Mikhail responded quickly, resolved the technical problems, and helped me understand them — many thanks. I'm satisfied with the result.
There were several issues concerning both the technical side and overall understanding. Mikhail responded quickly to the request, helped sort things out and resolved the technical problems and helped clarify …
VPS setup, server setup
2026-02-18 · ★ 5/5
Everything was done quickly and efficiently. I recommend.
Everything was done quickly and efficiently. I recommend.
VPS setup, server setup
2026-01-17 · ★ 5/5
Everything went well; the contractor responded quickly to questions and helped resolve the issue. Thanks!
Everything went well, the contractor responded quickly to questions and helped resolve the issue. Thank you!
VPS setup, server setup
2025-12-16 · ★ 5/5
// Contact
Need help?
Get in touch with me and I'll help solve the problem
I reply within one business day (03:00-13:00 GMT)
Или оставьте заявку здесь:
// Related