Netcat (NC) is a toolkit for debugging and checking networks under Linux. It can create TCP/IP connections and process TCP/UDP sockets.
Here, we will learn Netcat commands using examples.
The Netcat tool can run in server mode and listen to a specified port:
$ nc -l 2389
Then, you can use client mode to connect to port 2389:
$ nc localhost 2389
Now, if you enter some text, it will be sent to the server:
$ nc localhost 2389
HI, server
The following will be displayed in the terminal window of the server:
$ nc -l 2389
HI, server
The Netcat tool can also be used to transfer files. Let's suppose we have a testfile file on the client side:
$ cat testfile
hello server
There is an empty file named test on the server side.
Then, we use the following command to enable the server side:
$ nc -l 2389 > test
Run the client:
$ cat testfile | nc localhost 2389
Then, you stop the server side, and you can check that the test content is the testfile file sent by the client just now:
$ cat test
hello server
In most cases, we do not want the connection to be maintained all the time, so we can use the -w parameter to specify the idle timeout period of the connection. This parameter is followed by a value representing the number of seconds. If the connection exceeds the specified time, the connection will be terminated.
Server:
$ nc -l 2389
Client:
$ nc -w 10 localhost 2389
The connection will be interrupted after ten seconds.
Note: Do not use the -w and -l parameters at the same time on the server side because the -w parameter will not affect the server side.
The -4 and -6 parameters of Netcat are used to specify IP address types, which are IPv4 and IPv6, respectively:
Server:
$ nc -4 -l 2389
Client:
$ nc -4 localhost 2389
Then, we can use the Netstat command to view the network:
$ netstat | grep 2389
tcp 0 0 localhost:2389 localhost:50851 ESTABLISHED
tcp 0 0 localhost:50851 localhost:2389 ESTABLISHED
Next, let's look at IPv6:
Server:
$ nc -6 -l 2389
Client:
$ nc -6 localhost 2389
Run the Netstat command again:
$ netstat | grep 2389
tcp6 0 0 localhost:2389 localhost:33234 ESTABLISHED
tcp6 0 0 localhost:33234 localhost:2389 ESTABLISHED
A prefix of tcp6 indicates an IPv6 address is used.
This function uses the -d parameter. Please see the following example:
Server:
$ nc -l 2389
Client:
$ nc -d localhost 2389
Hi
The Hi text you enter will not be sent to the server.
If the client connected to the server disconnects, the server will also exit.
Server:
$ nc -l 2389
Client:
$ nc localhost 2389
^C
Server:
$ nc -l 2389
$
In the preceding examples, the server side also exits immediately when the client is disconnected.
We can use the -k parameter to control the server. It will not exit due to the disconnection of the client.
Server:
$ nc -k -l 2389
Client:
$ nc localhost 2389
^C
Server:
$ nc -k -l 2389
The Netcat client can use the -q parameter to control how long it takes to exit after receiving the EOF. The unit of this parameter is seconds:
The client starts in the following way:
$ nc -q 5 localhost 2389
Now, if the client receives the EOF, it will wait five seconds and exit.
Netcat uses TCP protocol by default, but it also supports UDP. You can use the -u parameter to enable UDP protocol communication.
Server:
$ nc -4 -u -l 2389
Client:
$ nc -4 -u localhost 2389
This way, the client and server use the UDP protocol, which can be viewed through the Netstat command:
$ netstat | grep 2389
udp 0 0 localhost:42634 localhost:2389 ESTABLISHED
Disclaimer: This is a translated article from Linux China, all rights reserved to the original author. The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.
1,003 posts | 246 followers
FollowAlibaba Cloud Community - June 6, 2022
William Pan - February 6, 2020
Apache Flink Community China - August 2, 2019
Alibaba Cloud Community - April 7, 2024
Alex - February 14, 2020
Alibaba Clouder - February 28, 2018
1,003 posts | 246 followers
FollowAlibaba Cloud Linux is a free-to-use, native operating system that provides a stable, reliable, and high-performance environment for your applications.
Learn MoreLimited Offer! Only $4.90/1st Year for New Users.
Learn MoreExplore Web Hosting solutions that can power your personal website or empower your online business.
Learn MoreExplore how our Web Hosting solutions help small and medium sized companies power their websites and online businesses.
Learn MoreMore Posts by Alibaba Cloud Community