Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
Site Navigation
Linux Forums
Linux Articles
Product Showcase
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds
Free Publications

Netcat or nc in short can be aptly described as one of those two letter command-line tools that have all of legendary UNIX magic and power.

nc however is a new program and does not share the age of well known programs like cat or dd. However its power and versatility make one think why no one came up with this before.

Past is past. Let us worry about the future now. The future is VoIP, multimedia streaming, video on demand...but we are digressing. We are discussing netcat here.

Interestingly netcat is going to help us in future as the best way to debug future network protocols. Peer to peer networking is often touted as the wave of the future Internet and I shall talk about an interesting anecdote of how invaluable netcat proved to me whilst testing my peer topeer NAT traversal library.

It is not very likely you have used it. So let us start with few simple exams before we get on to nontrivial ones.

$nc -l -p 1234

will set up a TCP server on your machine. You can fire up

$nc localhost 1234

on another terminal which will set up a TCP client. You can now start talking between the two. Whatever you type here appears there and vice versa. Simple, isn't it?

on another terminal which will set up a TCP client. You can now start. Moreover it has power. The sort of power that only deceptively simple tools can possess. Let me demonstrate.

$nc -l -u -p 1234

will setup a UDP server. And you can talk to it with

$nc -u localhost 1234

Now, you will argue I am sure that telnet can be used as TCP clients.But it can hardly compare with netcat in its power and versatility as a network tool. Nowadays of course nobody uses telnet. We all use ssh.

Anyway netcat is used along with ssh for HTTP proxy authentication. And netcat can serve as simple inetd replacement, a simple command-line browser and so on. My God! You mean to say that netcat is so powerful?

It just depends on the way you look at it. netcat has a very clear goal and agenda. Of that of doing one thing and doing it well. And you use itas a duct tape for connecting with other similar tools to achieve your goal.

You could use netcat as a simple UDP or TCP based server and spawn a shell command every time a client connects. netcat can be used a simple chat application or even for sniffing network packets. The possibilities are endless.

The time has come to tell you how I used netcat for testing my p2p NAT library.

I used an approach called "UDP hole punching" for making inbound connections to machines behind NAT. Some say "protected by" but I shall avoid using that term.

I had to figure out if my NAT traversal library was doing its job. So once my library created the appropriate NAT mapping, I had to test application traffic. I used netcat for that.

In fact you can transfer files very simply even binary ones.

$nc -l -p 5000 > /tmp/network-file

and connect to this host across the Internet like this.

$nc server.com 5000 < network-file

You can even develop a simple VoIP application using netcat as it simply sets up a network pipe which you can send and receive data transparently.

For that you need to use a nice like G.729 using speex and use netcat's UDP serving capability. Voice traffic is delay sensitive and not loss sensitive and hence UDP is what we care. If you are lucky you could even use shell commands like this.

$ mkfifo /tmp/inputvoice 
$ cat /dev/dsp > /tmp/inputvoice&
$ nc -u 1700 < /tmp/inputvoice > /dev/dsp

On the other side, you do something similar and

$nc -u  1700 < /tmp/inputvoice >/dev/dsp

Of course, the above example is merely an example and not a practical one.

nc can be used for taking remote backups and you can combine it with ssh and cron to do really cool things.

Think of nc as extending the UNIX pipe concept across the network and a whole new world of possibilities open up.

Did I tell you that netcat can do port scanning and network exploration? That is what the man page is for!

