This cheat sheet is designed to briefly reference some of the most commonly used Netcat commands, along with a brief description and practical examples to illustrate their use. Whether you’re a seasoned professional looking to brush up on your command-line skills or a newcomer eager to explore the possibilities of networking through Netcat, you’ll find valuable insights and shortcuts here.
Installation:
Distribution | Command |
---|---|
Debian, Ubuntu | sudo apt-get install netcat |
CentOS/Fedora/Red Hat | sudo dnf install nc |
NixOS | nix-shell -p netcat-gnu for a temporary installation environment. |
Usage:
Task | Command |
---|---|
Listen on a port | nc -l 1234 |
Connect to a port | nc example.com 1234 |
Transfer files (Receiver) | nc -l 1234 > file.txt |
Transfer files (Sender) | nc example.com 1234 < file.txt |
Chat server | nc -l 1234 |
Chat client | nc example.com 1234 |
Port scanning | nc -zv example.com 20-30 |
File transfer with tar (Sender) | tar cf - . | nc -w 3 example.com 1234 |
File transfer with tar (Receiver) | nc -l 1234 | tar x |
Execute command upon connection | nc -l 1234 -e /bin/bash |
Connect via UDP | nc -u example.com 1234 |
Verbose output | nc -v example.com 1234 |
Listen with specific source IP | nc -l 1234 -s 10.0.0.1 |
Send a text message to a port | nc -lk 1234 |
Use with IPv6 | nc -6 example.com 1234 |
Bind to a specific local port (client) | nc -p 8080 example.com 1234 |
Transfer directory with tar and netcat (Sender) | tar czf - directory/ | nc -w 3 example.com 1234 |
Transfer directory with tar and netcat (Receiver) | nc -l 1234 | tar xzf - |
Test if the port is open | echo "Hello World" | nc example.com 1234 |
Receive file and print on console | nc -l 1234 > /dev/stdout |
Test if port is open | echo -n | nc -w 1 example.com 1234 |
Use custom timeout | nc -w 10 example.com 1234 |
Proxy with Netcat | nc -l 1234 | nc example.com 5678 |
Create a simple HTTP server | echo -e "HTTP/1.1 200 OK\r\n\r\nHello World" | nc -l 1234 |
Send file with UDP (Sender) | nc -u example.com 1234 < file.txt |
Receive file with UDP (Receiver) | nc -ul 1234 > file.txt |
Check mail server (SMTP) interaction | nc -C example.com 25 |
Echo service emulation | nc -l 1234 -c 'xargs -n1 echo' |
Listen on multiple ports (using -k for persistent listening and a loop) | echo -n "example.com" | nc 8.8.8.8 53 |
Test if the port is open | while true; do nc -lk 1234; done |
This Netcat cheat sheet has highlighted the tool’s flexibility and power for everything from simple tasks to advanced operations. Mastering Netcat can significantly boost your networking skills. Keep practising, exploring further resources, and share your findings to deepen your understanding and contribute to the community’s collective knowledge.