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 re‐
69 ceived. Also causes a delay time between connections to multiple
70 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 re‐
96 solved 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 re‐
114 strictions 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 se‐
122 quentially within a range or in the order that the system assigns
123 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 in‐
140 coming 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 re‐
155 quests. 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 re‐
164 ceiving 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. Re‐
196 quires -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 -N 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’), as the -N flag was
229 given.
230
232 The example in the previous section can be expanded to build a basic data
233 transfer model. Any information input into one end of the connection
234 will be output to the other end, and input and output can be easily cap‐
235 tured in order to emulate file transfer.
236
237 Start by using nc to listen on a specific port, with output captured into
238 a file:
239
240 $ nc -l 1234 > filename.out
241
242 Using a second machine, connect to the listening nc process, feeding it
243 the file which is to be transferred:
244
245 $ nc -N host.example.com 1234 < filename.in
246
247 After the file has been transferred, the connection will close automati‐
248 cally.
249
251 It is sometimes useful to talk to servers “by hand” rather than through a
252 user interface. It can aid in troubleshooting, when it might be neces‐
253 sary to verify what data a server is sending in response to commands is‐
254 sued by the client. For example, to retrieve the home page of a web
255 site:
256
257 $ printf "GET / HTTP/1.0\r\n\r\n" | nc host.example.com 80
258
259 Note that this also displays the headers sent by the web server. They
260 can be filtered, using a tool such as sed(1), if necessary.
261
262 More complicated examples can be built up when the user knows the format
263 of requests required by the server. As another example, an email may be
264 submitted to an SMTP server using:
265
266 $ nc localhost 25 << EOF
267 HELO host.example.com
268 MAIL FROM:<user@host.example.com>
269 RCPT TO:<user2@host.example.com>
270 DATA
271 Body of email.
272 .
273 QUIT
274 EOF
275
277 It may be useful to know which ports are open and running services on a
278 target machine. The -z flag can be used to tell nc to report open ports,
279 rather than initiate a connection. For example:
280
281 $ nc -z host.example.com 20-30
282 Connection to host.example.com 22 port [tcp/ssh] succeeded!
283 Connection to host.example.com 25 port [tcp/smtp] succeeded!
284
285 The port range was specified to limit the search to ports 20 - 30.
286
287 Alternatively, it might be useful to know which server software is run‐
288 ning, and which versions. This information is often contained within the
289 greeting banners. In order to retrieve these, it is necessary to first
290 make a connection, and then break the connection when the banner has been
291 retrieved. This can be accomplished by specifying a small timeout with
292 the -w flag, or perhaps by issuing a "QUIT" command to the server:
293
294 $ echo "QUIT" | nc host.example.com 20-30
295 SSH-1.99-OpenSSH_3.6.1p2
296 Protocol mismatch.
297 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
298
300 Open a TCP connection to port 42 of host.example.com, using port 31337 as
301 the source port, with a timeout of 5 seconds:
302
303 $ nc -p 31337 -w 5 host.example.com 42
304
305 Open a TCP connection to port 443 of www.example.com, and negotiate TLS
306 with any supported TLS protocol version and "compat" ciphers:
307
308 $ nc -cv -T protocols=all -T ciphers=compat www.example.com 443
309
310 Open a TCP connection to port 443 of www.google.ca, and negotiate TLS.
311 Check for a different name in the certificate for validation:
312
313 $ nc -cv -e adsf.au.doubleclick.net www.google.ca 443
314
315 Open a UDP connection to port 53 of host.example.com:
316
317 $ nc -u host.example.com 53
318
319 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as
320 the IP for the local end of the connection:
321
322 $ nc -s 10.1.2.3 host.example.com 42
323
324 Create and listen on a UNIX-domain stream socket:
325
326 $ nc -lU /var/tmp/dsocket
327
328 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
329 port 8080. This example could also be used by ssh(1); see the
330 ProxyCommand directive in ssh_config(5) for more information.
331
332 $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
333
334 The same example again, this time enabling proxy authentication with
335 username “ruser” if the proxy requires it:
336
337 $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
338
340 cat(1), ssh(1)
341
343 Original implementation by *Hobbit* <hobbit@avian.org>.
344 Rewritten with IPv6 support by
345 Eric Jackson <ericj@monkey.org>.
346
348 UDP port scans using the -uz combination of flags will always report suc‐
349 cess irrespective of the target machine's state. However, in conjunction
350 with a traffic sniffer either on the target machine or an intermediary
351 device, the -uz combination could be useful for communications diagnos‐
352 tics. Note that the amount of UDP traffic generated may be limited ei‐
353 ther due to hardware resources and/or configuration settings.
354
355BSD September 11, 2022 BSD