Why telnet on a specific port for localhost fails with "connection refused" error?

Solution Verified - Updated -

Environment

Red Hat Enterprise Linux(all versions)

Issue

Getting connection refused while doing telnet on localhost on a specific port, for ex 7000.

$ telnet localhost 7000
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
$

Resolution

Work with your application vendor to bind the specific port (ex : 7000) to a specific IP address to be able to telnet on localhost, bind port 7000 to localhost IP or all IP's. netstat/ss should show below lines :

$ sudo netstat -neopa |grep 7000
tcp     0   0 127.0.0.1:7000        0.0.0.0:*               LISTEN      1000    2675189 111478/nc           off (0.00/0/0)
$

or

$ sudo netstat -neopa |grep 7000
tcp     0   0 0.0.0.0:7000          0.0.0.0:*               LISTEN      1000    2671135 112149/nc           off (0.00/0/0)
tcp6    0   0 :::7000               :::*                    LISTEN      1000    2671134 112149/nc           off (0.00/0/0)
$

Root Cause

  • If the ports are bind to a specific IP address, then telnet on localhost will fail :
$ sudo netstat -neopa |grep 7000
tcp     0   0 192.168.1.4:7000      0.0.0.0:*               LISTEN      1000    2679363 112838/nc           off (0.00/0/0)
$

Diagnostic Steps

  • Check if your application is running on a specific IP address.
$ sudo netstat -neopa |grep 7000
tcp     0   0 0.0.0.0:7000          0.0.0.0:*               LISTEN      1000    2671135 112149/nc           off (0.00/0/0)
tcp6    0   0 :::7000               :::*                    LISTEN      1000    2671134 112149/nc           off (0.00/0/0)
$

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments