Netcat - Basic Usage and Overview
Last Updated :
28 Oct, 2024
Netcat is a versatile Unix utility that facilitates reading and writing data across network connections using either TCP or UDP protocols. Often referred to as the "Swiss Army knife" of networking, Netcat can perform a wide range of tasks, including connecting to remote servers, listening for incoming connections, and transferring files.
Netcat is a Unix utility that reads and writes data across network connections using TCP or UDP protocol.
The following tasks can be done easily with Netcat:
- Connect to a port of a target host.
- Listen to a certain port for any inbound connections.
- Send data across client and server once the connection is established.
- Transfer files across the network once the connection is established.
- Can execute programs and scripts of the client on the server and vice versa.
- Can Provide remote shell access of server to a client where shell commands can be executed.
Syntax
nc [options] [hostname] [port]
This command establishes connections or listens for incoming connections, depending on the specified options.
Basic Example
A simple client-server connection:
Type this command on the server machine.
nc -l -p 1234
Here, nc stands for Netcat, that we are calling the Netcat program. -l option tells the program to listen on a port specified by -p option. In this case, it is 1234. Now type the following on the client machine or on the other terminal:
nc 127.0.0.1 1234

This will create a TCP connection with the IP address(that is, 127.0.0.1) on the specified port(that is, 1234).
Common Options Used with Netcat
Some important options that can be used with Netcat:
1. Verbose Mode
Verbose, prints additional information about the connection.
#command for terminal 1
nc -vlp 1234
#command for terminal 2
nc -v 127.0.0.1 1234

The above command on the client is showing it has successfully connected to the server. This command can also be used to scan a port of the server if it is open or not.
2. Wait Before Terminating
After data transfer wait w seconds before terminating the connection.

#command for terminal 1
nc -w 20 -lp 1234
#command for terminal 2
nc -w 2 127.0.0.1 1234
3. Simple Chat and Data Transfer
To perform simple chat and data transfer:
#command for terminal 1
nc -lp 1234
#command for terminal 2
nc 127.0.0.1 1234

Use the above sequence of command to send the messages or data from one terminal and one ip to the other
4. File Transfer
To perform file transfer:
#command for terminal 1
nc -v -w 30 -l -p 1234 >manav.txt
#command for terminal 2
nc -v -w 2 127.0.0.1 1234<manav.txt

In this example, the server will terminate the connection 30 seconds after receiving the file. If the file is not in the current directory, then specify the entire path.
5. Execute Shell Command
To execute shell command after successful establishment of connection
#command for terminal 1
nc -lp 1234 -c /bin/sh
#command for terminal 2
nc 127.0.0.1 1234

/bin/sh is a Unix command which provides a shell to execute shell commands. This will provide a remote shell to the client, from where the client can execute shell command on the server.
Some important points on Netcat
- By default, Netcat uses TCP connection. To establish a UDP connection -u option is used.
- Without the -w option the connection doesn't terminate until quitting the Netcat program.
- -n option specifies a numerical IP address, not a domain name. That is, -n option allows only an IP address with which to connect but cannot resolve a domain name to IP address.
- -k option is used in listen mode to accept multiple connections.
Conclusion
Netcat is a powerful tool for network communication that simplifies various tasks such as connecting to ports, transferring files, and providing remote shell access. Its versatility and ease of use make it a valuable asset for network administrators and developers alike. Understanding its commands and options can significantly enhance productivity and efficiency in network management.
Similar Reads
Practical Uses of nc(netcat) command in Linux Netcat is one of the most powerful networking tools, security tools, and network monitoring tools. It is even considered a Swiss army knife of networking tools. It acts like a cat command over a network. It is generally used for the following reasons:Operation related to TCP, UDP, or UNIX-domain soc
7 min read
Top 30 Basic NMAP Commands for Beginners For a cybersecurity enthusiast or professional, mastering network scanning tools is very important to comprehend and assess the security position of networks and systems. Nmap, an influential and versatile Network Mapper, is the network reconnaissance and vulnerability detection go-to tool. As you b
15 min read
Linux Network Commands Cheat Sheet Linux is a very popular operating system. Many people use it. Developers and network admins need to know Linux network commands well. This article will going explains most of the common Linux network commands in a very easy way and It also has a cheat sheet about these commands. The cheat sheet tell
9 min read
Command-Line Tools and Utilities For Network Management in Linux If you are thinking of becoming a system administrator, or you are already a system admin, then this article is for you.As a system admin, your daily routine will include configuring, maintaining, troubleshooting, monitoring, securing networks, and managing servers within data centers. Network confi
8 min read
Mininet Commands Mininet is a network emulator used to create virtual network topologies. It allows you to simulate complex networks on a single machine by creating virtual hosts, switches, routers, and links. This is useful for testing before deploying them in real-world scenarios.Running sudo mn in your terminal w
3 min read
Class 10 Computer Application Unit 1 Notes - Networking This Unit 1 Class 10th Computer Application Syllabus covers everything a Class 10 student needs to know. Based on the latest NCERT guidelines, these notes provide a comprehensive overview of key concepts and include important questions designed to help you score well in both your class tests and fin
15+ min read
Introduction of Novell NetWare Novell NetWare is type of Network Operating System. It provides wide networking services ranging from easy and simple file to network user, data, security, and even resource management. It is generally designed for networks or Local Area Network (LAN) operating system. Novell NetWare is most popular
3 min read
What is Computer Networking? Computer networking is like having a group of friends who all have phones and can call or text each other. In computer networking, instead of phones, we have computers and instead of phone lines, we use cables, Wi-Fi, or other methods to connect them. When computers are connected to a network, they
15+ min read
Class 10 Computer Application Notes If you're a Class 10 student looking for reliable study material, our Class 10 Computer Application Notes - PDF Download is just what you need. These notes are designed to cover the entire Class 10 Computer Application syllabus, offering clear explanations of key concepts like programming, internet
5 min read
Introduction to Internet Computers and their structures are tough to approach, and it is made even extra tough when you want to recognize phrases associated with the difficulty this is already utilized in regular English, Network, and the net will appear to be absolutely wonderful from one some other, however, they may seem
10 min read