1. Overview

The telnet command is a user interface to the TELNET protocol. We can use the telnet command to manage a remote machine using the command line.

It’s similar to the ssh command. The difference is that ssh uses encryption, whereas telnet sends information in plain text. To sum up, ssh is much more secure, and using telnet is not recommended.

2. Installing Telnet

To use telnet, we need to have the Telnet client installed on the local machine and the Telnet server installed on the remote machine. After that, we can use the local machine to connect to the remote machine.

2.1. Installing Telnet Client

To install telnet on the local machine, we can use package managers like yum and apt:

$ sudo yum install telnet  # On CentOS, Fedora, and RHEL
$ sudo apt install telnet  # On Ubuntu/Debian 

2.2. Installing Telnet Server

To install the Telnet server, we should run:

$ sudo yum install telnet-server  # On CentOS, Fedora, and RHEL
$ sudo apt install telnetd  # On Ubuntu/Debian

Now we’ve installed telnet on both machines.

3. Configurations

Before we can use telnet, we need to make some configurations.

3.1. Starting the Telnet Service

We need to start the telnet service on the remote machine:

$ sudo systemctl start telnet.socket  # On CentOS, Fedora, and RHEL

It’s worth mentioning that on Ubuntu/Debian machines, inetd is the service that listens on all ports used by Internet services such as FTP, POP3, and telnet. And, it starts automatically at boot. So we don’t need to start it.

Moreover, we can make the telnet service start at boot by running this command:

$ sudo systemctl enable telnet.socket # On CentOS, Fedora, and RHEL

Now let’s make sure the service is running. On Ubuntu/Debian, we should run:

$ systemctl status inetd
inetd.service - Internet superserver
     Loaded: loaded (/lib/systemd/system/inetd.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-07-04 02:50:13 +03; 1s ago

On Red Hat-based Linux distributions:

$ systemctl status telnet.socket
telnet.socket - Telnet Server Activation Socket
   Loaded: loaded (/usr/lib/systemd/system/telnet.socket; enabled; vendor preset: disabled)
   Active: active (listening) since Mon 2022-07-04 01:27:28 UTC; 2s ago

The telnet service is up and running.

3.2. Allowing the Telnet Port Through the Firewall

Next, we need to allow the telnet port (23 by default) through the firewall on the remote machine. On CentOS, Fedora, and RHEL, we should run:

$ sudo firewall-cmd --permanent --add-port=23/tcp
success

Then reload the firewall for the changes to take effect:

$ sudo firewall-cmd --reload
success

And on Ubuntu/Debian, we should run:

$ sudo ufw allow 23/tcp
Rules updated
Rules updated (v6)

Then reload the firewall:

$ sudo ufw reload
Firewall reloaded

Now we’ve successfully allowed the telnet port through the firewall.

3.3. Creating a New User for Telnet

By default, root is not allowed to login through telnet for security reasons. Hence, we need to create a new user on the remote machine:

$ sudo useradd telnet

After that, we should set a password for it:

$ sudo passwd telnet
Changing password for user telnet.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

We’ve successfully configured telnet on the remote machine.

4. Using Telnet

To connect to the remote machine from the local machine, we should run telnet in the following pattern:

$ telnet -l [username] [remote machine IP] [port (optional)]

For example:

$ telnet -l telnet 5.182.18.49
Trying 5.182.18.49...
Connected to 5.182.18.49.
Escape character is '^]'.
Password: 
[telnet@remote ~]$

To log out, we can run:

$ logout
Connection closed by foreign host.

After that, we’ll be back at our local machine.

5. Conclusion

To sum up, we learned how to install and use telnet on a Linux machine. As we discussed, ssh is much more secure than Telnet. So we should use ssh instead of Telnet whenever possible.


» 下一篇: Linux文件系统指南