8 Practical Linux Netcat NC Command Examples
8 Practical Linux Netcat NC Command Examples
8 Practical Linux Netcat NC Command Examples
Menu
Home
Free eBook
Start Here
Contact
About
This utility can be used for creating TCP/UDP connections and investigating them. The biggest use of this
utility is in the scripts where we need to deal with TCP/UDP sockets.
In this article we will learn about the netcat command by some practical examples.
Also, it can be used in client mode trying to connect on the port(2389) just opened
$ nc localhost 2389
Now, if we write some text at the client side, it reaches the server side. Here is the proof :
$ nc localhost 2389
HI, server
http://www.thegeekstuff.com/2012/04/nc-command-examples/?utm_source=feedburner 1/11
11/13/2017 8 Practical Linux Netcat NC Command Examples
$ nc -l 2389
HI, server
So we see that netcat utility can be used in the client server socket communication.
Now, when we see the test file at the server end, we see :
$ cat test
hello test
So we see that the file data was transfered from client to server.
There are cases when we do not want a connection to remain open forever. In that case, through -w switch we
can specify the timeout in a connection. So after the seconds specified along with -w flag, the connection
between the client and server is terminated.
Server :
nc -l 2389
Client :
$ nc -w 10 localhost 2389
http://www.thegeekstuff.com/2012/04/nc-command-examples/?utm_source=feedburner 2/11
11/13/2017 8 Practical Linux Netcat NC Command Examples
NOTE : Do not use the -w flag with -l flag at the server side as in that case -w flag causes no effect and hence
the connection remains open forever.
Server :
$ nc -4 -l 2389
Client :
$ nc -4 localhost 2389
The first field in the above output would contain a postfix 6 in case the IPV6 addresses are being used. Since
in this case it is not, so a connection between server and client is established using IPV4 addresses.
Server :
$ nc -6 -l 2389
Client :
$ nc -6 localhost 2389
So now a postfix 6 with tcp shows that nc is now using IPV6 addresses.
This functionality can be achieved by using the flag -d. In the following example, we used this flag at the client
side.
Server :
$ nc -l 2389
Client :
$ nc -d localhost 2389
Hi
The text Hi will not be sent to the server end as using -d option the read from stdin has been disabled.
Server :
$ nc -l 2389
Client :
$ nc localhost 2389
^C
Server :
$ nc -l 2389
$
So, in the above example we see that as soon as the client got disconnected the server was also terminated.
This behavior can be controlled by using the -k flag at the server side to force the server to stay up even after
the client has disconnected.
Server :
$ nc -k -l 2389
Client :
$ nc localhost 2389
^C
Server :
$ nc -k -l 2389
So we see that by using the -k option the server remains up even if the client got disconnected.
nc -q 5 localhost 2389
Now if the client ever receives an EOF then it will wait for 5 seconds before terminating.
By default all the sockets that nc utility creates are TCP protocols but this utility also works with UDP protocol.
To enable UDP protocol the -u flag is used.
Server :
$ nc -4 -u -l 2389
Client :
$ nc -4 -u localhost 2389
Now, both the server and client are configured to use UDP protocol. This can be confirmed by the following
netstat command. So we see that this connection is now using the UDP protocol.
http://www.thegeekstuff.com/2012/04/nc-command-examples/?utm_source=feedburner 4/11
11/13/2017 8 Practical Linux Netcat NC Command Examples
$ netstat | grep 2389
udp 0 0 localhost:42634 localhost:2389 ESTABLISHED
Thanks for this overview. I just encountered an nc snippet in a script i have to debug, and as i never
used it myself i guess this page will help me with this task.
Link
Himanshu April 23, 2012, 2:34 am
@Pierre B
Thanks!!
Link
Saurabh April 23, 2012, 3:31 am
Link
rinku April 23, 2012, 4:26 am
Great .
Link
Shaheem April 23, 2012, 2:11 pm
http://www.thegeekstuff.com/2012/04/nc-command-examples/?utm_source=feedburner 5/11
11/13/2017 8 Practical Linux Netcat NC Command Examples
Always great articles on this site. Is there any way to use netcat for multicasting udp or is there a different
util for that? And for either answer if there is, will you be covering examples of it please?
Link
shark April 24, 2012, 6:15 am
Link
Mohan April 24, 2012, 12:45 pm
Link
Anonymous April 26, 2012, 10:39 am
Typo in section 3:
NOTE : Do not use the -w flag with -l flag at the server side as in that case -w flag causes no AFFECT
and hence the connection remains open forever.
should read
NOTE : Do not use the -w flag with -l flag at the server side as in that case -w flag causes no EFFECT
and hence the connection remains open forever.
Link
Ramesh Natarajan April 26, 2012, 11:42 pm
@Anonymous,
Link
Tanky Woo January 13, 2013, 8:04 am
Link
SomeGuy March 13, 2013, 7:10 pm
Tanky Woo: There are at least two different versions of netcat around (traditional and openbsd, both
available in Ubuntu etc). The traditional one requires -p to specify the port, while its optional in the
openbsd version. At some point, some version (some older openbsd version?) complained if you used -p.
Link
James May 22, 2013, 6:25 am
Hi guys,
Im trying to understand NC a little better and this is a great tutorial! Im trying to redirect a com port via
udp, but starting out, Im just testing a server that stays alive.
The following works fine, but if I disconnect the client, The server stays live, but when I reconnect again,
no data is passed and the client drops back to the shell after attempting to transmit. Any thoughts?
Sever:
nc -k -l -4 -u 2221
http://www.thegeekstuff.com/2012/04/nc-command-examples/?utm_source=feedburner 6/11
11/13/2017 8 Practical Linux Netcat NC Command Examples
Client
nc -4 -u localhost 2221
Link
unggul August 11, 2013, 4:13 am
here is my code
is that right ?
because on my android phone when receive the packet data is success,but sometimes jammed .
Thanks for advance !
Link
Joe Antkowiak December 29, 2014, 4:43 pm
UDP doesnt work the way you have it defined here, there is no such thing as a UDP connection
UDP is connection-less and only listens for data packets on a given port, and NC behaves quite
differently with UDP than it does with TCP because of this.
Your output showing a UDP Connection appears to be fabricated. Netstat will never show you an
established state for anything UDP because there are no connections to establish. It wont even show
you client addresses because there are no connections, just packets coming in on a given port from any
source.
Link
Marius Constantinescu September 25, 2015, 7:53 am
thank you!
Link
Brock Judd April 13, 2016, 9:48 pm
Link
PK April 5, 2017, 6:12 am
Link
Pankaj Kumar Saini May 6, 2017, 8:13 am
I googled the problem but could not found satisfactory answers. It will be very helpful if you can reply
the solution. Thanks very much.
Link
Leave a Comment
http://www.thegeekstuff.com/2012/04/nc-command-examples/?utm_source=feedburner 7/11
11/13/2017 8 Practical Linux Netcat NC Command Examples
Name
Website
Comment
Submit
Next post: How to Create Linux Proc Files in C Program using LKM
adlinktech.com/Top+vendor
EBOOKS
Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build a Strong Foundation in Linux
Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting
Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk
Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor
Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well
http://www.thegeekstuff.com/2012/04/nc-command-examples/?utm_source=feedburner 8/11
11/13/2017 8 Practical Linux Netcat NC Command Examples
POPULAR POSTS
CATEGORIES
Linux Tutorials
Vim Editor
Sed Scripting
Awk Scripting
Bash Shell Scripting
Nagios Monitoring
http://www.thegeekstuff.com/2012/04/nc-command-examples/?utm_source=feedburner 9/11
11/13/2017 8 Practical Linux Netcat NC Command Examples
OpenSSH
IPTables Firewall
Apache Web Server
MySQL Database
Perl Programming
Google Tutorials
Ubuntu Tutorials
PostgreSQL DB
Hello World Examples
C Programming
C++ Programming
DELL Server Tutorials
Oracle Database
VMware Tutorials
Ramesh Natarajan
Follow
Contact Us
Email Me : Use this Contact Form to get in touch me with your comments, questions or suggestions about this
site. You can also simply drop me a line to say hello!.
Follow us on Google+
Follow us on Twitter
Support Us
http://www.thegeekstuff.com/2012/04/nc-command-examples/?utm_source=feedburner 11/11