How to use Telnet to ping ports from the command line

Sometimes you may want to ping an IP and a port in Linux. You can accomplish this by using Telnet to ping the ports on an IP or range. If you are looking to Ping IPs and not ports. See our fping guide. This is useful for several reasons one being if you are trying to resolve a connection timed-out error on your cPanel VPS Server when sending Email or when a connection is blocked like SSH. For example, Exim or your mail program might be reporting an error like this;

Connecting to []:25 ... failed: Connection timed out

The actual full error would look something like this ;

cwd=/usr/local/cpanel/whostmgr/docroot 4 args: /usr/sbin/exim -v -M 1f5Vqj-00033t-Vq
delivering 1f5Vqj-00033t-Vq
Sender identification U=gatekeeper
Connecting to []:25 ... failed: Connection timed out (timeout=5m)
LOG: MAIN [] Connection timed out
== R=dkim_lookuphost T=dkim_remote_smtp defer (110): Connection timed out

A connection refused error means something is terminating the connection between your mail server and the remote mail server. Something like a firewall possibly. Unless you have access to the remote mail server then you won’t be able to fix this error and you would need to contact the administrator of that mail server to have the connection allowed.

Ping Ports On Linux Servers

We can use a range of programs to verify our theory. The most common would be Telnet or NMAP. We’re going to use Telnet to ping the offending mail server’s IP and port 25 which is showing the connection refused error above.

Install Telnet on CentOS/RHEL

Issue the yum command below to deploy the program

yum install telnet telnet-server -y

Next, using a text editor edit the configuration file

nano /etc/xinetd.d/telnet

Edit the line “Set disable” as below

Set disable = no:

On CentOS 6 systems issue the below command. You can just copy the whole line and paste it into the console

/bin/systemctl start xinetd.service ; chkconfig telnet on; chkconfig xinetd on

On CentOS 7 issue the below commands one at a time.

systemctl start telnet.socket
systemctl enable telnet.socket

The default port for Telnet is 23 so you need to allow this through your firewall. If you are using IPtables you can use the following command.

For CentOS 6 systems using a text editor open the IPtables file

nano /etc/sysconfig/iptables

And add this line then save and close the file.

-A INPUT -p tcp -m state --state NEW --dport 23 -j ACCEPT

Next, restart IPtables.

service iptables restart

For CentOS 7 systems issue the following commands one at a time.

firewall-cmd --permanent --add-port=23/tcp
firewall-cmd --reload

Ping Ports Linux Servers

You now have Telnet installed you can start pinging ports. In your SSH console.

The syntax is as follows;

telnet [HOST] [IP]

We want to ping the IP and port 25 so we would use;

telnet 25

It’s best to try this from different servers, if they all fail with the connection refused error you know the problem is with the remote mail server and not your server.

[First2Host@gatekeeper ~] telnet 25
telnet: connect to address Connection timed out

From the returned Telnet ports command error we know that the other mail server is not allowing connections from our server and they would need to be contacted to have connections allowed.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button