Rate This Article: poor excellent
Comments about this article
How is nc new?
writen by: Robert on 2007-03-23 16:08:04
It's been around over a decade. socat is even better: it understands HTTP/SOCKS proxies, pipes, SSL, unix file descriptors, sockets, etc. . .
RE: How is nc new? written by Robert:
writen by: Aaron on 2007-03-23 16:28:40
dude! proofread your work before it gets posted to digg and makes you look like a moron! there's a gigantic cut-and-paste error near the beginning, and all the commands about the voip server came out on one line.
RE: proofread! written by Aaron:
I'm not very impressed so far, mr venkat
writen by: Harry Manback on 2007-03-23 16:58:50
1) "Hobbit (hobbit@avian.org) created netcat in 1995 as a feature-rich network debugging and exploration tool." That means netcat is 12 years old. That's only recent in geological and historical contexts. Not exactly an expert on this are you? 2) "It is not very likely you have used it." Way to condescend to your audience. Maybe give us a bit of credit for having used UNIX and GNU/Linux a time or two before? Ass.
RE: I'm not very impressed so far, mr venkat written by Harry Manback:
Why are you such a cock
writen by: we on 2007-03-23 17:26:39
So the guy doesn't know his stuff. Does that give you the right to behave like a fucking cock trying to sound all knowing. I think not. It's not like he raped your mother or something. It was just an article. Get the fuck over it.
RE: Why are you such a cock written by we:
writen by: LinuxLoader on 2007-06-12 02:18:53
RE: re written by LinuxLoader:
writen by: Dan Farrell on 2007-10-09 20:43:17
2) "It is not very likely you have used it." Way to condescend to your audience. Maybe give us a bit of credit for having used UNIX and GNU/Linux a time or two before? Ass. [/quote] Reading this as condescending requires, I believe, a subtler grasp of the nuances of English than many non-native speakers posess.
RE: Unfair! written by Dan Farrell:
Interesting overview, and helpful!
writen by: Dan Farrell on 2007-10-09 20:45:44
Neat! I am going to use nc all the time now! I was just thinking to myself the other day, 'how can I send my i/o through the network?' now that's a plausibility!
RE: Interesting overview, and helpful! written by Dan Farrell:
writen by: casual reader on 2007-10-19 06:01:46
the writing on all these new articles is terrible.
RE: ugh written by casual reader:
writen by: Greg on 2007-11-28 18:30:15
Well, This has been nice, First of all, Someone shares something that they thought was interesting, And then all these people come in and start picking on Him, That's kinda' unfair, Isn't it? No one said You had to read it, Or reply to it. I thought it was pretty cool.
RE: Ouch written by Greg:
in escrow
writen by: causal reeder on 2008-01-15 12:45:33
hahahahaha hahahahahahahahahaha hahahahahahahahahahahahahahaha [i]hehe[/i] hey at least it's better than windsock
RE: in escrow written by causal reeder:
for everybody complaining ... just shut
writen by: Renan Holanda on 2008-02-19 09:21:59
Wow, somebody actually takes time to write an article with good intent just trying to help, and thats the feed back he gets.. for all of you complaining stop being such little kids and just shut it.. if you all are so damn good I would like to read an article of yours!! and if you say that nc is so old why would you even bother reading the article.. what gets me really mad is not that you give your opinion on it.. but do you really have to be so nasty about it???
RE: for everybody complaining ... just shut written by Renan Holanda:
Better than no article...
writen by: Ted on 2008-03-03 11:54:57
RE: Better than no article... written by Ted:
Haters, get lost!
writen by: francine on 2008-06-07 19:39:38
RE: Haters, get lost! written by francine:
wow! what a bunch of low lives.
writen by: Internet graffiti on 2008-06-13 10:26:46
wow! what a bunch of low lives. Not everyone, Just the losers that need to unhelpful comments. Good job on the post. Please check out cryptcat. Thanks for socat post.
RE: wow! what a bunch of low lives. written by Internet graffiti:
Great Article!!
writen by: Spider on 2008-08-22 01:40:11
RE: Great Article!! written by Spider:
great one
writen by: virus on 2008-12-27 10:56:06
Girish that is a great article.. thks
RE: great one written by virus:
Nice article. Thanks!
writen by: fet on 2009-03-20 11:45:27
RE: Nice article. Thanks! written by fet:
I really like it!
writen by: on 2009-05-27 05:33:02
Well, I'm very thankful to Girish, cause his article made me open my eyes about nc. I didn't know it was possible with that, this means I can do a lot of stuff relating to network debugging and development. Girish, don't listen to haters, they're just a bunch of fakers. I'm a guy from Morocco, imagine a guy somewhere in North-Africa read your article and got very satisfied about your article. That is what you realized with your article. Thanks.
RE: I really like it! written by :
free the network from the noise
writen by: edabxv on 2010-09-24 14:33:56
i been using internet from the days of BB, over 25 years, and still using to find tech info. NOBODY know EVERYTHING.
Once there were People(with big "P") that posted info and still are, like Girish.
Today there are a lot of "noise", people (with a small "p") posting comments and not articles. Go Away, Please!
RE: free the network from the noise written by edabxv:
Great article
writen by: mchacongonzalez on 2010-11-11 18:39:16
Hi Netcat is a good computer networking service that allows you to do an outbound or inbound connections, TCP or UDP, to or from any ports;
full DNS forward/reverse checking, with appropriate warnings; ability to use any local source port; ability to use any locally-configured network source address; and netcat can be used as a proxy because both ports and hosts can be redirected.
RE: Great article written by mchacongonzalez:

Comment title: * please do not put your response text here