在 RHEL 8 上尋找遠程登錄? 試試數控
已發表: 2022-05-02Telnet 是一種用於遠程訪問計算機並提供基於文本的雙向通信的網絡協議。 所以你需要一個 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 $
結合ping
、 traceroute
或tracepath
、 netstat
等命令,可以輕鬆解決簡單的網絡連接問題。
如果您使用的是 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>
例如,如果我想檢查是否可以通過http
或https
訪問 Geekflare。 我可以檢查使用nc
如下所示(端口80
用於http
而443
用於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