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

NAME

4     nc — arbitrary TCP and UDP connections and listens
5

SYNOPSIS

7     nc [-46CDdhklnrStUuvz] [-I length] [-i interval] [-O length]
8        [-P proxy_username] [-p source_port] [-s source] [-T ToS] [-V rtable]
9        [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]]
10        [destination] [port]
11

DESCRIPTION

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

CLIENT/SERVER MODEL

143     It is quite simple to build a very basic client/server model using nc.
144     On one console, start nc listening on a specific port for a connection.
145     For example:
146
147           $ nc -l 1234
148
149     nc is now listening on port 1234 for a connection.  On a second console
150     (or a second machine), connect to the machine and port being listened on:
151
152           $ nc 127.0.0.1 1234
153
154     There should now be a connection between the ports.  Anything typed at
155     the second console will be concatenated to the first, and vice-versa.
156     After the connection has been set up, nc does not really care which side
157     is being used as a ‘server’ and which side is being used as a ‘client’.
158     The connection may be terminated using an EOF (‘^D’).
159

DATA TRANSFER

161     The example in the previous section can be expanded to build a basic data
162     transfer model.  Any information input into one end of the connection
163     will be output to the other end, and input and output can be easily cap‐
164     tured in order to emulate file transfer.
165
166     Start by using nc to listen on a specific port, with output captured into
167     a file:
168
169           $ nc -l 1234 > filename.out
170
171     Using a second machine, connect to the listening nc process, feeding it
172     the file which is to be transferred:
173
174           $ nc host.example.com 1234 < filename.in
175
176     After the file has been transferred, the connection will close automati‐
177     cally.
178

TALKING TO SERVERS

180     It is sometimes useful to talk to servers “by hand” rather than through a
181     user interface.  It can aid in troubleshooting, when it might be neces‐
182     sary to verify what data a server is sending in response to commands
183     issued by the client.  For example, to retrieve the home page of a web
184     site:
185
186           $ printf "GET / HTTP/1.0\r\n\r\n" | nc host.example.com 80
187
188     Note that this also displays the headers sent by the web server.  They
189     can be filtered, using a tool such as sed(1), if necessary.
190
191     More complicated examples can be built up when the user knows the format
192     of requests required by the server.  As another example, an email may be
193     submitted to an SMTP server using:
194
195           $ nc localhost 25 << EOF
196           HELO host.example.com
197           MAIL FROM:<user@host.example.com>
198           RCPT TO:<user2@host.example.com>
199           DATA
200           Body of email.
201           .
202           QUIT
203           EOF
204

PORT SCANNING

206     It may be useful to know which ports are open and running services on a
207     target machine.  The -z flag can be used to tell nc to report open ports,
208     rather than initiate a connection.  For example:
209
210           $ nc -z host.example.com 20-30
211           Connection to host.example.com 22 port [tcp/ssh] succeeded!
212           Connection to host.example.com 25 port [tcp/smtp] succeeded!
213
214     The port range was specified to limit the search to ports 20 - 30.
215
216     Alternatively, it might be useful to know which server software is run‐
217     ning, and which versions.  This information is often contained within the
218     greeting banners.  In order to retrieve these, it is necessary to first
219     make a connection, and then break the connection when the banner has been
220     retrieved.  This can be accomplished by specifying a small timeout with
221     the -w flag, or perhaps by issuing a "QUIT" command to the server:
222
223           $ echo "QUIT" | nc host.example.com 20-30
224           SSH-1.99-OpenSSH_3.6.1p2
225           Protocol mismatch.
226           220 host.example.com IMS SMTP Receiver Version 0.84 Ready
227

EXAMPLES

229     Open a TCP connection to port 42 of host.example.com, using port 31337 as
230     the source port, with a timeout of 5 seconds:
231
232           $ nc -p 31337 -w 5 host.example.com 42
233
234     Open a UDP connection to port 53 of host.example.com:
235
236           $ nc -u host.example.com 53
237
238     Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as
239     the IP for the local end of the connection:
240
241           $ nc -s 10.1.2.3 host.example.com 42
242
243     Create and listen on a UNIX-domain stream socket:
244
245           $ nc -lU /var/tmp/dsocket
246
247     Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
248     port 8080.  This example could also be used by ssh(1); see the
249     ProxyCommand directive in ssh_config(5) for more information.
250
251           $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
252
253     The same example again, this time enabling proxy authentication with
254     username “ruser” if the proxy requires it:
255
256           $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
257

SEE ALSO

259     cat(1), ssh(1)
260

AUTHORS

262     Original implementation by *Hobbit* ⟨hobbit@avian.org⟩.
263     Rewritten with IPv6 support by Eric Jackson <ericj@monkey.org>.
264

CAVEATS

266     UDP port scans will always succeed (i.e. report the port as open), ren‐
267     dering the -uz combination of flags relatively useless.
268
269BSD                              June 22, 2019                             BSD
Impressum