1NC(1)                     BSD General Commands Manual                    NC(1)
2

NAME

4     nc — arbitrary TCP and UDP connections and listens
5

SYNOPSIS

7     nc [-46DdhklnrStUuvzC] [-i interval] [-p source_port]
8        [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_protocol] [-x
9        proxy_address[:port]] [hostname] [port[s]]
10

DESCRIPTION

12     The nc (or netcat) utility is used for just about anything under the sun
13     involving TCP or UDP.  It can open TCP connections, send UDP packets,
14     listen on arbitrary TCP and UDP ports, do port scanning, and deal with
15     both IPv4 and IPv6.  Unlike telnet(1), nc scripts nicely, and separates
16     error messages onto standard error instead of sending them to standard
17     output, as telnet(1) does with some.
18
19     Common uses include:
20
21           ·   simple TCP proxies
22           ·   shell-script based HTTP clients and servers
23           ·   network daemon testing
24           ·   a SOCKS or HTTP ProxyCommand for ssh(1)
25           ·   and much, much more
26
27     The options are as follows:
28
29     -4      Forces nc to use IPv4 addresses only.
30
31     -6      Forces nc to use IPv6 addresses only.
32
33     -D      Enable debugging on the socket.
34
35     -d      Do not attempt to read from stdin.
36
37     -h      Prints out nc help.
38
39     -i interval
40             Specifies a delay time interval between lines of text sent and
41             received.  Also causes a delay time between connections to multi‐
42             ple ports.
43
44     -k      Forces nc to stay listening for another connection after its cur‐
45             rent connection is completed.  It is an error to use this option
46             without the -l option.
47
48     -l      Used to specify that nc should listen for an incoming connection
49             rather than initiate a connection to a remote host.  It is an
50             error to use this option in conjunction with the -p, -s, or -z
51             options.  Additionally, any timeouts specified with the -w option
52             are ignored.
53
54     -n      Do not do any DNS or service lookups on any specified addresses,
55             hostnames or ports.
56
57     -p source_port
58             Specifies the source port nc should use, subject to privilege
59             restrictions and availability.  It is an error to use this option
60             in conjunction with the -l option.
61
62     -r      Specifies that source and/or destination ports should be chosen
63             randomly instead of sequentially within a range or in the order
64             that the system assigns them.
65
66     -S      Enables the RFC 2385 TCP MD5 signature option.
67
68     -s source_ip_address
69             Specifies the IP of the interface which is used to send the pack‐
70             ets.  It is an error to use this option in conjunction with the
71             -l option.
72
73     -T ToS  Specifies IP Type of Service (ToS) for the connection.  Valid
74             values are the tokens “lowdelay”, “throughput”, “reliability”, or
75             an 8-bit hexadecimal value preceded by “0x”.
76
77     -C      Send CRLF as line-ending
78
79     -t      Causes nc to send RFC 854 DON'T and WON'T responses to RFC 854 DO
80             and WILL requests.  This makes it possible to use nc to script
81             telnet sessions.
82
83     -U      Specifies to use Unix Domain Sockets.
84
85     -u      Use UDP instead of the default option of TCP.
86
87     -v      Have nc give more verbose output.
88
89     -w timeout
90             If a connection and stdin are idle for more than timeout seconds,
91             then the connection is silently closed.  The -w flag has no
92             effect on the -l option, i.e. nc will listen forever for a con‐
93             nection, with or without the -w flag.  The default is no timeout.
94
95     -X proxy_version
96             Requests that nc should use the specified protocol when talking
97             to the proxy server.  Supported protocols are “4” (SOCKS v.4),
98             “5” (SOCKS v.5) and “connect” (HTTPS proxy).  If the protocol is
99             not specified, SOCKS version 5 is used.
100
101     -x proxy_address[:port]
102             Requests that nc should connect to hostname using a proxy at
103             proxy_address and port.  If port is not specified, the well-known
104             port for the proxy protocol is used (1080 for SOCKS, 3128 for
105             HTTPS).
106
107     -z      Specifies that nc should just scan for listening daemons, without
108             sending any data to them.  It is an error to use this option in
109             conjunction with the -l option.
110
111     hostname can be a numerical IP address or a symbolic hostname (unless the
112     -n option is given).  In general, a hostname must be specified, unless
113     the -l option is given (in which case the local host is used).
114
115     port[s] can be single integers or ranges.  Ranges are in the form nn-mm.
116     In general, a destination port must be specified, unless the -U option is
117     given (in which case a socket must be specified).
118

