Linux网络瑞士军刀 nc(netcat)

1.命令简介

nc(netcat)是一个短小精悍、功能实用、简单可靠的网络工具,主要有如下作用:

(1)端口侦听,nc 可以作为 server 以 TCP 或 UDP 方式侦听指定端口;

(2)端口扫描,nc 可以作为 client 发起 TCP 或 UDP 请求;

(3)机器之间传输文件;

(4)机器之间网络测速。

2.命令格式

shell 复制代码
nc [-46DdhklnrStUuvzC] [-i interval] [-p source_port] [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [hostname] [port[s]]

常用选项:

  • -4:只使用 IPv4 地址
  • -6:只使用 IPv6 地址
  • -l:启动本地监听
  • -n:不使用 DNS 解析
  • -p:指定源端口
  • -s:指定源 IP 地址
  • -u:使用 UDP,默认是 TCP
  • -v:显示详细信息
  • -w:设定超时时间(只适合用在 Client 端)
  • -d:禁止从标准输入读取数据,也就是客户端输入数据不会发送到服务端
  • -k:让服务端保持连接,不断开

3.常用示例

模拟服务端

shell 复制代码
nc -vkl 8080

模拟客户端

shell 复制代码
nc -v 127.0.0.1 8888

连接日志

shell 复制代码
#server
root@VM-16-3-ubuntu:~# nc -vkl 8080
Listening on 0.0.0.0 8080
Connection received on localhost 42930

#client
root@VM-16-3-ubuntu:~# nc -v 127.0.0.1 8080
Connection to 127.0.0.1 8080 port [tcp/http-alt] succeeded!
#随后双方便可输入消息发送

发送文件

nc 不仅可以发送消息,还可发送文件。

假设服务端有一个 out.txt 的空文件,而客户端有一个 in.txt 文件,含有数据:hello server

Server 端接收文件:

shell 复制代码
nc localhost 6000 > out.txt

Client 端发送文件:

shell 复制代码
nc localhost 6000 < in.txt

之后,我们可以看到 Server 端的 out.txt 文件中已经有数据了:

shell 复制代码
# cat out.txt
hello server

除了可以发送文件,nc 也可以发送目录,只需要将目录压缩发送即可。

端口扫码

端口扫描是一个非常重要的功能,很多时候系统管理员会通过扫描服务器上端口,来识别系统中漏洞,nc 工具提供了非常方便的操作:

shell 复制代码
nc -vz 127.0.0.1 1-100

这条命令扫描 192.168.1.3 上 1-100 端口区间,有哪些端口是开放的。

shell 复制代码
# nc -vz 127.0.0.1 1-100
nc: connect to 127.0.0.1 port 1 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 2 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 3 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 4 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 5 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 6 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 7 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 8 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 9 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 10 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 11 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 12 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 13 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 14 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 15 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 16 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 17 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 18 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 19 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 20 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 21 (tcp) failed: Connection refused
Connection to 127.0.0.1 22 port [tcp/ssh] succeeded!
nc: connect to 127.0.0.1 port 23 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 24 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 25 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 26 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 27 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 28 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 29 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 30 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 31 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 32 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 33 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 34 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 35 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 36 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 37 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 38 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 39 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 40 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 41 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 42 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 43 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 44 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 45 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 46 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 47 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 48 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 49 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 50 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 51 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 52 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 53 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 54 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 55 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 56 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 57 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 58 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 59 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 60 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 61 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 62 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 63 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 64 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 65 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 66 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 67 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 68 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 69 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 70 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 71 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 72 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 73 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 74 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 75 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 76 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 77 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 78 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 79 (tcp) failed: Connection refused
Connection to 127.0.0.1 80 port [tcp/http] succeeded!
nc: connect to 127.0.0.1 port 81 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 82 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 83 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 84 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 85 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 86 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 87 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 88 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 89 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 90 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 91 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 92 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 93 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 94 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 95 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 96 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 97 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 98 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 99 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 100 (tcp) failed: Connection refused

可以看到,只有 22和80号端口是开放的。

总结

nc 通过在两台机器之间建立连接来完成很多网络功能,数据传输、网络连接、端口扫描等,也有助于我们进行网络调试,排查网络故障。

相关推荐
梅见十柒21 分钟前
wsl2中kali linux下的docker使用教程(教程总结)
linux·经验分享·docker·云原生
Koi慢热24 分钟前
路由基础(全)
linux·网络·网络协议·安全
传而习乎34 分钟前
Linux:CentOS 7 解压 7zip 压缩的文件
linux·运维·centos
soulteary36 分钟前
突破内存限制:Mac Mini M2 服务器化实践指南
运维·服务器·redis·macos·arm·pika
我们的五年44 分钟前
【Linux课程学习】:进程程序替换,execl,execv,execlp,execvp,execve,execle,execvpe函数
linux·c++·学习
IT果果日记1 小时前
ubuntu 安装 conda
linux·ubuntu·conda
Python私教1 小时前
ubuntu搭建k8s环境详细教程
linux·ubuntu·kubernetes
羑悻的小杀马特1 小时前
环境变量简介
linux
小陈phd2 小时前
Vscode LinuxC++环境配置
linux·c++·vscode
运维&陈同学2 小时前
【zookeeper01】消息队列与微服务之zookeeper工作原理
运维·分布式·微服务·zookeeper·云原生·架构·消息队列