首页·终端命令

nc (netcat)

网络

用法

nc [-lv] <host> <port>

开一条裸的 TCP 或 UDP 连接来看端口是否应答,也能在两台机器间当管道;同名却互不兼容的 netcat 有三种(OpenBSD、GNU、Nmap 的 ncat),所以 `-z` 或 `-p` 在你这台上可能根本没有。

常用选项

选项含义
-lListen instead of connecting. On GNU netcat you also need -p <port>.
-vSay what happened, which is the whole point when testing a port.
-zScan without sending data. OpenBSD/macOS nc only; GNU netcat has no -z.
-w 3Give up after three seconds instead of hanging.
-uUDP instead of TCP.
-4 / -6Force one address family.

示例

nc -zv example.com 443

Is the port open? Works on macOS and OpenBSD netcat.

nc -l 1234 # then on the other host: nc <host> 1234

A raw two-way pipe for a quick test.

nc -l 1234 > incoming.bin

Receive a file that the other side sends with nc host 1234 < file.

连不上的时候,这些能把范围缩小——是名字解析不了、没有路由,还是端口被挡。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. nc (netcat) 是做什么的?

开一条裸的 TCP 或 UDP 连接来看端口是否应答,也能在两台机器间当管道;同名却互不兼容的 netcat 有三种(OpenBSD、GNU、Nmap 的 ncat),所以 `-z` 或 `-p` 在你这台上可能根本没有。

Q. 怎么写?

nc [-lv] <host> <port> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man nc。这条命令属于网络。

相关命令

man 手册: man nc-(netcat)