CLIENT/SERVER MODEL

120     It is quite simple to build a very basic client/server model using nc.
121     On one console, start nc listening on a specific port for a connection.
122     For example:
123
124           $ nc -l 1234
125
126     nc is now listening on port 1234 for a connection.  On a second console
127     (or a second machine), connect to the machine and port being listened on:
128
129           $ nc 127.0.0.1 1234
130
131     There should now be a connection between the ports.  Anything typed at
132     the second console will be concatenated to the first, and vice-versa.
133     After the connection has been set up, nc does not really care which side
134     is being used as a ‘server’ and which side is being used as a ‘client’.
135     The connection may be terminated using an EOF (‘^D’).
136

DATA TRANSFER

138     The example in the previous section can be expanded to build a basic data
139     transfer model.  Any information input into one end of the connection
140     will be output to the other end, and input and output can be easily cap‐
141     tured in order to emulate file transfer.
142
143     Start by using nc to listen on a specific port, with output captured into
144     a file:
145
146           $ nc -l 1234 > filename.out
147
148     Using a second machine, connect to the listening nc process, feeding it
149     the file which is to be transferred:
150
151           $ nc host.example.com 1234 < filename.in
152
153     After the file has been transferred, the connection will close automati‐
154     cally.
155

TALKING TO SERVERS

157     It is sometimes useful to talk to servers “by hand” rather than through a
158     user interface.  It can aid in troubleshooting, when it might be neces‐
159     sary to verify what data a server is sending in response to commands
160     issued by the client.  For example, to retrieve the home page of a web
161     site:
162
163           $ echo -n "GET / HTTP/1.0\r\n\r\n" | nc host.example.com 80
164
165     Note that this also displays the headers sent by the web server.  They
166     can be filtered, using a tool such as sed(1), if necessary.
167
168     More complicated examples can be built up when the user knows the format
169     of requests required by the server.  As another example, an email may be
170     submitted to an SMTP server using:
171
172           $ nc [-C] localhost 25 << EOF
173           HELO host.example.com
174           MAIL FROM: <user@host.example.com>
175           RCPT TO: <user2@host.example.com>
176           DATA
177           Body of email.
178           .
179           QUIT
180           EOF
181

PORT SCANNING

183     It may be useful to know which ports are open and running services on a
184     target machine.  The -z flag can be used to tell nc to report open ports,
185     rather than initiate a connection.  For example:
186
187           $ nc -z host.example.com 20-30
188           Connection to host.example.com 22 port [tcp/ssh] succeeded!
189           Connection to host.example.com 25 port [tcp/smtp] succeeded!
190
191     The port range was specified to limit the search to ports 20 - 30.
192
193     Alternatively, it might be useful to know which server software is run‐
194     ning, and which versions.  This information is often contained within the
195     greeting banners.  In order to retrieve these, it is necessary to first
196     make a connection, and then break the connection when the banner has been
197     retrieved.  This can be accomplished by specifying a small timeout with
198     the -w flag, or perhaps by issuing a "QUIT" command to the server:
199
200           $ echo "QUIT" | nc host.example.com 20-30
201           SSH-1.99-OpenSSH_3.6.1p2
202           Protocol mismatch.
203           220 host.example.com IMS SMTP Receiver Version 0.84 Ready
204

EXAMPLES

206     Open a TCP connection to port 42 of host.example.com, using port 31337 as
207     the source port, with a timeout of 5 seconds:
208
209           $ nc -p 31337 -w 5 host.example.com 42
210
211     Open a UDP connection to port 53 of host.example.com:
212
213           $ nc -u host.example.com 53
214
215     Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as
216     the IP for the local end of the connection:
217
218           $ nc -s 10.1.2.3 host.example.com 42
219
220     Create and listen on a Unix Domain Socket:
221
222           $ nc -lU /var/tmp/dsocket
223
224     Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
225     port 8080.  This example could also be used by ssh(1); see the
226     ProxyCommand directive in ssh_config(5) for more information.
227
228           $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
229

SEE ALSO

231     cat(1), ssh(1)
232

AUTHORS

234     Original implementation by *Hobbit* ⟨hobbit@avian.org⟩.
235     Rewritten with IPv6 support by Eric Jackson <ericj@monkey.org>.
236

CAVEATS

238     UDP port scans will always succeed (i.e. report the port as open), ren‐
239     dering the -uz combination of flags relatively useless.
240
241BSD                             August 22, 2006                            BSD
Impressum