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 port 80, 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, and output_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 to 100 on example.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!

  • flux ( flux@beehaw.org ) 
    link
    fedilink
    arrow-up
    1
    ·
    3 years ago

    nc is nice an simple tool that’s been useful for quite a bit of times in the past.

    However, perhaps take a look at socat, which is a lot more versatile tool for connecting two kinds of file descriptors, that may refer to files, standard input, standard input with readline, TCP/UDP/SCTP/UNIX/multicast, supports proxies—and also handles end-of-stream properly that I at least have had trouble with nc, but maybe it’s fixed nowadays.

    Basic socat invocation: socat - tcp4:1.2.3.4:22. Can also do tcp4-listen:1234 and indeed by combining those you get a TCP forwarder. My most common use case is as an ssh ProxyCommand, socat - tcp4:%u:%p. (Well not that directly, but effectively doing that from within a shell script that decides where to connect, to handle a changing IP.)

    socat doesn’t come with port scanning though, but perhaps better use specialized tools, such as strobe or nmap for that use anyway.

    There’s also a modern alternative to socat: websocat. It’s name-sake trick is to work with websockets, but seems to also do most of what socat can plus it can also do filtering with “overlays”. I haven’t really used that tool, though, but maybe I will if I need to deal with websockets from command line.