How to install and use the ‘nc’ command in Linux?

In the Linux system, the nc (netcat) command is a practical tool for network connections. It can be used to create arbitrary TCP/UDP connections, send any data, and listen on any port. Below are the installation and usage instructions for the nc command:

  1. Install the nc command:
  2. Debian/Ubuntu system: Run the following command in the terminal to install nc: sudo apt-get install netcat
  3. CentOS/RHEL system: Use the following command in the terminal to install nc: sudo yum install nc
  4. Utilize the nc command:
  5. Establish a TCP connection: nc [options] host port
  6. Establish a UDP connection using the command: nc -u [options] host port
  7. Listen on port: nc -l [options] port
  8. Here are some common options for nc commands:
  9. -v: Display detailed output.
  10. Disable DNS resolution
  11. -p: Specify the local port
  12. Set a timeout.
  13. -l: Monitoring mode
  14. -u: Utilizing the UDP protocol.
  15. Here are some examples of usage:
  16. Establish a TCP connection and send data: echo “Hello World” | nc host port
  17. Listen on local port and display received data: nc -l port
  18. Transfer a file to a remote host by running the command: nc host port < file
  19. Receive files sent from a remote host: nc -l port > file.

This is just some basic usage of the nc command, for more advanced usage you can refer to the man page (man nc).

bannerAds