首頁·終端機命令

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)