1.\" Copyright (c) 1996 David Sacerdote All rights reserved. 2.\" Redistribution and use in source and binary forms, with or without 3.\" modification, are permitted provided that the following conditions are 4.\" met: 1. Redistributions of source code must retain the above copyright 5.\" notice, this list of conditions and the following disclaimer. 6.\" 7.\" 2. Redistributions in binary form must reproduce the above copyright 8.\" notice, this list of conditions and the following disclaimer in the 9.\" documentation and/or other materials provided with the distribution. 3. 10.\" The name of the author may not be used to endorse or promote products 11.\" derived from this 12.\" 13.\" software without specific prior written permission THIS SOFTWARE IS 14.\" PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 15.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 16.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 17.\" NO EVENT SHALL THE AUTHOR 18.\" 19.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 22.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23.\" WHETHER IN CONTRACT, 24.\" 25.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27.\" POSSIBILITY OF SUCH DAMAGE. 28.\" 29.\" Portions Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved. 30.\" Copyright 2024 Oxide Computer Company 31.\" Copyright 2025 MNX Cloud, Inc. 32.\" 33.Dd February 10, 2025 34.Dt NC 1 35.Os 36.Sh NAME 37.Nm nc 38.Nd arbitrary TCP and UDP connections and listens 39.Sh SYNOPSIS 40.Nm 41.Fl h 42.Nm 43.Op Fl 46BdnrStuvz 44.Op Fl i Ar interval 45.Op Fl M Ar ttl 46.Op Fl m Ar minttl 47.Op Fl P Ar proxy_username 48.Op Fl p Ar port 49.Op Fl s Ar source_ip_address 50.Op Fl T Ar ToS 51.Op Fl w Ar timeout 52.Op Fl X Ar proxy_protocol 53.Op Fl x Ar proxy_address Ns Op &: Ar port 54.Ar hostname 55.Ar port_list 56.Nm 57.Fl l 58.Op Fl 46DdnrStuvz 59.Op Fl i Ar interval 60.Op Fl M Ar ttl 61.Op Fl m Ar minttl 62.Op Fl T Ar ToS 63.Op Ar hostname 64.Ar port 65.Nm 66.Fl l 67.Op Fl 46DdnrStuvz 68.Op Fl i Ar interval 69.Op Fl M Ar ttl 70.Op Fl m Ar minttl 71.Op Fl T Ar ToS 72.Fl p Ar port 73.Nm 74.Fl U 75.Op Fl Ddtvz 76.Op Fl i Ar interval 77.Op Fl w Ar timeout 78.Fl p Ar path 79.Nm 80.Fl Ul 81.Op Fl 46Ddktv 82.Op Fl i Ar interval 83.Ar path 84.Sh DESCRIPTION 85The 86.Nm 87.Po 88or 89.Nm netcat 90.Pc 91utility is used for a variety of tasks associated with TCP or UDP. 92.Nm 93can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP 94ports, perform port scanning, and deal with both IPv4 and IPv6. 95Unlike 96.Xr telnet 1 , 97.Nm 98scripts nicely, and separates error messages onto standard error instead of 99sending them to standard output. 100.Pp 101The 102.Nm 103command is often used for the following tasks: 104.Bl -bullet -width Ds 105.It 106simple TCP proxies 107.It 108shell-script based HTTP clients and servers 109.It 110network daemon testing 111.It 112a SOCKS or HTTP ProxyCommand for 113.Xr ssh 1 114.El 115.Sh OPTIONS 116The following options are supported: 117.Bl -tag -width Ds 118.It Fl 4 119Force 120.Nm 121to use IPv4 addresses only. 122.It Fl 6 123Force 124.Nm 125to use IPv6 addresses only. 126.It Fl B 127Attempt to bypass IPsec policy and force cleartext. 128Will fail without the 129.Sy sys_ip_config 130privilege, or with the 131.Fl l 132or 133.Fl U 134flags. 135If no IPsec policy exists, this flag is extraneous. 136.It Fl D 137Enable debugging on the socket. 138.It Fl d 139Do not attempt to read from 140.Dv stdin . 141.It Fl h 142Print 143.Nm 144help. 145.It Fl i Ar interval 146Specify a delay time of 147.Ar interval 148between lines of text sent and received. 149This option also causes a delay time between connections to multiple ports. 150.It Fl k 151Force 152.Nm 153to listen for another connection after its current connection is closed. 154.Pp 155It is an error to use this option without the 156.Fl l 157option. 158.It Fl l 159Listen for an incoming connection rather than initiate a connection to a remote 160host. 161.Pp 162It is an error to use this option in conjunction with the 163.Fl s 164or 165.Fl z 166options. 167Additionally, any 168.Ar timeout 169specified with the 170.Fl w 171option is ignored. 172.It Fl M 173Set the default IPv4 TTL or IPv6 hop count that should be used in outgoing 174packets. 175This corresponds to the 176.Dv IP_TTL 177and 178.Dv IPV6_UNICAST_HOPS 179socket options. 180.It Fl m 181Set the minimum IPv4 TTL or IPv6 hop count that must be present on a packet for 182a packet to be received. 183This corresponds to the 184.Dv IP_MINTTL 185and 186.Dv IPV6_MINHOPCOUNT 187socket options. 188.It Fl n 189Do not do any naming or service lookups on any addresses, hostnames, or ports. 190.Pp 191Use of this option means that 192.Ar hostname 193and 194.Ar port 195arguments are restricted to numeric values. 196.Pp 197If used with 198.Fl v 199option all addresses and ports are printed in numeric form, in addition to the 200restriction imposed on the arguments. 201This option does not have any effect when used in conjunction with the 202.Fl U 203option. 204.It Fl P Ar proxy_username 205Specify a username 206.Po 207.Ar proxy_username 208.Pc 209to present to a proxy server that requires authentication. 210If 211.Ar proxy_username 212is not specified, authentication is not attempted. 213Proxy authentication is only supported for HTTP CONNECT proxies at present. 214.Pp 215It is an error to use this option in conjunction with the 216.Fl l 217option. 218.It Fl p Ar port 219When used without 220.Fl l 221option, specify the source port 222.Nm 223should use, subject to privilege restrictions and availability. 224When used with the 225.Fl l 226option, set the listen port. 227.Pp 228This option can be used with 229.Fl l 230option only provided global port argument is not specified. 231.It Fl r 232Choose source or destination ports randomly instead of sequentially within a 233range or in the order that the system assigns them. 234.Pp 235It is an error to use this option in conjunction with the 236.Fl l 237option. 238.It Fl S 239Enables the 240.%T RFC 2385 241TCP MD5 signature option. 242.Pp 243In order for packets to be sent or received in conjunction with this option, a 244security association that matches the traffic must also be created using 245.Xr tcpkey 8 . 246.It Fl s Ar source_ip_address 247Specify the IP of the interface which is used to send the packets. 248.Pp 249It is an error to use this option in conjunction with the 250.Fl l 251option. 252.It Fl T Ar ToS 253Specify IP Type of Service 254.Pq ToS 255or IPv6 traffic class for the connection. 256Valid values are the tokens: 257.Cm lowdelay , 258.Cm throughput , 259.Cm reliability , 260or any decimal or hexadecimal integer between 0 and 255. 261.It Fl t 262Cause 263.Nm 264to send 265.%T RFC 854 266.Dq DON'T 267and 268.Dq WON'T 269responses to 270.%T RFC 854 271.Dq DO 272and 273.Dq WILL 274requests. 275This makes it possible to use 276.Nm 277to script telnet sessions. 278.It Fl U 279Specify the use of Unix Domain Sockets. 280If you specify this option without 281.Fl l , 282it becomes an 283.Dv AF_UNIX 284client. 285If you specify this option with the 286.Fl l 287option, a 288.Dv AF_UNIX 289server is created. 290.Pp 291Use of this option requires that a single argument of a valid Unix domain path 292has to be provided to 293.Nm , 294not a host name or port. 295.It Fl u 296Use UDP instead of the default option of TCP. 297.It Fl v 298Specify verbose output. 299.It Fl w Ar timeout 300Silently close the connection if a connection and 301.Dv stdin 302are idle for more than 303.Ar timeout 304seconds. 305.Pp 306This option has no effect on the 307.Fl l 308option, that is, 309.Nm 310listens forever for a connection, with or without the 311.Fl w 312flag. 313The default is no timeout. 314.It Fl X Ar proxy_protocol 315Use the specified protocol when talking to the proxy server. 316Supported protocols are 4 317.Pq SOCKS v.4 , 3185 319.Pq SOCKS v.5 320and connect 321.Pq HTTP proxy . 322If the protocol is not specified, SOCKS v.5 is used. 323.Pp 324It is an error to use this option in conjunction with the 325.Fl l 326option. 327.It Fl x Ar proxy_address Ns Op &: Ar port 328Request connection to 329.Ar hostname 330using a proxy at 331.Ar proxy_address 332and 333.Ar port . 334If 335.Ar port 336is not specified, the well-known port for the proxy protocol is used 337.Pq 1080 for SOCKS, 3128 for HTTP . 338.Pp 339It is an error to use this option in conjunction with the 340.Fl l 341option. 342.It Fl z 343Scan for listening daemons, without sending any data to them. 344.Pp 345It is an error to use this option in conjunction with the 346.Fl l 347option. 348.El 349.Sh OPERANDS 350The following operands are supported: 351.Bl -tag -width Ds 352.It Ar hostname 353Specify host name. 354.Pp 355.Ar hostname 356can be a numerical IP address or a symbolic hostname 357.Po 358unless the 359.Fl n 360option is specified 361.Pc . 362.Pp 363In general, 364.Ar hostname 365must be specified, unless the 366.Fl l 367option is given or 368.Fl U 369is used 370.Pq in which case the argument is a path . 371If 372.Ar hostname 373argument is specified with 374.Fl l 375option then 376.Ar port 377argument must be given as well and 378.Nm 379tries to bind to that address and port. 380If 381.Ar hostname 382argument is not specified with 383.Fl l 384option then 385.Nm 386tries to listen on a wildcard socket for given 387.Ar port . 388.It Ar path 389Specify pathname. 390.It Ar port | port_list 391Specify port. 392.Pp 393.Ar port_list 394can be specified as single integers, ranges or combinations of both. 395Specify ranges in the form of nn-mm. 396The 397.Ar port_list 398must have at least one member, but can have multiple ports/ranges separated by 399commas. 400.Pp 401In general, a destination port must be specified, unless the 402.Fl U 403option is given, in which case a Unix Domain Socket path must be specified 404instead of 405.Ar hostname . 406.El 407.Sh USAGE 408.Ss Client/Server Model 409It is quite simple to build a very basic client/server model using 410.Nm . 411On one console, start 412.Nm 413listening on a specific port for a connection. 414For example, the command: 415.Pp 416.Dl $ nc -l 1234 417.Pp 418listens on port 1234 for a connection. 419On a second console 420.Pq or a second machine , 421connect to the machine and port to which 422.Nm 423is listening: 424.Pp 425.Dl $ nc 127.0.0.1 1234 426.Pp 427There should now be a connection between the ports. 428Anything typed at the second console is concatenated to the first, and 429vice-versa. 430After the connection has been set up, 431.Nm 432does not really care which side is being used as a server and which side is 433being used as a client. 434The connection can be terminated using an EOF 435.Pq Ctrl/d . 436.Ss Data Transfer 437The example in the previous section can be expanded to build a basic data 438transfer model. 439Any information input into one end of the connection is output to the other 440end, and input and output can be easily captured in order to emulate file 441transfer. 442.Pp 443Start by using 444.Nm 445to listen on a specific port, with output captured into a file: 446.Pp 447.Dl $ nc -l 1234 > filename.out 448.Pp 449Using a second machine, connect to the listening 450.Nm 451process, feeding it the file which is to be transferred: 452.Pp 453.Dl $ nc host.example.com 1234 < filename.in 454.Pp 455After the file has been transferred, the connection closes automatically. 456.Ss Talking to Servers 457It is sometimes useful to talk to servers by hand rather than through a user 458interface. 459It can aid in troubleshooting, when it might be necessary to verify what data a 460server is sending in response to commands issued by the client. 461.Pp 462For example, to retrieve the home page of a web site: 463.Pp 464.Dl $ echo -n \&"GET / HTTP/1.0\er\en\er\en\&" | nc host.example.com 80 465.Pp 466This also displays the headers sent by the web server. 467They can be filtered, if necessary, by using a tool such as 468.Xr sed 1 . 469.Pp 470More complicated examples can be built up when the user knows the format of 471requests required by the server. 472As another example, an email can be submitted to an SMTP server using: 473.Bd -literal -offset indent 474$ nc localhost 25 << EOF 475HELO host.example.com 476MAIL FROM: <user@host.example.com> 477RCPT TO: <user2@host.example.com> 478DATA 479Body of email. 480\&. 481QUIT 482EOF 483.Ed 484.Ss Port Scanning 485It can be useful to know which ports are open and running services on a target 486machine. 487The 488.Fl z 489flag can be used to tell 490.Nm 491to report open ports, rather than to initiate a connection. 492.Pp 493In this example: 494.Bd -literal -offset indent 495$ nc -z host.example.com 20-30 496Connection to host.example.com 22 port [tcp/ssh] succeeded! 497Connection to host.example.com 25 port [tcp/smtp] succeeded! 498.Ed 499.Pp 500The port range was specified to limit the search to ports 20 - 30. 501.Pp 502Alternatively, it might be useful to know which server software is running, and 503which versions. 504This information is often contained within the greeting banners. 505In order to retrieve these, it is necessary to first make a connection, and 506then break the connection when the banner has been retrieved. 507This can be accomplished by specifying a small timeout with the 508.Fl w 509flag, or perhaps by issuing a QUIT command to the server: 510.Bd -literal -offset indent 511$ echo "QUIT" | nc host.example.com 20-30 512SSH-2.0-Sun_SSH_1.1 513Protocol mismatch. 514220 host.example.com IMS SMTP Receiver Version 0.84 Ready 515.Ed 516.Ss inetd Capabilities 517One of the possible uses is to create simple services by using 518.Xr inetd 8 . 519.Pp 520The following example creates a redirect from TCP port 8080 to port 80 on host 521realwww: 522.Bd -literal -offset indent 523# cat << EOF >> /etc/services 524wwwredir 8080/tcp # WWW redirect EOF 525# cat << EOF > /tmp/wwwredir.conf 526wwwredir stream tcp nowait nobody /usr/bin/nc /usr/bin/nc -w 3 realwww 80 527EOF 528# inetconv -i /tmp/wwwredir.conf 529wwwredir -> /var/svc/manifest/network/wwwredir-tcp.xml 530Importing wwwredir-tcp.xml ...Done 531# inetadm -l wwwredir/tcp 532SCOPE NAME=VALUE 533name="wwwredir" 534endpoint_type="stream" 535proto="tcp" 536isrpc=FALSE 537wait=FALSE 538exec="/usr/bin/nc -w 3 realwww 80" 539arg0="/usr/bin/nc" 540user="nobody" 541default bind_addr="" 542default bind_fail_max=-1 543default bind_fail_interval=-1 544default max_con_rate=-1 545default max_copies=-1 546default con_rate_offline=-1 547default failrate_cnt=40 548default failrate_interval=60 549default inherit_env=TRUE 550default tcp_trace=TRUE 551default tcp_wrappers=FALSE 552.Ed 553.Ss Privileges 554To bind to a privileged port number 555.Nm 556needs to be granted the 557.Sy net_privaddr 558privilege. 559Bypassing IPsec policy requires the 560.Sy sys_ip_config 561privilege. 562If Trusted Extensions are configured and the port 563.Nm 564should listen on is configured as a multi-level port 565.Nm 566also needs the 567.Sy net_bindmlp 568privilege. 569.Pp 570Privileges can be assigned to the user or role directly, by specifying them in 571the account's default privilege set in 572.Xr user_attr 5 . 573However, this means that any application that this user or role starts have 574these additional privileges. 575To only grant the 576.Xr privileges 7 577when 578.Nm 579is invoked, the recommended approach is to create and assign an 580.Xr rbac 7 581rights profile. 582See 583.Sx EXAMPLES 584for additional information. 585.Sh EXAMPLES 586Open a TCP connection to port 42 of host.example.com, using port 3141 as the 587source port, with a timeout of 5 seconds: 588.Pp 589.Dl $ nc -p 3141 -w 5 host.example.com 42 590.Pp 591Open a UDP connection to port 53 of host.example.com: 592.Pp 593.Dl $ nc -u host.example.com 53 594.Pp 595Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the IP 596for the local end of the connection: 597.Pp 598.Dl $ nc -s 10.1.2.3 host.example.com 42 599.Pp 600Use a list of ports and port ranges for a port scan on various ports: 601.Pp 602.Dl $ nc -z host.example.com 21-25,53,80,110-120,443 603.Pp 604Create and listen on a Unix Domain Socket: 605.Pp 606.Dl $ nc -lU /var/tmp/dsocket 607.Pp 608Create and listen on a UDP socket with associated port 8888: 609.Pp 610.Dl $ nc -u -l -p 8888 611.Pp 612which is the same as: 613.Pp 614.Dl $ nc -u -l 8888 615.Pp 616Create and listen on a TCP socket with associated port 2222 and bind to address 617127.0.0.1 only: 618.Pp 619.Dl $ nc -l 127.0.0.1 2222 620.Pp 621Connect to port 42 of host.example.com using an HTTP proxy at 10.2.3.4, port 6228080. 623This example could also be used by 624.Xr ssh 1 . 625See the 626.Cm ProxyCommand 627directive in 628.Xr ssh_config 5 629for more information. 630.Pp 631.Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42 632.Pp 633The same example again, this time enabling proxy authentication with username 634ruser if the proxy requires it: 635.Pp 636.Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42 637.Pp 638To run 639.Nm 640with the smallest possible set of privileges as a user or role that has 641additional privileges 642.Pq such as the default root account 643it can be invoked using 644.Xr ppriv 1 645as well. 646For example, limiting it to only run with the privilege to bind to a privileged 647port: 648.Bd -literal -offset indent 649$ ppriv -e -sA=basic,!file_link_any,!proc_exec,!proc_fork,\e 650 !proc_info,!proc_session,net_privaddr nc -l 42 651.Ed 652.Pp 653To allow a user or role to use only 654.Nm 655with the 656.Sy net_privaddr 657privilege, a rights profile needs to be created: 658.Pp 659.Pa /etc/security/exec_attr: 660.Dl Netcat privileged:solaris:cmd:::/usr/bin/nc:privs=net_privaddr 661.Pa /etc/security/prof_attr 662.Dl Netcat privileged:::Allow nc to bind to privileged ports:help=None.html 663.Pp 664Assigning this rights profile using 665.Xr user_attr 5 666permits the user or role to run 667.Nm 668allowing it to listen on any port. 669To permit a user or role to use 670.Nm 671only to listen on specific ports a wrapper script should be specified in the 672rights profiles: 673.Pp 674.Pa /etc/security/exec_attr 675.Dl Netcat restricted:solaris:cmd:::/usr/bin/nc-restricted:privs=net_privaddr 676.Pa /etc/security/prof_attr 677.Dl Netcat restricted:::Allow nc to bind to privileged ports:help=None.html 678.Pp 679and write a shell script that restricts the permissible options, for example, 680one that permits one to bind only on ports between 42 and 64 non-inclusive: 681.Bd -literal -offset indent 682#!/bin/ksh 683 684(( $# == 1 )) || exit 1 685(( $1 > 42 && $1 < 64 )) || exit 1 686exec /usr/bin/nc -l -p "$1" 687.Ed 688.Pp 689This grants the extra privileges when the user or role invokes 690.Nm 691using the wrapper script from a profile shell. 692See 693.Xr pfsh 1 , 694.Xr pfksh 1 , 695.Xr pfcsh 1 , 696and 697.Xr pfexec 1 . 698.Pp 699Invoking 700.Nm 701directly does not run it with the additional privileges, and neither does 702invoking the script without using 703.Sy pfexec 704or a profile shell. 705.Sh INTERFACE STABILITY 706The command line syntax is 707.Sy Committed 708for the 709.Fl 4 , 710.Fl 6 , 711.Fl l , 712.Fl n , 713.Fl p , 714.Fl u , 715and 716.Fl w 717options and their arguments 718.Pq if any . 719The 720.Ar name 721and 722.Ar port 723list arguments are 724.Sy Committed . 725The port range syntax is 726.Sy Uncommitted . 727The interface stability level for all other command line options and their 728arguments is 729.Sy Uncommitted. 730.Sh SEE ALSO 731.Xr cat 1 , 732.Xr pfcsh 1 , 733.Xr pfexec 1 , 734.Xr pfksh 1 , 735.Xr pfsh 1 , 736.Xr ppriv 1 , 737.Xr sed 1 , 738.Xr ssh 1 , 739.Xr telnet 1 , 740.Xr ssh_config 5 , 741.Xr user_attr 5 , 742.Xr attributes 7 , 743.Xr privileges 7 , 744.Xr rbac 7 , 745.Xr inetadm 8 , 746.Xr inetconv 8 , 747.Xr inetd 8 , 748.Xr tcpkey 8 749.Sh AUTHORS 750The original implementation of 751.Nm 752was written by 753.An Hobbit Aq Mt hobbit@avian.org 754.Pp 755.Nm 756was rewritten with IPv6 support by 757.An -nosplit 758.An Eric Jackson Aq Mt ericj@monkey.org 759.Sh NOTES 760UDP port scans always succeed, that is, report the port as open, rendering the 761.Fl uz 762combination of flags relatively useless. 763