在 RHEL 8 上寻找远程登录? 试试数控

已发表: 2022-05-02

Telnet 是一种用于远程访问计算机并提供基于文本的双向通信的网络协议。 所以你需要一个 telnet 服务器和客户端来互相交谈。

Telnet 是一种流行的 Linux/Windows 实用程序,长期以来一直发挥其作用。

现代系统上 telnet 的一个主要问题是它不安全。 telnet 中的所有通信都以纯文本形式进行,并且所有网络流量都是未加密的。 基本上任何拥有适当访问权限和工具的人都可以窥探网络流量以读取此流量。 因此,大多数现代 Linux 操作系统都没有预装 telnet,其他人建议不要使用它。

随着 SSH 或 Secure Shell 协议的出现,它不仅仅是 telnet 的加密替代品,将 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 $

结合pingtraceroutetracepathnetstat等命令,可以轻松解决简单的网络连接问题。

如果您使用的是 RHEL 8(甚至是旧版本的 RHEL/CentOS),您可以选择使用 nc(或 Ncat 或网络连接器),它支持许多与网络诊断相关的选项。 我们将讨论如何在 RHEL8 和类似系统上安装和使用这个工具。

什么是nc?

nc(或 Ncat)是一种流行的通用命令行工具,用于跨网络读取、写入、重定向和加密数据。 最初是为nmap项目编写的,现在有多个 Netcat 实现可用。 它适用于跨 IPv4 和 IPv6 的 TCP 和 UDP,并提供无限的潜在用例。

以下是nc实用程序的一些主要功能:

  • 能够将ncats在一起
  • 将 TCP、UDP 和 SCTP 端口重定向到其他站点
  • 使用 SSL 支持加密通信
  • 通过 SOCK4/5 或 HTTP 代理支持代理(包括身份验证)
  • 支持多种平台,包括 Windows、Linux 和 macOS

安装数控

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>

例如,如果我想检查是否可以通过httphttps访问 Geekflare。 我可以检查使用nc如下所示(端口80用于http443用于https ):

 $ nc -vz geekflare.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 geekflare.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. $

同样,一个不可访问或被阻塞的端口将显示如下输出(检查多个地址,因为 Geekflare DNS 指向多个 IP):

 $ nc -vz geekflare.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 geekflare.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 主机之间的 UDP 连接以获取 DNS,一个简单的方法是启动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 (eg 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