Linux 下测试 TCP|UDP 连通性

小助手读文章 00:00 / 00:00

最近在琢磨 Google 的 QUIC 协议,刚好遇到需要测试 UDP 是否会通的问题,这里做下记录。


测试通过 netcat 软件实现:


[email protected]:~/ubuntu16-caddy# apt install netcat
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
The following NEW packages will be installed:
  netcat netcat-traditional
0 upgraded, 2 newly installed, 0 to remove and 2 not upgraded.
Need to get 65.1 kB of archives.
After this operation, 157 kB of additional disk space will be used.
Do you want to continue? [Y/n] y


[[email protected] armx]# yum install nc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package nmap-ncat.x86_64 2:6.40-16.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

 Package                     Arch                     Version                            Repository              Size
 nmap-ncat                   x86_64                   2:6.40-16.el7                      base                   206 k

Transaction Summary
Install  1 Package

Total download size: 206 k
Installed size: 423 k
Is this ok [y/d/N]: y



[email protected]:~/ubuntu16-caddy# nc -l -u 443



[[email protected] armx]# nc -u 服务端IP 443



在服务端直接输入任意字符,观察客户端变化,可以测试服务端到客户端的连通性,如果客户端同步显示服务端输入的字符,说明服务端到客户端的 UDP 是通的,反之亦然。

UDP 测试.gif

如果不通,可能是防火墙阻止 UDP 流量,可以修改下放行;也可能是被 QOS 限制,可以尝试换个网络再测试一下。


nc 命令能实现的功能不仅仅以上测试 UDP,还可以测试 TCP、扫描端口、文件传输等等,具体可参见帮助文档:

[[email protected] ~]# nc --help
Ncat 7.50 ( )
Usage: ncat [options] [hostname] [port]

Options taking a time assume seconds. Append 'ms' for milliseconds,
's' for seconds, 'm' for minutes, or 'h' for hours (e.g. 500ms).
  -4                         Use IPv4 only
  -6                         Use IPv6 only
  -U, --unixsock             Use Unix domain sockets only
  -C, --crlf                 Use CRLF for EOL sequence
  -c, --sh-exec <command>    Executes the given command via /bin/sh 通过 /bin/sh 执行客户端参数过来的指令(通常使用方法为 -c bash)
  -e, --exec <command>       Executes the given command
      --lua-exec <filename>  Executes the given Lua script
  -g hop1[,hop2,...]         Loose source routing hop points (8 max) 松散源路由跳点(最多8个)
  -G <n>                     Loose source routing hop pointer (4, 8, 12, ...) 指向器数目 (4,8,12,...)
  -m, --max-conns <n>        Maximum <n> simultaneous connections
  -h, --help                 Display this help screen 查看帮助信息
  -d, --delay <time>         Wait between read/writes 同时建立的最大连接数
  -o, --output <filename>    Dump session data to a file 输出文件
  -x, --hex-dump <filename>  Dump session data as hex to a file 十六进制输出文件
  -i, --idle-timeout <time>  Idle read/write timeout 空闲读/写超时时间
  -p, --source-port port     Specify source port to use 指定特定的源端口
  -s, --source addr          Specify source address to use (doesn't affect -l) 指定特定的源 ip
  -l, --listen               Bind and listen for incoming connections 绑定并侦听传入的连接(一般用于服务端)
  -k, --keep-open            Accept multiple connections in listen mode 在监听模式下接受多个连接
  -n, --nodns                Do not resolve hostnames via DNS 不解析域名
  -t, --telnet               Answer Telnet negotiations 应答 Telnet 握手协议
  -u, --udp                  Use UDP instead of default TCP  使用 udp 连接(不用 -u 的话默认是使用 tcp 连接)
      --sctp                 Use SCTP instead of default TCP 使用 sctp 连接
  -v, --verbose              Set verbosity level (can be used several times) 输出详细的连接产生的日志
  -w, --wait <time>          Connect timeout 设置连接超时时间
  -z                         Zero-I/O mode, report connection status only
      --append-output        Append rather than clobber specified output files
      --send-only            Only send data, ignoring received; quit on EOF
      --recv-only            Only receive data, never send anything
      --allow                Allow only given hosts to connect to Ncat
      --allowfile            A file of hosts allowed to connect to Ncat
      --deny                 Deny given hosts from connecting to Ncat
      --denyfile             A file of hosts denied from connecting to Ncat
      --broker               Enable Ncat's connection brokering mode
      --chat                 Start a simple Ncat chat server
      --proxy <addr[:port]>  Specify address of host to proxy through
      --proxy-type <type>    Specify proxy type ("http" or "socks4" or "socks5")
      --proxy-auth <auth>    Authenticate with HTTP or SOCKS proxy server
      --ssl                  Connect or listen with SSL 传输过程中使用ssl加密(服务端和客户端都要使用这个参数)
      --ssl-cert             Specify SSL certificate file (PEM) for listening
      --ssl-key              Specify SSL private key (PEM) for listening
      --ssl-verify           Verify trust and domain name of certificates
      --ssl-trustfile        PEM file containing trusted SSL certificates
      --ssl-ciphers          Cipherlist containing SSL ciphers to use
      --version              Display Ncat's version information and exit

See the ncat(1) manpage for full options, descriptions and usage examples


1、《Linux nc 命令详解
2、《Linux 主机端口扫描工具 - Nmap

ArmxMod for Typecho


 继续浏览关于 linux联通教程建站方法udp测试 的文章

 本文最后更新于 2019/07/05 12:30:00,可能因经年累月而与现状有所差异

 引用转载请注明:VirCloud's Blog > 运维 > Linux 下测试 TCP|UDP 连通性


  1. 心灵博客


    1. 欧文斯


    2. 欧文斯

      这个确实比较有用 icon_mrgreen.gif

  2. 芒果