Home  >  Article  >  Operation and Maintenance  >  How to use Linux nc command

How to use Linux nc command

PHPz
PHPzforward
2023-05-17 17:13:293205browse

1. Command introduction

Netcat, short for nc, is a powerful network tool known as the Swiss Army Knife of the network industry. Actually, the command in Linux system is ncat. The nc command is just a soft link to ncat. The main functions of the nc command are as follows:

Realize the listening of any TCP/UDP port. nc can be used as a server to listen to the scan of the specified port in TCP or UDP mode. nc can be used as a client to initiate a TCP or UDP connection to the machine. Transferring files between machines Network speed test

nc If you cannot find the nc command, you can use yum install -y nc to install it. The centos6 system installs nc-1.84-24.el6.x86_64, and centos7 installs it. It's nmap-ncat-6.40-19.el7.x86_64.

2. Usage examples

1. Verify whether the server port is accessible

As follows, verify that port 80 of the 172.16.7.78 server is accessible, but port 81 is unreachable.

How to use Linux nc command

2. Copy the file

First activate nc monitoring on the file receiving terminal test2 machine

How to use Linux nc command

Then send the file on the file sending terminal test1 machine

How to use Linux nc command

Check whether the file has been successfully received on test2

How to use Linux nc command

3. Communication and chat between terminals

Start nc monitoring on the test1 host, and ctrl C interrupts communication.

How to use Linux nc command

#Connect to monitor on test2, ctrl C interrupts communication.

How to use Linux nc command

4、端口扫描

端口扫描,通的端口返回succeeded,不通的端口返回refused。此扫描基于nc-1.84-24.el6.x86_64。
[root@test1 /]# nc -v -w 1 172.16.7.78 -z 22-81
Connection to 172.16.7.78 22 port [tcp/ssh] succeeded!
nc: connect to 172.16.7.78 port 23 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 24 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 25 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 26 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 27 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 28 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 29 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 30 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 31 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 32 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 33 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 34 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 35 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 36 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 37 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 38 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 39 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 40 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 41 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 42 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 43 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 44 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 45 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 46 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 47 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 48 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 49 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 50 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 51 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 52 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 53 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 54 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 55 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 56 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 57 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 58 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 59 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 60 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 61 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 62 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 63 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 64 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 65 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 66 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 67 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 68 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 69 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 70 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 71 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 72 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 73 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 74 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 75 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 76 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 77 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 78 (tcp) failed: Connection refused
nc: connect to 172.16.7.78 port 79 (tcp) failed: Connection refused
Connection to 172.16.7.78 80 port [tcp/http] succeeded!
nc: connect to 172.16.7.78 port 81 (tcp) failed: Connection refused

5、验证UDP端口

[root@test1 ~]# nc -uvz 192.168.0.125 111
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.0.125:111.
Ncat: UDP packet sent successfully
Ncat: 1 bytes sent, 0 bytes received in 2.04 seconds.

6、测速网速

测速网速需要结合dstat命令查看。

安装dstat命令

[root@test1 ~]# yum install -y dstat
[root@test2 ~]# yum install -y dstat

Listening port on test1 host

[root@test1 ~]# nc -l 33333 >/dev/null

Send data on test2 host, all 0 data

[root@test2 ~]# nc 192.168.0.124 33333 /zero

View traffic

[root@test1 ~]# dstat
[root@test2 ~]# dstat

How to use Linux nc command

How to use Linux nc command

3. Usage syntax and parameter description

1. Usage syntax

Usage: ncat [options] [hostname] [port]

2. Parameter description

##-G Loose source route hop pointers (4, 8, 12,...) -m, --max-conns Maximum number of simultaneous connections-h, - -helpHelp Display this help screen-d, --delay Delay between read/write-o, --output Dump session data to file-x, -- hex-dump Dump session data to a file as hex-i, --idle-timeout Idle read/write timeout-p, --source-port portSpecify the source port to use-s, --source addrSpecify the source address to use (does not affect -l) -l, --listenBind and listen for incoming connections-k, --keep-openAccept multiple connections in listening mode-n, --nodnsDo not resolve hostname via DNS-t, --telnetReply to telnet negotiation-u, --udpUse udp instead of default TCP–sctpUse sctp instead of the default TCP-v, --verboseSet the verbose level (can be used multiple times)-w, --wait Connection timeout, in seconds-zOnly Report connection status–append-outputAppend instead of slamming the specified output file&send -onlyIgnore receiving; exit EOF##–recv-only–allow–allowfile–deny–denyfile–broker–chat–proxy –proxy-type &ndash ;proxy-auth –ssl–ssl-cert–ssl -key–ssl-verify–ssl-trustfile##–ssl-ciphersCipherlist contains the ssl cipher to use–versionView command version
Parameter Parameter description
-4 Use IPv4 only
-6 Use IPv6 only
-U, --unixsock Use only Unix domain sockets
-C , --crlf Use crlf for EOL sequence
-c, --sh-exec via /bin/ sh executes the given command
-e, --exec executes the given command
–lua-exec Execute the given lua script
-g hop1[,hop2,…] Loose source route hops (up to 8)
Never send anything
The given host is connected to Ncat
The file that allows the host to connect to Ncat
The given host is connected to Ncat
Deny the connection to Ncat Host file
Enable Ncat’s connection proxy mode
Start a simple Ncat chat server
Specify the host address to be passed through the proxy
Specify the proxy type ("http" or "socks4" or "socks5")
Authenticate via HTTP or SOCKS proxy server
Connect or listen using ssl
Specify the ssl certificate file (PEM) used for listening
Specify the ssl private key (PEM) used for listening
Trust and domain name of the certificate
PEM file containing trusted ssl certificate

The above is the detailed content of How to use Linux nc command. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete