1NC(1) BSD General Commands Manual NC(1)
2
4 nc — arbitrary TCP and UDP connections and listens
5
7 nc [-46cDdFhklNnrStUuvz] [-C certfile] [-e name] [-H hash] [-I length]
8 [-i interval] [-K keyfile] [-M ttl] [-m minttl] [-O length]
9 [-o staplefile] [-P proxy_username] [-p source_port] [-R CAfile]
10 [-s sourceaddr] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout]
11 [-X proxy_protocol] [-x proxy_address[:port]] [-Z peercertfile]
12 [destination] [port]
13
15 The nc (or netcat) utility is used for just about anything under the sun
16 involving TCP, UDP, or UNIX-domain sockets. It can open TCP connections,
17 send UDP packets, listen on arbitrary TCP and UDP ports, do port scan‐
18 ning, and deal with both IPv4 and IPv6. Unlike telnet(1), nc scripts
19 nicely, and separates error messages onto standard error instead of send‐
20 ing them to standard output, as telnet(1) does with some.
21
22 Common uses include:
23
24 · simple TCP proxies
25 · shell-script based HTTP clients and servers
26 · network daemon testing
27 · a SOCKS or HTTP ProxyCommand for ssh(1)
28 · and much, much more
29
30 The options are as follows:
31
32 -4 Use IPv4 addresses only.
33
34 -6 Use IPv6 addresses only.
35
36 -C certfile
37 Load the public key part of the TLS peer certificate from
38 certfile, in PEM format. Requires -c.
39
40 -c Use TLS to connect or listen. Cannot be used together with any
41 of the options -FuU.
42
43 -D Enable debugging on the socket.
44
45 -d Do not attempt to read from stdin.
46
47 -e name
48 Only accept the TLS peer certificate if it contains the name.
49 Requires -c. If not specified, destination is used.
50
51 -F Pass the first connected socket using sendmsg(2) to stdout and
52 exit. This is useful in conjunction with -X to have nc perform
53 connection setup with a proxy but then leave the rest of the con‐
54 nection to another program (e.g. ssh(1) using the ssh_config(5)
55 ProxyUseFdpass option). Cannot be used with -c or -U.
56
57 -H hash
58 Only accept the TLS peer certificate if its hash returned from
59 tls_peer_cert_hash(3) matches hash. Requires -c and cannot be
60 used with -T noverify.
61
62 -h Print out the nc help text and exit.
63
64 -I length
65 Specify the size of the TCP receive buffer.
66
67 -i interval
68 Sleep for interval seconds between lines of text sent and
69 received. Also causes a delay time between connections to multi‐
70 ple ports.
71
72 -K keyfile
73 Load the TLS private key from keyfile, in PEM format. Requires
74 -c.
75
76 -k When a connection is completed, listen for another one. Requires
77 -l. When used together with the -u option, the server socket is
78 not connected and it can receive UDP datagrams from multiple
79 hosts.
80
81 -l Listen for an incoming connection rather than initiating a con‐
82 nection to a remote host. Cannot be used together with any of
83 the options -psxz. Additionally, any timeouts specified with the
84 -w option are ignored.
85
86 -M ttl Set the TTL / hop limit of outgoing packets.
87
88 -m minttl
89 Ask the kernel to drop incoming packets whose TTL / hop limit is
90 under minttl.
91
92 -N [22mshutdown(2) the network socket after EOF on the input. Some
93 servers require this to finish their work.
94
95 -n Do not perform domain name resolution. If a name cannot be
96 resolved without DNS, an error will be reported.
97
98 -O length
99 Specify the size of the TCP send buffer.
100
101 -o staplefile
102 During the TLS handshake, load data to be stapled from
103 staplefile, which is expected to contain an OCSP response from an
104 OCSP server in DER format. Requires -c and -C.
105
106 -P proxy_username
107 Specifies a username to present to a proxy server that requires
108 authentication. If no username is specified then authentication
109 will not be attempted. Proxy authentication is only supported
110 for HTTP CONNECT proxies at present.
111
112 -p source_port
113 Specify the source port nc should use, subject to privilege
114 restrictions and availability.
115
116 -R CAfile
117 Load the root CA bundle for TLS certificate verification from
118 CAfile, in PEM format, instead of /etc/ssl/cert.pem. Requires
119 -c.
120
121 -r Choose source and/or destination ports randomly instead of
122 sequentially within a range or in the order that the system
123 assigns them.
124
125 -S Enable the RFC 2385 TCP MD5 signature option.
126
127 -s sourceaddr
128 Set the source address to send packets from, which is useful on
129 machines with multiple interfaces. For UNIX-domain datagram
130 sockets, specifies the local temporary socket file to create and
131 use so that datagrams can be received. Cannot be used together
132 with -l or -x.
133
134 -T keyword
135 Change the IPv4 TOS/IPv6 traffic class value or the TLS options.
136
137 For TLS options, keyword may be one of: noverify, which disables
138 certificate verification; noname, which disables certificate name
139 checking; clientcert, which requires a client certificate on
140 incoming connections; or muststaple, which requires the peer to
141 provide a valid stapled OCSP response with the handshake. The
142 following TLS options specify a value in the form of a key=value
143 pair: ciphers, which allows the supported TLS ciphers to be spec‐
144 ified (see tls_config_set_ciphers(3) for further details);
145 protocols, which allows the supported TLS protocols to be speci‐
146 fied (see tls_config_parse_protocols(3) for further details).
147 Specifying TLS options requires -c.
148
149 For the IPv4 TOS/IPv6 traffic class value, keyword may be one of
150 critical, inetcontrol, lowcost, lowdelay, netcontrol, throughput,
151 reliability, or one of the DiffServ Code Points: ef, af11 ...
152 af43, cs0 ... cs7; or a number in either hex or decimal.
153
154 -t Send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL
155 requests. This makes it possible to use nc to script telnet ses‐
156 sions.
157
158 -U Use UNIX-domain sockets. Cannot be used together with any of the
159 options -cFx.
160
161 -u Use UDP instead of TCP. Cannot be used together with -c or -x.
162 For UNIX-domain sockets, use a datagram socket instead of a
163 stream socket. If a UNIX-domain socket is used, a temporary
164 receiving socket is created in /tmp unless the -s flag is given.
165
166 -V rtable
167 Set the routing table to be used.
168
169 -v Produce more verbose output.
170
171 -W recvlimit
172 Terminate after receiving recvlimit packets from the network.
173
174 -w timeout
175 Connections which cannot be established or are idle timeout after
176 timeout seconds. The -w flag has no effect on the -l option,
177 i.e. nc will listen forever for a connection, with or without the
178 -w flag. The default is no timeout.
179
180 -X proxy_protocol
181 Use proxy_protocol when talking to the proxy server. Supported
182 protocols are 4 (SOCKS v.4), 5 (SOCKS v.5) and connect (HTTPS
183 proxy). If the protocol is not specified, SOCKS version 5 is
184 used.
185
186 -x proxy_address[:port]
187 Connect to destination using a proxy at proxy_address and port.
188 If port is not specified, the well-known port for the proxy pro‐
189 tocol is used (1080 for SOCKS, 3128 for HTTPS). An IPv6 address
190 can be specified unambiguously by enclosing proxy_address in
191 square brackets. A proxy cannot be used with any of the options
192 -lsuU.
193
194 -Z peercertfile
195 Save the peer certificates to peercertfile, in PEM format.
196 Requires -c.
197
198 -z Only scan for listening daemons, without sending any data to
199 them. Cannot be used together with -l.
200
201 destination can be a numerical IP address or a symbolic hostname (unless
202 the -n option is given). In general, a destination must be specified,
203 unless the -l option is given (in which case the local host is used).
204 For UNIX-domain sockets, a destination is required and is the socket path
205 to connect to (or listen on if the -l option is given).
206
207 port can be specified as a numeric port number or as a service name.
208 Port ranges may be specified as numeric port numbers of the form nn-mm.
209 In general, a destination port must be specified, unless the -U option is
210 given.
211
213 It is quite simple to build a very basic client/server model using nc.
214 On one console, start nc listening on a specific port for a connection.
215 For example:
216
217 $ nc -l 1234
218
219 nc is now listening on port 1234 for a connection. On a second console
220 (or a second machine), connect to the machine and port being listened on:
221
222 $ nc 127.0.0.1 1234
223
224 There should now be a connection between the ports. Anything typed at
225 the second console will be concatenated to the first, and vice-versa.
226 After the connection has been set up, nc does not really care which side
227 is being used as a ‘server’ and which side is being used as a ‘client’.
228 The connection may be terminated using an EOF (‘^D’).
229
231 The example in the previous section can be expanded to build a basic data
232 transfer model. Any information input into one end of the connection
233 will be output to the other end, and input and output can be easily cap‐
234 tured in order to emulate file transfer.
235
236 Start by using nc to listen on a specific port, with output captured into
237 a file:
238
239 $ nc -l 1234 > filename.out
240
241 Using a second machine, connect to the listening nc process, feeding it
242 the file which is to be transferred:
243
244 $ nc -N host.example.com 1234 < filename.in
245
246 After the file has been transferred, the connection will close automati‐
247 cally.
248
250 It is sometimes useful to talk to servers “by hand” rather than through a
251 user interface. It can aid in troubleshooting, when it might be neces‐
252 sary to verify what data a server is sending in response to commands
253 issued by the client. For example, to retrieve the home page of a web
254 site:
255
256 $ printf "GET / HTTP/1.0\r\n\r\n" | nc host.example.com 80
257
258 Note that this also displays the headers sent by the web server. They
259 can be filtered, using a tool such as sed(1), if necessary.
260
261 More complicated examples can be built up when the user knows the format
262 of requests required by the server. As another example, an email may be
263 submitted to an SMTP server using:
264
265 $ nc localhost 25 << EOF
266 HELO host.example.com
267 MAIL FROM:<user@host.example.com>
268 RCPT TO:<user2@host.example.com>
269 DATA
270 Body of email.
271 .
272 QUIT
273 EOF
274
276 It may be useful to know which ports are open and running services on a
277 target machine. The -z flag can be used to tell nc to report open ports,
278 rather than initiate a connection. For example:
279
280 $ nc -z host.example.com 20-30
281 Connection to host.example.com 22 port [tcp/ssh] succeeded!
282 Connection to host.example.com 25 port [tcp/smtp] succeeded!
283
284 The port range was specified to limit the search to ports 20 - 30.
285
286 Alternatively, it might be useful to know which server software is run‐
287 ning, and which versions. This information is often contained within the
288 greeting banners. In order to retrieve these, it is necessary to first
289 make a connection, and then break the connection when the banner has been
290 retrieved. This can be accomplished by specifying a small timeout with
291 the -w flag, or perhaps by issuing a "QUIT" command to the server:
292
293 $ echo "QUIT" | nc host.example.com 20-30
294 SSH-1.99-OpenSSH_3.6.1p2
295 Protocol mismatch.
296 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
297
299 Open a TCP connection to port 42 of host.example.com, using port 31337 as
300 the source port, with a timeout of 5 seconds:
301
302 $ nc -p 31337 -w 5 host.example.com 42
303
304 Open a TCP connection to port 443 of www.example.com, and negotiate TLS
305 with any supported TLS protocol version and "compat" ciphers:
306
307 $ nc -cv -T protocols=all -T ciphers=compat www.example.com 443
308
309 Open a TCP connection to port 443 of www.google.ca, and negotiate TLS.
310 Check for a different name in the certificate for validation:
311
312 $ nc -cv -e adsf.au.doubleclick.net www.google.ca 443
313
314 Open a UDP connection to port 53 of host.example.com:
315
316 $ nc -u host.example.com 53
317
318 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as
319 the IP for the local end of the connection:
320
321 $ nc -s 10.1.2.3 host.example.com 42
322
323 Create and listen on a UNIX-domain stream socket:
324
325 $ nc -lU /var/tmp/dsocket
326
327 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
328 port 8080. This example could also be used by ssh(1); see the
329 ProxyCommand directive in ssh_config(5) for more information.
330
331 $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
332
333 The same example again, this time enabling proxy authentication with
334 username “ruser” if the proxy requires it:
335
336 $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
337
339 cat(1), ssh(1)
340
342 Original implementation by *Hobbit* <hobbit@avian.org>.
343 Rewritten with IPv6 support by
344 Eric Jackson <ericj@monkey.org>.
345
347 UDP port scans using the -uz combination of flags will always report suc‐
348 cess irrespective of the target machine's state. However, in conjunction
349 with a traffic sniffer either on the target machine or an intermediary
350 device, the -uz combination could be useful for communications diagnos‐
351 tics. Note that the amount of UDP traffic generated may be limited
352 either due to hardware resources and/or configuration settings.
353
354BSD February 12, 2020 BSD