telnet Command: Tutorial & Examples

Connect to a remote host using the Telnet protocol

Telnet is like the postman of the digital realm, allowing you to remotely connect to another computer or server over a network. It's a text-based protocol that enables you to access the command-line interface of a remote system. Think of it as a virtual terminal that bridges the gap between you and the distant server.

How It Works and Why It's Important

Telnet works by establishing a plain-text communication channel between your local machine and the remote server. It's crucial for scenarios where a graphical user interface (GUI) is impractical or unavailable, making it a go-to tool for managing servers, especially in headless environments.

Telnet Commands on Linux

Now, let's talk about the commands that make Telnet dance to your tunes on a Linux system.

telnet

The Telnet command itself is your gateway to remote systems. Open a terminal and type telnet, followed by the IP address or domain of the target server. It's like a phone call to the server's command line.

telnet 192.168.1.1

Ctrl + ]

Once you're connected, use Ctrl + ] to access Telnet's command prompt. From here, you can issue various Telnet-specific commands or even close the connection.

quit or exit

To gracefully exit Telnet, type either quit or exit at the Telnet command prompt. This ensures a proper termination of the connection.

Telnet in Action: Connecting to a HTTP server

Let's say you want to check the status of a service on a remote server. You can use Telnet to establish a connection and then execute the necessary commands.

  1. Open your terminal.

  2. Type the following command to initiate a Telnet connection to the HTTP server on port 80:

    telnet example.com 80
    

    Replace example.com with the actual domain or IP address of the HTTP server you want to connect to.

  3. Press Enter.

  4. You should see a connection message, indicating a successful connection to the server.

  5. Now, you can manually send an HTTP request. For example, to request the homepage, type the following and press Enter:

    GET / HTTP/1.1
    Host: example.com
    

    This is a basic HTTP GET request for the root path ("/") with the specified HTTP version and the host header.

  6. Press Enter again.

  7. You should receive the HTTP response from the server. It will include information about the server, along with the HTML content of the homepage.

  8. To close the Telnet connection, type Ctrl + ] to access the Telnet command prompt and then type quit or exit.

This example demonstrates the simplicity of Telnet in interacting with a web server on port 80. However, keep in mind that Telnet sends data in plain text, including any passwords or sensitive information. For secure communication with a web server, consider using HTTPS and tools like curl or dedicated web browsers.

To open a shell on a remove server, it's better to use alternatives like SSH (Secure Shell) whenever possible to avoid potential risks.

Checking for Open Ports

The telnet command can also be us used to check different ports on a remote host:

Check if a remote HTTP server is running on port 80:

telnet example.com 80

Check if a remote SSH server is running on port 22:

telnet example.com 22

Check if a remote SMTP server is running on port 25:

telnet example.com 25

In the first example, the telnet command is used to connect to the remote host example.com on port 80, which is the default port for the HTTP protocol. In the second example, the telnet command is used to connect to the remote host on port 22, which is the default port for the SSH protocol. In the third example, the telnet command is used to connect to the remote host on port 25, which is the default port for the SMTP protocol.

When you run telnet command to a port, if the connection is successful, you will see a message like Connected to [hostname] and you will see a cursor blinking. If the port is closed or not available the command will return with an error message such as Could not open connection to the host, on port [port]: Connect failed.

It's worth noting that telnet is an insecure protocol since it sends data in plaintext, and it's recommended to use SSH protocol to connect to remote servers. Also, Telnet support is not enabled by default on many modern Linux distributions, so you may need to install it before using it.

Conclusion

Telnet, despite its simplicity, plays a crucial role in the realm of Linux server management. Understanding how to use it can empower you to remotely troubleshoot, configure, and interact with servers efficiently. Just remember to prioritize security and consider alternative methods like SSH for sensitive environments.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA