telnet in Linux: a TCP client and a port test

Introduction

telnet is a protocol for a remote terminal session, and also the name of the client program that speaks it. The protocol sends everything, passwords included, as plain text, so nobody logs in over it any more.

The client stayed useful for a smaller job: it opens a raw TCP connection to a host and a port and shows what the server answers. That makes it a quick port test on Linux , next to the tools of the article Networks .

The client on Windows has its own article: telnet in Windows .

The commands on this page were run on Alpine Linux 3.24 with GNU inetutils telnet 2.8 and BusyBox netcat 1.37, and the output quoted below is that run. The commands that name a remote host (192.0.2.10, example.com) were run against 127.0.0.1 and a local listener instead. The apt, dnf and ssh commands were not run: this is not a Debian, Ubuntu or .rpm system, and it has no SSH client. Debian and Ubuntu differ in: their netcat is OpenBSD netcat, which prints succeeded! where BusyBox prints open, and which takes a port range; their telnet is the same GNU inetutils client as here.

Install the client

Most distributions do not install the client by default. On Debian and Ubuntu

sudo apt install telnet

On CentOS , Rocky and the other .rpm distributions (on CentOS 7 use yum instead of dnf)

sudo dnf install telnet

The server side is a separate package, telnet-server on the .rpm systems (on RHEL , Rocky and CentOS 8 and newer it comes from EPEL), and it is needed only when you want the old login service itself. The client is enough for everything below.

Open a TCP connection

The command takes a host and a port.

telnet 192.0.2.10 25

A name works as well as an address, and the port may be a name from /etc/services:

telnet example.com smtp

After the connection is up, everything you type goes to the server as is, and everything the server sends is printed. There is no prompt of its own and no line editing. The first three lines are always the same, and the rest is the server's: here it is a listener that closes at once.

telnet 127.0.0.1 12345

Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. Connection closed by foreign host.

To leave a session, press Ctrl + ]: the program answers with its own prompt telnet>, and quit closes the connection.

telnet> quit

A script sends the same two things without a keyboard: the escape character is octal 035.

printf '\035quit\n' | telnet 127.0.0.1 12345

Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. telnet> quit Connection closed.

An HTTP server shows the protocol in plain words: type a request line, the Host header and a blank line, and the response headers appear in the same terminal, followed by the body.

telnet 127.0.0.1 8080

Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. GET / HTTP/1.0 Host: 127.0.0.1 HTTP/1.0 200 OK Server: SimpleHTTP/0.6 Python/3.14.10 Date: Thu, 26 Nov 2026 07:38:49 GMT Content-type: text/html Content-Length: 6 Last-Modified: Thu, 26 Nov 2026 07:38:33 GMT hello Connection closed by foreign host.

One caveat belongs to scripts: when the input is a pipe and not a terminal, the client sends every Enter as a carriage return followed by a NUL byte, and an HTTP server answers 400 Bad request version instead. Type the request, do not pipe it.

Test a port

The useful part today is the question «is anything listening there?». telnet answers it before any protocol is spoken: either the connection is established and the server banner or silence follows, or the program reports that the connection was refused or timed out.

For the first answer, start a listener in another terminal and connect to it; the transcript is the one above. For the second, ask a port where nothing listens:

nc -l -p 12345

telnet 127.0.0.1 12346

Trying 127.0.0.1... telnet: Unable to connect to remote host: Connection refused

The answer on the screen is enough for a one-off check. A script needs a status, and there netcat is the better tool: its exit code is 0 when the port is open and 1 when it is not. That is the command checked for this page: a listener was started on port 12345 and asked from the same machine.

nc -z -v -w 2 127.0.0.1 12345

127.0.0.1 (127.0.0.1:12345) open

That line comes from BusyBox netcat. The OpenBSD netcat that Debian and Ubuntu install prints Connection to 127.0.0.1 12345 port [tcp/*] succeeded! instead; the exit code is the same.

The same command against a port with no listener prints nothing and exits with 1, so the exit code is the answer to read:

nc -z -w 2 127.0.0.1 12346; echo $?

1

A closed port answers at once with a refusal; a filtered port stays silent until the timeout, which is why -w is worth giving. telnet waits for its own timeout, much longer by default.

Why not for login

The login service listens on TCP port 23 and sends the password over the network as clear text: anyone on the path — a switch port, a Wi-Fi access point, a router — can read it with a sniffer. There is no encryption, no host key to check, and no way to tell a real server from an impostor.

For that reason the server package is not installed by default on any current distribution, and some do not ship it at all. Port 23 open to the internet is found by scanners within hours and used for brute force.

The replacement is SSH : it encrypts the session and authenticates both sides. A device that offers only telnet belongs on a management network, never on a public address.

Alternatives: nc, ssh, curl

nc (netcat) does the same raw TCP job, scripts better and can scan a range of ports. OpenBSD netcat, the one in Debian and Ubuntu, takes the range as 20-30:

nc -zv 192.0.2.10 20-30

BusyBox netcat, the one on Alpine, does not take a range: it stops with nc: bad port '20-30'.

SSH covers both jobs at once. It is the login tool, and with -v it reports the handshake of a port, which is a port test that also proves the service is really SSH:

ssh -v -p 22 user@192.0.2.10 exit

curl is the right test for an HTTP or HTTPS port: it speaks the protocol instead of opening a socket, so a curl request tells a working web server from a port that merely accepts connections.

curl -sv http://192.0.2.10:8080/ -o /dev/null

Against a local web server the same command shows the request going out and the answer coming back:

curl -sv http://127.0.0.1:8080/ -o /dev/null

* Trying 127.0.0.1:8080... * Established connection to 127.0.0.1 (127.0.0.1 port 8080) from 127.0.0.1 port 53818 * using HTTP/1.x > GET / HTTP/1.1 > Host: 127.0.0.1:8080 > User-Agent: curl/8.23.0 > Accept: */* > * Request completely sent off * HTTP 1.0, assume close after body < HTTP/1.0 200 OK < Server: SimpleHTTP/0.6 Python/3.14.10 < Date: Thu, 26 Nov 2026 07:38:39 GMT < Content-type: text/html < Content-Length: 6 < Last-Modified: Thu, 26 Nov 2026 07:38:33 GMT < { [6 bytes data] * shutting down connection #0

For a quick look at what travels over the wire once a connection is open, the packet analyzer of the article Wireshark is the next step.

Article author: Arthur Isaev