Telnet 的替代方案: 使用 nc 实用程序进行网络诊断
Telnet 是一种网络协议,用于远程访问计算机,并提供基于文本的双向通信。它需要一个 telnet 服务器和一个客户端来进行交互。
Telnet 长期以来一直是 Linux/Windows 系统中广泛使用的工具。 然而,现代系统中使用 telnet 的主要问题是其安全性。Telnet 中的所有通信都以明文形式传输,网络流量未加密,这使得任何有权访问网络流量的人都能够轻易读取信息。 因此,大多数现代 Linux 操作系统不再预装 telnet,甚至不建议使用它。
随着 SSH(安全外壳协议)的出现,telnet 已经过时。SSH 不仅是 telnet 的加密替代品,也更适合用于其预期的目的。尽管如此,许多系统管理员和技术爱好者仍然使用 telnet 的一个替代用途:检查远程 TCP 端口的连通性。
可以使用 telnet 命令来检查远程 TCP 端口是否正在侦听并正确响应。以下代码片段展示了如何通过检查 HTTP/HTTPS 连接来确认 google.com 是否正常工作:
$ telnet google.com 80 Trying 142.250.183.206... Connected to google.com. Escape character is '^]'. ^] telnet> quit Connection closed. $ $ telnet google.com 443 Trying 142.250.183.206... Connected to google.com. Escape character is '^]'. ^] telnet> quit Connection closed. $
当使用 telnet 检查时,未打开或无法访问的 TCP 端口会有以下表现:
$ telnet google.com 22 Trying 142.250.193.174... ^C $
结合使用 ping、traceroute 或 tracepath、netstat 等命令,可以更容易地诊断简单的网络连接问题。
如果您使用的是 RHEL 8 (或更早版本的 RHEL/CentOS),您可以选择使用 nc (或 Ncat 或网络连接器),它提供了许多与网络诊断相关的选项。我们将讨论如何在 RHEL 8 和类似系统上安装和使用这个工具。
什么是 nc?
nc (或 Ncat) 是一种流行的通用命令行工具,用于跨网络读取、写入、重定向和加密数据。它最初是为 nmap 项目编写的,现在有多种 Netcat 实现可用。它支持跨 IPv4 和 IPv6 的 TCP 和 UDP,并提供了丰富的应用场景。
以下是 nc 实用程序的主要功能:
- 能够将 ncat 链接在一起
- 将 TCP、UDP 和 SCTP 端口重定向到其他站点
- 使用 SSL 支持加密通信
- 通过 SOCK4/5 或 HTTP 代理支持代理 (包括身份验证)
- 支持多种平台,包括 Windows、Linux 和 macOS
安装 nc
nc 作为 RHEL 系统中默认存储库的一部分提供。要在 RHEL 7 系统上安装,只需在终端执行以下命令:
$ sudo yum install -y nc
对于 RHEL 8 系统,可以使用 dnf 命令安装:
$ sudo dnf install -y nc
检查 TCP 连接
尽管 nc 提供了许多功能来支持各种应用程序,但其常见用途之一是在网络故障排除期间替代 telnet。
nc 可以显示您是否可以访问 TCP 端口。以下是命令语法:
$ nc -vz <IP/DNS> <Port>
例如,如果您想检查是否可以通过 HTTP 或 HTTPS 访问 techblik.com。 可以使用 nc 进行检查,如下所示(端口 80 用于 HTTP,而 443 用于 HTTPS):
$ nc -vz techblik.com.com 80 Ncat: Version 7.70 ( https://nmap.org/ncat ) Ncat: Connected to 104.26.11.88:80. Ncat: 0 bytes sent, 0 bytes received in 0.02 seconds. $ $ nc -vz techblik.com.com 443 Ncat: Version 7.70 ( https://nmap.org/ncat ) Ncat: Connected to 104.26.10.88:443. Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds. $
类似地,无法访问或被阻止的端口将显示如下输出(由于 techblik.com 的 DNS 指向多个 IP,这里检查多个地址):
$ nc -vz techblik.com.com 22 Ncat: Version 7.70 ( https://nmap.org/ncat ) Ncat: Connection to 172.67.70.213 failed: Connection timed out. Ncat: Trying next address... Ncat: Connection to 104.26.11.88 failed: Connection timed out. Ncat: Trying next address... Ncat: Connection to 104.26.10.88 failed: Connection timed out. Ncat: Trying next address... Ncat: Connection to 2606:4700:20::681a:a58 failed: Network is unreachable. Ncat: Trying next address... Ncat: Connection to 2606:4700:20::681a:b58 failed: Network is unreachable. Ncat: Trying next address... Ncat: Network is unreachable. $ $ dig techblik.com.com +short 104.26.10.88 172.67.70.213 104.26.11.88 $
检查 UDP 连接
telnet 只能检查与远程 TCP 端口的通信,而 nc 允许您检查 TCP 和 UDP 连接。
nc 可以使用以下命令发送 UDP 数据包,而不是默认的 TCP 数据包:
$ nc -vzu <IP/DNS> <Port>
但是,UDP 是一种无会话协议,与 TCP 不同,因此,不能仅仅通过在一端发送 UDP 数据包来确认所有情况下的端到端 UDP 连接,除非远程端的侦听进程发送一些响应。nc 将无法判断其发送的数据包是否到达目的地。但是,nc 提供了另一种方法来通过启动 UDP 侦听器来确定端到端 UDP 连接,假设可以正确访问远程服务器上的 CLI。
例如,如果您需要使用 nc 检查两个 Linux 主机之间用于 DNS 的 UDP 连接,一个简单的方法是启动 nc 服务器来侦听所需的端口:
$ sudo nc -ul <Port>
对于 DNS,我们需要检查端口 53,所以命令将是:
$ nc -ul 53
在客户端,需要启动另一个 nc 进程,将 UDP 数据包发送到服务器:
$ nc -u <IP/DNS> <Port>
这将使得我们的命令为:
$ nc -u <IP/DNS> 53
假设这两台机器之间端口 53 的 UDP 流量没有被阻止,您在一台机器上键入和输入的任何内容都应该在另一台主机上可见,就像双向聊天一样。如果不是,则某些防火墙会阻止这两个系统之间的连接。
使用 nc 的服务器和客户端模型非常适用于主机间的简单连接检查。与上述 UDP 检查类似,nc 也可以在指定端口上监听 TCP 数据包:
$ sudo nc -l <Port>
在客户端,可以正常发送 TCP 数据包以检查连通性:
$ nc <IP/DNS> <Port>
对于 TCP 连接(与 UDP 不同),不需要上述服务器/客户端 nc 方法,因为它是面向连接的协议,并使用确认。任何在 TCP 上工作的监听进程都会直接响应 nc TCP 数据包。
总结
本文概述了 nc 实用程序如何直接替代现代 Linux 系统中的 telnet,用于检查端口连通性,并为用户提供更多诊断和解决网络问题的能力。
可以使用 nc -h
命令访问 nc 的帮助信息:
$ nc -h Ncat 7.70 ( https://nmap.org/ncat ) 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 -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) -G <n> Loose source routing hop pointer (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) -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 -u, --udp Use UDP instead of default TCP --sctp Use SCTP instead of default TCP -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-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 --ssl-alpn ALPN protocol list to use. --version Display Ncat's version information and exit See the ncat(1) manpage for full options, descriptions and usage examples $
有关 nc 命令的更多详细信息,请参阅其手册页:
$ man nc