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:
- Install the nc command:
- Debian/Ubuntu system: Run the following command in the terminal to install nc: sudo apt-get install netcat
- CentOS/RHEL system: Use the following command in the terminal to install nc: sudo yum install nc
- Utilize the nc command:
- Establish a TCP connection: nc [options] host port
- Establish a UDP connection using the command: nc -u [options] host port
- Listen on port: nc -l [options] port
- Here are some common options for nc commands:
- -v: Display detailed output.
- Disable DNS resolution
- -p: Specify the local port
- Set a timeout.
- -l: Monitoring mode
- -u: Utilizing the UDP protocol.
- Here are some examples of usage:
- Establish a TCP connection and send data: echo “Hello World” | nc host port
- Listen on local port and display received data: nc -l port
- Transfer a file to a remote host by running the command: nc host port < file
- 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).