Telnet: Great for Testing if a TCP Port is Open (and Remote Login)

Telnet is commonly used to check if a TCP connection to a specific port succeeds — essentially testing whether the port is open and reachable. Note that telnet itself is TCP-based, so it cannot test UDP sockets.

Syntax Basic:

telnet [options] host [port]

More complete:

telnet [-8acdEfFKLrx] [-b<host_alias>] [-e<escape_char>] [-k<realm>] [-l<user>] [-n<tracefile>] [-S<tos>] [-X<authtype>] [host [port]]

Quick background Telnet is the classic remote login client based on the TELNET protocol. It lets you run commands on a remote server as if you were sitting in front of it. You log in with username/password, but everything (including credentials) is sent in clear text — very insecure. Most modern Linux servers disable telnet and use SSH instead.

That said, curl can test HTTP services, and telnet can too (more on that below).

For UDP testing, use:

  • nc -u
  • nmap -sU
  • tcpdump
  • Application-level tools like dig for DNS

Common options

  • -4 Force IPv4
  • -6 Force IPv6
  • -8 Allow 8-bit data
  • -a Attempt automatic login (rarely used now)
  • -b<alias> Use alias for host
  • -e<char> Set escape character (default Ctrl+]) Example: telnet -e ^X host port
  • -l<user> Specify login user (for traditional telnet servers) Example: telnet -l root 192.168.1.10
  • -n<file> Log session to file (great for debugging)

In interactive mode After connecting, press Ctrl+] to enter telnet command mode. Useful commands:

  • quit Exit telnet
  • close Close current connection
  • status Show connection status
  • open host port Connect to new host/port
  • set localecho Enable local echo
  • toggle crlf Toggle CR/LF handling

Examples

  1. Connect to a remote telnet service
telnet 192.168.120.206

(or by domain: telnet www.baidu.com)

If it fails:

  • Check IP/address
  • Is the host up?
  • Routing correct? (route)
  • Is telnet service running? (netstat -tlnp → look for TCP 23 LISTEN)
  • Firewall allowing port 23? (iptables -L)
  1. Test TCP reachability (the 80% use case)
telnet 127.0.0.1 6379

Outcomes:

  • Connected → port open and TCP reachable
  • Connection refused → port not listening
  • Connection timed out → network/firewall issue
  1. As a generic TCP client Telnet doesn’t care about application protocols — it just sends whatever you type over TCP. Perfect for quick testing of text-based protocols.

HTTP example:

telnet example.com 80
GET / HTTP/1.1
Host: example.com

(then hit Enter twice)

Redis example:

telnet 127.0.0.1 6379
PING

→ +PONG

Typical telnet service config (/etc/xinetd.d/telnet)

service telnet
{
    disable = no
    flags = REUSE
    socket_type = stream
    wait = no
    user = root
    server = /usr/sbin/in.telnetd
    log_on_failure += USERID
}

Restrict access:

only_from = 10.0.0.2
no_access = 10.0.0.{2,3,4}
access_times = 9:00-12:00 13:00-17:00
bind = 10.0.0.2

Limitations

  • Clear-text transmission of credentials → never use for real logins in production (use SSH)
  • Can’t test UDP
  • Binary protocols will garble output → stick to text protocols

Modern role: Telnet is best positioned as a quick TCP port/text-protocol debugging tool, not a login method.

Related tools

  • nc/netcat → Swiss army knife for TCP/UDP
  • curl → HTTP/HTTPS testing
  • ssh → Secure remote login
  • nmap → Port scanning (including UDP)

telnet登入远程主机、测试TCP链接是否可达

功能说明:telnet可以用于测试TCP socket链接是否连通(某个TCP端口是否开放)。但telnet不能测试 UDP socket链接,因为telnet基于TCP 协议,不支持 UDP。

语  法:

1 基本命令格式

telnet [选项] 主机 [端口]

2 复杂命令格式

telnet [-8acdEfFKLrx][-b<主机别名>][-e<脱离字符>][-k<域名>][-l<用户名称>][-n<记录文件>][-S<服务类型>][-X<认证形态>][主机名称或IP地址<通信端口>]

补充说明:

1telnet一般用于登入远程主机,与之类似的工具是ssh。telnet程序是基于TELNET协议的远程登录客户端程序。Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式。它为用户提供了在本地计算机上完成远程主机工作的 能力。在终端使用者的电脑上使用telnet程序,用它连接到服务器。终端使用者可以在telnet程序中输入命令,这些命令会在服务器上运行,就像直接在服务器的控制台上输入一样。可以在本地就能控制服务器。要开始一个 telnet会话,必须输入用户名和密码来登录服务器。但是telnet因为采用明文传送报文,安全性不好,很多Linux服务器都不开放telnet服务,而改用更安全的ssh了。

2 curl可以测试HTTP服务,telnet也可以测试HTTP服务。

3 那怎么测UDP?可以使用以下工具:

  • nc -u
  • nmap -sU
  • tcpdump
  • 应用层工具dig测试DNS

选  项:

-4    强制IPv4地址解析

-6    强制进行IPv6地址解析

-8   允许使用8位字符资料,包括输入与输出。

-a   尝试自动登入远端系统(已很少使用)。

-b<主机别名>   使用别名指定远端主机名称。

-c   不读取用户专属目录里的.telnetrc文件。

-d   启动排错模式。

-e<脱离字符>   设置escape字符(默认是 Ctrl + ]),用于优雅退出telnet例如:telnet -e ^X host port

-E   滤除脱离字符。

-f   此参数的效果和指定”-F”参数相同。

-F   使用Kerberos V5认证时,加上此参数可把本地主机的认证数据上传到远端主机。

-k<域名>   使用Kerberos认证时,加上此参数让远端主机采用指定的领域名,而非该主机的域名。

-K   不自动登入远端主机。

-l<用户名称>   指定要登入远端主机的用户名称(主要用于传统 telnet server)。例如:telnet -l root 192.168.1.10

-L   允许输出8位字符资料。

-n<记录文件>   指定文件记录通信数据(调试用)。用法:telnet -n telnet.log host port

-r   使用类似rlogin指令的用户界面。

-S<服务类型>   设置telnet连线所需的IP TOS信息。

-x   假设主机有支持数据加密的功能,就使用它。

-X<认证形态>   关闭指定的认证形态。

telnet交互模式下的常用命令:

连接到远程服务后,按:

Ctrl + ]

进入 telnet 命令模式。常用命令有:

quit        退出 telnet

close      关闭当前连接

status     查看连接状态

open host port     打开新连接

set localecho        本地回显

toggle crlf           切换 CRLF

    例:

1 连接到远程主机的telnet服务

telnet 192.168.120.206

如果连接失败,排查步骤如下:

  • 确认ip地址是否正确?
  • 确认ip地址对应的主机是否已经开机?
  • 如果主机已经启动,确认路由设置是否设置正确?(使用route命令查看)

如果主机已经启动,确认主机上是否开启了telnet服务?(使用netstat命令查看,TCP的23端口是否有LISTEN状态的行),可以使用以下命令启动telnet服务service xinetd restart

  • 如果主机已经启动telnet服务,确认防火墙是否放开了23端口的访问?(使用iptables-save查看)

主机名可以换成域名,例如:

telnet www.baidu.com

2 测试 TCP 连接是否可达

telnet 127.0.0.1 6379

可能出现以下结果:

  • 连接成功,意味着6379端口开放 + TCP 通
  • Connection refused,意味着端口未监听
  • Connection timed out,意味着网络 / 防火墙问题

这是 telnet 最常见用途(80% 场景)。

3 作为“通用 TCP 客户端”

telnet 不关心上层协议,只管:把你输入的字符,通过 TCP 发给对方。所以你可以用它来测试:HTTP、SMTP、Redis(文本命令)或自定义 TCP 文本协议。

示例:测试 HTTP

telnet example.com 80

然后输入:

GET / HTTP/1.1
Host: example.com

示例:测试 Redis

telnet 127.0.0.1 6379

输入:

PING

返回:

+PONG

telnet服务常用配置:

service telnet
{
    disable = no #启用
    flags = REUSE #socket可重用
    socket_type = stream #连接方式为TCP
    wait = no #为每个请求启动一个进程
    user = root #启动服务的用户为root
    server = /usr/sbin/in.telnetd #要激活的进程
    log_on_failure += USERID #登录失败时记录登录用户名
}

#配置允许登录的客户端列表
only_from = 10.0.0.2 #只允许10.0.0.2登录

#配置禁止登录的客户端列表
no_access = 10.0.0.{2,3,4}  #禁止10.08.0.2、10.0.0.3、10.0.0.4登录

#设置开放时段
access_times = 9:00-12:00 13:00-17:00 # 每天只有这两个时段开放服务

#配置用户只从某个地址登录telnet服务
bind = 10.0.0.2

telnet的局限性:

  • 明文传输用户名、密码等敏感数据(非常不安全),不适合生产环境登录。生产环境登录应该使用ssh。
  • 不能测试 UDP。
  • 不适合二进制协议,因为会乱码,只适合文本。

现代环境中 telnet 的“正确定位”是TCP 端口 / 文本协议的调试工具,而不是登录工具。

其他相关的网络工具:

  • nc (netcat):TCP / UDP 万能工具
  • curl:测试HTTP / HTTPS
  • ssh:用于安全登录
  • nmap:用于端口扫描,也能测 UDP

Telnet Man Page Lists “The source code is not comprehensible” as a BUG

Here’s the man page for the telnet command in an old Ubuntu release:

http://manpages.ubuntu.com/manpages/karmic/man1/telnet-ssl.1.html

Scroll all the way to the bottom and you’ll find this under BUGS:

The source code is not comprehensible.

The source package is here: http://packages.ubuntu.com/source/dapper/netkit-telnet

Download it and take a look — it’s genuinely hard to read. A quick scan reveals at least these issues:

  1. Indentation mixes spaces and tabs, so large parts of the code appear completely unindented.
  2. Heavy use of #if/#else and a ton of preprocessor macros, including some odd ones like:
#ifndef B19200
#define B19200 B9600
#endif
#ifndef B38400
#define B38400 B19200
#endif
  1. What looks like C++ written in C — there are actual class declarations in terminal.cc.
  2. Variable names are cryptic (lots of old, tmp, c1, c2, s1, s2, s3), function naming is inconsistent, and there’s basically no coding style guide.

It’s legitimately tough to follow.

Still, huge respect to whoever decided to call out the source code’s readability as an official BUG in the man page.

(via coolshell.cn)