温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux nc命令如何使用

发布时间:2023-03-15 11:58:08 来源:亿速云 阅读:124 作者:iii 栏目:开发技术

这篇文章主要介绍了Linux nc命令如何使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Linux nc命令如何使用文章都会有所收获,下面我们一起来看看吧。

一、命令简介

nc是netcat的简写,是一个功能强大的网络工具,有着网络界的瑞士军刀美誉。nc命令在linux系统中实际命令是ncat,nc是软连接到ncat。nc命令的主要作用如下:

实现任意TCP/UDP端口的侦听,nc可以作为server以TCP或UDP方式侦听指定端口端口的扫描,nc可以作为client发起TCP或UDP连接机器之间传输文件机器之间网络测速

nc如果找不到nc命令可以使用yum install -y nc安装,其中centos6系统安装的是nc-1.84-24.el6.x86_64,centos7安装的是nmap-ncat-6.40-19.el7.x86_64。

二、使用示例

1、验证服务器端口是否通

如下验证172.16.7.78服务器的80端口通,81端口不通。

Linux nc命令如何使用

2、拷贝文件

首先在文件接收终端test2机器上激活nc监听

Linux nc命令如何使用

然后在文件发送终端test1机器上发送文件

Linux nc命令如何使用

在test2上检查文件是否已成功接收

Linux nc命令如何使用

3、终端之间通信聊天

test1主机上启动nc监听,ctrl+C中断通信。

Linux nc命令如何使用

test2上连接监听,ctrl+C中断通信。

Linux nc命令如何使用

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

test1主机上监听端口

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

test2主机上发送数据,全0数据

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

查看流量

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

Linux nc命令如何使用

Linux nc命令如何使用

三、使用语法及参数说明

1、使用语法

用法:ncat [options] [hostname] [port]

2、参数说明

参数参数说明
-4Use IPv4 only
-6Use IPv6 only
-U, --unixsock仅使用Unix域套接字
-C, --crlf将crlf用于EOL序列
-c, --sh-exec <command>通过/bin/sh执行给定的命令
-e, --exec <command>执行给定的命令
&ndash;lua-exec <filename>执行给定的lua脚本
-g hop1[,hop2,&hellip;]松散源路由跃点(最多8个)
-G <n>松散源路由跃点指针(4,8,12,&hellip;)
-m, --max-conns <n>最大同时连接数
-h, --help帮助显示此帮助屏幕
-d, --delay <time>读/写之间的延迟
-o, --output <filename>将会话数据转储到文件
-x, --hex-dump <filename>将会话数据作为十六进制转储到文件
-i, --idle-timeout <time>空闲读/写超时
-p, --source-port port指定要使用的源端口
-s, --source addr指定要使用的源地址(不影响-l)
-l, --listen绑定并侦听传入连接
-k, --keep-open在侦听模式下接受多个连接
-n, --nodns不通过DNS解析主机名
-t, --telnet应答telnet协商
-u, --udp使用udp而不是默认TCP
&ndash;sctp使用sctp而不是默认的TCP
-v, --verbose设置详细级别(可以多次使用)
-w, --wait <time>连接超时时间,单位秒
-z仅报告连接状态
&ndash;append-output追加而不是重击指定的输出文件
&ndash;send-only忽略接收;退出EOF
&ndash;recv-only从不发送任何东西
&ndash;allow给定主机连接到Ncat
&ndash;allowfile允许连接到Ncat的主机的文件
&ndash;deny给定主机连接到Ncat
&ndash;denyfile拒绝连接到Ncat的主机文件
&ndash;broker启用Ncat的连接代理模式
&ndash;chat开始一个简单的Ncat聊天服务器
&ndash;proxy <addr[:port]>指定要通过代理的主机地址
&ndash;proxy-type <type>指定代理类型(“http”或“socks4”或“socks5”)
&ndash;proxy-auth <auth>通过HTTP或SOCKS代理服务器进行身份验证
&ndash;ssl使用ssl连接或侦听
&ndash;ssl-cert指定用于侦听的ssl证书文件(PEM)
&ndash;ssl-key指定用于侦听的ssl私钥(PEM)
&ndash;ssl-verify证书的信任和域名
&ndash;ssl-trustfile包含可信ssl证书的PEM文件
&ndash;ssl-ciphersCipherlist包含要使用的ssl密码
&ndash;version查看命令版本

关于“Linux nc命令如何使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Linux nc命令如何使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI