Netcat是一個非常有用的Unix命令,我們可以用來執行各種網絡任務,它對調試以及學習工作原理也非常有用。
可通過nc
命令。
使用以下語法連接到任何網絡服務器:
nc DOMAIN PORT
nc localhost 8000
將其連接到服務器後,您可以通過鍵入任何消息來發送任何消息,並且您將看到服務器發回的任何答复。
就像簡單的一對一聊天一樣,您可以在其上以協議(HTTP,FTP,SMTP等)的形式來進行應用程序級功能。
使用Netcat,您可以模擬支持Internet的所有那些應用程序級別協議。
您可以連接到Web服務器並向其發送HTTP協議說明。我可以通過以下方式連接到我的網站nc flaviocopes.com 80
,我可以寄出
GET / HTTP/1.1
Host: flaviocopes.com(the third line is an empty line)
and the server will reply back, with an HTTP/1.1 301 Moved Permanently
to https://flaviocopes.com/
response because I force HTTPS.

Here’s a simple example to interface with an “echo” server I built separately, that sends back what we send to it:

Netcat can work with TCP, the default protocol, or UDP.
To use UDP, add the -u
flag:
nc -u localhost 8000
You can create a server, too. Use the -l
(listen) option to listen on a specific port:
nc -l PORT
and Netcat will print every command received.
Try running the server with nc -l 8001
on one terminal window, and the client nc localhost 8001
on another, then send messages to the server by typing them in the client terminal.
Netcat can also be used for network inspection. You can scan the open ports of a server, in a specific range:
nc -v -z localhost 1-10000
Tip: combine with grep
to filter the noise: nc -v -z localhost 1-10000 2>&1 | grep succeeded
(if you’re curious which is the process using a port, run lsof -i :PORT
)
You can tell a nc
server to send the content of a file to the client that connects:
nc -l PORT < FILENAME
The client connecting with nc DOMAIN PORT
will get the content of that file printed out, but it can save the content to a file using nc DOMAIN PORT > FILENAME
, resulting in a basic file transfer.
After the file has been served, the server will terminate.
You can wrap that command in a simple Bash shell loop:
while true; do nc -l PORT < FILENAME; done
That’s the simplest implementation of a Web server:
while true; do nc -l 80 < index.html; done
You can tell the client to send to the server the instructions contained in a file:
nc DOMAIN PORT < FILENAME
Download my free Linux Commands Handbook
More cli tutorials:
- The Bash shell
- Introduction to Bash Shell Scripting
- The Fish Shell
- Shell, watch file content as it populates
- How to exit Vim
- UNIX Editors
- The UNIX Filesystem Commands
- Unix Shells Tutorial
- How to set an alias in a macOS or Linux shell
- A practical guide to Homebrew
- How to fix the xcrun invalid active developer path error in macOS
- The Command Line for Complete Beginners
- Introduction to Linux
- How to find the process that is using a port
- Linux commands: mkdir
- Linux commands: cd
- Linux commands: pwd
- Linux commands: rmdir
- Linux commands: ls
- Linux commands: mv
- Linux commands: cp
- Linux commands: less
- Linux commands: tail
- Linux commands: touch
- Linux commands: cat
- Linux commands: find
- Linux commands: ln
- Linux commands: ps
- Linux commands: echo
- Linux commands: top
- Linux commands: kill
- Linux commands: killall
- Linux commands: alias
- Linux commands: jobs
- Linux commands: bg
- Linux commands: fg
- Linux commands: type
- Linux commands: which
- Linux commands: whoami
- Linux commands: who
- Linux commands: clear
- Linux commands: su
- Linux commands: sudo
- Linux commands: chown
- Linux commands: chmod
- Linux commands: passwd
- Linux commands: open
- Linux commands: wc
- Linux commands: history
- Linux commands: du
- Linux commands: umask
- Linux commands: grep
- Linux commands: man
- Linux commands: uname
- Linux commands: sort
- Linux commands: uniq
- Linux commands: diff
- Linux commands: nohup
- Linux commands: df
- Linux commands: xargs
- Linux commands: gzip
- Linux commands: gunzip
- Linux commands: ping
- Linux commands: traceroute
- Linux commands: tar
- Linux commands: export
- Linux commands: crontab
- Linux commands: dirname
- Linux commands: basename
- Linux commands: printenv
- Linux commands: env
- A short guide to the ed editor
- A short guide to vim
- A short guide to emacs
- A short guide to nano
- Linux, no space left on device
- How to use Netcat