NC(1)
๐ NC(1) โ General Commands Manual ๐
okay bestie, this is the General Commands Manual entry for nc (section 1) and it's literally iconic ๐
โจ
๐ NAME ๐
nc โ arbitrary TCP and UDP connections and listens ๐ฆ
๐ธ SYNOPSIS ๐ธ
nc [-46bCDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
[-m minttl] [-O length] [-P proxy_username] [-p source_port]
[-q seconds] [-s sourceaddr] [-T keyword] [-V rtable] [-W recvlimit]
[-w timeout] [-X proxy_protocol] [-x proxy_address[:port]]
[destination] [port]
โจ DESCRIPTION โจ
okay so the nc (or netcat) utility is used for literally just about anything under the sun involving TCP, UDP, or Unix-domain sockets ๐ญ it can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and handle both IPv4 and IPv6. the way that it scripts nicely? chef's kiss ๐ and unlike telnet(1), it sends its error messages to standard error instead of standard output, which telnet(1) does with some of them. no cap, that's a whole vibe ๐
common uses include:
- simple TCP proxies ๐ง
- shell-script based HTTP clients and servers ๐ป
- network daemon testing ๐ฅ
- a SOCKS or HTTP ProxyCommand for ssh(1) ๐ซถ
- and much, much more ๐
๐ The options are as follows ๐
-4โ use IPv4 addresses only ๐-6โ use IPv6 addresses only ๐-bโ allow broadcast ๐ฃ-Cโ send CRLF as the line-ending ๐. every line feed (LF) character from the input data gets translated into CR+LF before it's written to the socket. line feeds that are already preceded by a carriage return (CR) are left alone, and received data is not affected at all-Dโ enable debugging on the socket ๐-dโ do not attempt to read from stdin ๐ โโ๏ธ-Fโ pass the first connected socket usingsendmsg(2)to stdout and exit ๐ฆ. this is seriously useful alongside-Xwhen you wantncto handle the proxy connection setup and then leave the rest of the connection to another program, like ssh(1) using thessh_config(5)ProxyUseFdpassoption. it can't be used with-U๐ซ-hโ print out thenchelp text and exit ๐-I lengthโ specify the size of the TCP receive buffer ๐-i intervalโ sleep forintervalseconds between lines of text sent and received โณ. this also adds a delay between connections to multiple ports-kโ when a connection is completed, listen for another one ๐. it requires-l. when it's used together with the-uoption, the server socket is not connected, so it can receive UDP datagrams from multiple hosts ๐-lโ listen for an incoming connection instead of initiating one to a remote host ๐. the destination and port to listen on can be given as non-optional arguments, or with the-sand-poptions respectively. it can't be used together with-xor-zโ. also, any timeouts set with-wget ignored-M ttlโ set the TTL / hop limit of outgoing packets ๐ฆ-m minttlโ ask the kernel to drop incoming packets whose TTL / hop limit is underminttl๐ก๏ธ-Nโshutdown(2)the network socket after EOF on the input ๐. some servers need this to finish their work-nโ do not perform domain name resolution ๐ . if a name can't be resolved without DNS, an error will be reported-O lengthโ specify the size of the TCP send buffer ๐ค-P proxy_usernameโ specify a username to present to a proxy server that requires authentication ๐. if no username is specified, authentication will not be attempted. right now, proxy authentication is only supported for HTTP CONNECT proxies-p source_portโ specify the source portncshould use ๐ช, subject to privilege restrictions and availability-q secondsโ after EOF on stdin, wait the specified number of seconds and then quit โฐ. ifsecondsis negative, it waits forever (that's the default ๐). specifying a non-negativesecondsimplies-N-rโ choose source and/or destination ports randomly ๐ฒ, instead of sequentially within a range or in the order the system assigns them-Sโ enable the RFC 2385 TCP MD5 signature option ๐-s sourceaddrโ set the source address to send packets from ๐, which is useful on machines with multiple interfaces. for Unix-domain datagram sockets, it specifies the local temporary socket file to create and use so datagrams can be received. it can't be used together with-x๐ซ-T keywordโ change the IPv4 TOS / IPv6 traffic class value ๐๏ธ.keywordmay be one ofcritical,inetcontrol,lowcost,lowdelay,netcontrol,throughput,reliability, or one of the DiffServ Code Points:ef,af11...af43,cs0...cs7; or a number in either hex or decimal ๐ข-tโ send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests ๐งธ. this is what makes it possible to usencto script telnet sessions-Uโ use Unix-domain sockets ๐งฆ. it can't be used together with-For-x. on Linux, if the name starts with an at symbol (@), it's read as an abstract namespace socket: the leading@gets replaced with a NUL byte before binding or connecting. for the details, check out unix(7) ๐-uโ use UDP instead of TCP ๐ธ. it can't be used together with-x. for Unix-domain sockets, it uses a datagram socket instead of a stream socket. if a Unix-domain socket is used, a temporary receiving socket is created in/tmpunless the-sflag is given-V rtableโ set the routing table to be used ๐บ๏ธ-vโ produce more verbose output ๐ข-W recvlimitโ terminate after receivingrecvlimitpackets from the network ๐-w timeoutโ connections that can't be established, or that sit idle, time out aftertimeoutseconds โ. the-wflag has no effect on the-loption, i.e.ncwill listen forever for a connection, with or without the-wflag ๐. the default is no timeout-X proxy_protocolโ useproxy_protocolwhen talking to the proxy server ๐ฃ๏ธ. supported protocols are4(SOCKS v.4),5(SOCKS v.5), andconnect(HTTPS proxy). if the protocol isn't specified, SOCKS version 5 is used-x proxy_address[:port]โ connect to the destination using a proxy atproxy_addressandport๐ช. ifportisn't specified, the well-known port for the proxy protocol is used (1080 for SOCKS, 3128 for HTTPS). an IPv6 address can be specified unambiguously by enclosingproxy_addressin square brackets. a proxy can't be used with any of the options-lsuU๐ โโ๏ธ-Zโ DCCP mode ๐-zโ only scan for listening daemons, without sending any data to them ๐. it can't be used together with-l
destination can be a numerical IP address or a symbolic hostname (unless the -n option is given) ๐ . in general, a destination must be specified, unless the -l option is given (in which case the local host is used). for Unix-domain sockets, a destination is required and it's the socket path to connect to (or to listen on if the -l option is given) ๐งญ
port can be specified as a numeric port number or as a service name ๐ข. port ranges may be specified as numeric port numbers of the form nn-mm. in general, a destination port must be specified, unless the -U option is given. for some options, the value 0 requests that the system choose a port number ๐ฐ
more to come, bestie! ๐ซถ
โจ TALKING TO SERVERS โจ
okay so sometimes it's lowkey iconic to talk to servers "by hand" instead of clicking through a fancy UI ๐ it's super helpful for troubleshooting, especially when you need to check exactly what data a server is sending back for the commands your client sent. like, want to grab the home page of a website? just do:
$ printf "GET / HTTP/1.0\r\n\r\n" | nc host.example.com 80
heads up, this also shows the headers the web server sends back ๐ if they're in your way, you can filter them with a tool like sed(1) ๐ฆ
once you know what format a server wants, you can build up way more complicated stuff. for example, you can submit an email to an SMTP server like this ๐
$ nc [-C] localhost 25 << EOF
HELO host.example.com
MAIL FROM:<user@host.example.com>
RCPT TO:<user2@host.example.com>
DATA
Body of email.
.
QUIT
EOF
๐ PORT SCANNING ๐
it can be super useful to know which ports are open and which ones have services running on a target machine โจ the -z flag tells nc to just report open ports instead of starting a connection. it's usually a good idea to pair it with -v so you get verbose output on stderr ๐ง
for example:
$ nc -zv host.example.com 20-30
Connection to host.example.com 22 port [tcp/ssh] succeeded!
Connection to host.example.com 25 port [tcp/smtp] succeeded!
the port range limits the search to ports 20 through 30, and they get scanned in increasing order (unless you set the -r flag) ๐ธ
you can also hand it a list of ports to scan, like so:
$ nc -zv host.example.com http 20 22-23
nc: connect to host.example.com 80 (tcp) failed: Connection refused
nc: connect to host.example.com 20 (tcp) failed: Connection refused
Connection to host.example.com port [tcp/ssh] succeeded!
nc: connect to host.example.com 23 (tcp) failed: Connection refused
the ports get scanned in the order you typed them in (unless -r is set) ๐
also, sometimes you wanna know what server software is running and which versions, and that info is often hiding in the greeting banners ๐ to grab those, nc has to make a connection first, then break it once the banner shows up. you can do that with a small timeout using the -w flag, or by sending a "QUIT" command to the server. no cap, both are very slay ๐
$ echo "QUIT" | nc host.example.com 20-30
SSH-1.99-OpenSSH_3.6.1p2
Protocol mismatch.
220 host.example.com IMS SMTP Receiver Version 0.84 Ready
๐ EXAMPLES ๐
okay here are some looks you can copy ๐ซถ
Open a TCP connection to port 42 of host.example.com, using port 31337 as the source port, with a timeout of 5 seconds โฑ๏ธ
$ nc -p 31337 -w 5 host.example.com 42
Open a UDP connection to port 53 of host.example.com ๐
$ nc -u host.example.com 53
Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the IP for the local end of the connection ๐ฆ
$ nc -s 10.1.2.3 host.example.com 42
Create and listen on a Unix-domain stream socket ๐ง
$ nc -lU /var/tmp/dsocket
Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4, port 8080. this one could also be used by ssh(1), so check out the ProxyCommand directive in ssh_config(5) for more info โจ
$ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
same thing again, but this time with proxy authentication enabled, using the username "ruser" in case the proxy asks for it ๐
$ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
๐ SEE ALSO ๐
๐ซถ AUTHORS ๐ซถ
Original implementation by Hobbit <hobbit@avian.org>. Rewritten with IPv6 support by Eric Jackson <ericj@monkey.org>. Modified for Debian port by Aron Xu โจaron@debian.orgโฉ ๐ธ
โ ๏ธ CAVEATS โ ๏ธ
not me being dramatic but UDP port scans using the -uz combo will ALWAYS report success, no matter what state the target machine is in ๐
however, if you pair it with a traffic sniffer on the target machine or some middle device, -uz can actually be useful for communications diagnostics ๐ just keep in mind the amount of UDP traffic generated might be limited by hardware resources and/or configuration settings ๐
bestie, that's the whole page and you're officially a netcat main character now ๐๐ this was a Debian thing, April 1, 2024, NC(1) ๐งโจ stay iconic ๐ญ๐
๐ฆ where this page comes from
- opensuse-leap 16.0: netcat-openbsd 1.229-160000.2.2 ยท BSD-3-Clause
- opensuse-leap 16.0: netcat-openbsd 1.229-160000.2.2 ยท BSD-3-Clause
- opensuse-leap 16.0: netcat-openbsd 1.229-160000.2.2 ยท BSD-3-Clause
- opensuse-leap 16.0: netcat-openbsd 1.229-160000.2.2 ยท BSD-3-Clause
- ubuntu questing: netcat-openbsd 1.229-1build1 ยท BSD-3-Clause
- Copyright (c) 1996 David Sacerdote
- ๐ girlified by anthropic/claude-haiku-5.5 on : a language model's rewrite of the original, under its terms