Home > Article > Operation and Maintenance > How to use Linux nc command
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.
As follows, verify that port 80 of the 172.16.7.78 server is accessible, but port 81 is unreachable.
First activate nc monitoring on the file receiving terminal test2 machine
Then send the file on the file sending terminal test1 machine
Check whether the file has been successfully received on test2
Start nc monitoring on the test1 host, and ctrl C interrupts communication.
#Connect to monitor on test2, ctrl C interrupts communication.
端口扫描,通的端口返回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
[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.
测速网速需要结合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
Usage: ncat [options] [hostname] [port]
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) |
Loose source route hop pointers (4, 8, 12,...) | |
Maximum number of simultaneous connections | |
Help Display this help screen | |
Dump session data to file | |
Dump session data to a file as hex | |
Specify the source port to use | |
Specify the source address to use (does not affect -l) | |
Bind and listen for incoming connections | |
Accept multiple connections in listening mode | |
Do not resolve hostname via DNS | |
Reply to telnet negotiation | |
Use udp instead of default TCP | |
Use sctp instead of the default TCP | |
Set the verbose level (can be used multiple times) | |
Only Report connection status | |
Append instead of slamming the specified output file | |
Ignore receiving; exit EOF | |
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 | ##–ssl-ciphers |
–version | |
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!