The netcat command can be used to test connectivity and send data across network connections. The command may be spelled out as “netcat” or abbreviated as “nc” depending on the distribution. Systems may be identified by IP address or by hostname. When troubleshooting, use netcat to listen on the destination computer and attempt a connection from the source computer in order to verify network functionality.
Syntax
The syntax of the netcat command is:
# netcat [options]
If you need more flexibility in connecting to a remote host than a command like telnet host port allows, use netcat (or nc). netcat can connect to remote TCP/UDP ports, specify a local port, listen on ports, scan ports, redirect standard I/O to and from network connections, and more. To open a TCP connection to a port with netcat, run
# netcat host port
netcat only terminates when the other side of the connection ends the connection, which can confuse things if you redirect standard input to netcat. You can end the connection at any time by pressing CTRL-C. (If you’d like the program and network connection to terminate based on the standard input stream, try the sock program instead.)
To listen on a particular port, run
# netcat -l -p port_number
netcat Command Examples
1. Connect two computers for the purpose of transferring information:
On comp1 (listen on port):
# netcat -l 4242
On comp2 (connect to listener):
# netcat comp1
2. Transfer file content between two computers:
On comp1 (listen on port):
# netcat -l 4242 > received.file
On comp2 (connect to listener):
# netcat comp1
3. Port scan a computer
# netcat -z -v domain.tld 1-1000 ### scans ports 1 to 1000
Conclusion
The netcat utility can read from and write to any network port, making it a virtual Swiss army knife for the networking world. You can use netcat to test just about any type of network situation, including building your very own client/server test tool. What makes netcat even more versatile is that it accepts input from redirection and piping, so you can easily use it in scripts to create simple ad hoc servers and clients for just about any network testing you need to do.