cross-posted from: https://lemmy.run/post/10044
Beginner’s Guide to nc (Netcat)
Welcome to the beginner’s guide to nc (Netcat)! Netcat is a versatile networking utility that allows you to read from and write to network connections using TCP or UDP. It’s a powerful tool for network troubleshooting, port scanning, file transfer, and even creating simple network servers. In this guide, we’ll cover the basics of nc and how to use it effectively.
Installation
To use nc, you first need to install it on your system. The installation process may vary depending on your operating system. Here are a few common methods:
Linux
On most Linux distributions, nc is usually included by default. If it’s not installed, you can install it using your package manager. For example, on Ubuntu or Debian, open a terminal and run:
sudo apt-get install netcat
macOS
macOS doesn’t come with nc pre-installed, but you can easily install it using the Homebrew package manager. Open a terminal and run:
brew install netcat
Windows
For Windows users, you can download the official version of nc from the Nmap project’s website. Choose the appropriate installer for your system and follow the installation instructions.
Basic Usage
Once you have nc installed, you can start using it to interact with network connections. Here are a few common use cases:
Connect to a Server
To connect to a server using nc, you need to know the server’s IP address or domain name and the port number it’s listening on. Use the following command:
nc <host> <port>
For example, to connect to a web server running on
example.com
on port80
, you would run:
nc example.com 80
Send and Receive Data
After establishing a connection, you can send and receive data through nc. Anything you type will be sent to the server, and any response from the server will be displayed on your screen. Simply type your message and press Enter.
File Transfer
nc can also be used for simple file transfer between two machines. One machine acts as the server and the other as the client. On the receiving machine (server), run the following command:
nc -l <port> > output_file
On the sending machine (client), use the following command to send a file:
nc <server_ip> <port> < input_file
The receiving machine will save the file as
output_file
. Make sure to replace<port>
,<server_ip>
,input_file
, andoutput_file
with the appropriate values.Port Scanning
Another useful feature of nc is port scanning. It allows you to check if a particular port on a remote machine is open or closed. Use the following command:
nc -z <host> <start_port>-<end_port>
For example, to scan ports
1
to100
onexample.com
, run:
nc -z example.com 1-100
Conclusion
Congratulations! You’ve learned the basics of nc and how to use it for various network-related tasks. This guide only scratches the surface of nc’s capabilities, so feel free to explore more advanced features and options in the official documentation or online resources. Happy networking!
I tried port scanning part on 127.0.0.1, there was no output until I supplied
-v
.Command I used with success:
nc -z 127.0.0.1 1-65535 -v 2>&1 | grep succeeded
Edit: I installed openbsd version of netcat
Hmm, OpenBSD commands sometime have different behavior than Linux.
I know as I had run into issues with rsync earlier where some options I used on Linux didn’t work as same on FreeBSD/OpenBSD.