How to Fix a Client/Server Connection Refused Using netcat?

Netcat, or nc, is a versatile utility of Linux command line that allows users to read and write data across networks. It is mostly used for network troubleshooting and provides the simplest way to transfer files using the UDP and TCP protocols. While working with it, some users reported a “Connection refused” error from the client side.

This guide explains the multiple reasons and possible fixes for the client/server connection refused error using netcat.

  • What Causes the Connection Refused Error While Using the netcat Command?
  • Reason 1: netstat Utility is not Installed
  • Solution: Install the netstat Command on Linux
  • Reason 2: The Listening Port is Closed
  • Solution: Configure Port for Establish Connection

Reason 1: Connection Refused Error While Using the netcat Command?

If the error “Connection refused” comes up while trying to use the netcat command to connect to a remote server, it means the server is not accepting the connection. This is because the port is closed. 

Another reason could be that the netstat utility is not installed on the remote server.

Solution: Install the netstat Command on Linux

To install the netstat command, use the following commands based on your distro:

$ sudo apt install netcat       #Ubuntu/Debian
$ sudo yum install nc          #CentOS/RHEL
$ sudo dnf install nc          #Fedora

The above image confirms the installation of netcat on Ubuntu 22.04.

Reason 2: Connection Refused Error While Using the netcat Command?

To fix the “Connection Refused” error while using the netcat command, determine the cause first; there could be multiple causes, as discussed below.

Verify if the Server is Running

To check if the server is up and listening to the specific port, understand this command before executing it:

  • The option -z specifies the netstat command to scan for the ports.
  • -v enables the verbose mode to display additional information.
  • -n instructs the netstat command to perform DNS lookup on the following IP.
  • 2>&1 is there to print the errors.
$ netcat -z -v -n 192.168.222.135 1200-5000 2>&1

In the above image, all the ports from 1200-5000 are closed for communication.

Let’s set up a port for the netcat to use by executing this command and here:

  • -l sets the netstat to listening mode.
  • -p specifies the port on which the netstat command will listen.
$ nc -l -p 1234

The above command opens up a port “1234” for the netcat command to use.

Now open up another terminal session, and use the ip command to find the IP address of the server:

$ ip a

Once the IP is viewed, share it with the client, and on the client side, use the netcat command like this to connect:

$ nc 192.168.222.134 1234

The connection is now established, and you can now use the netcat command to perform various operations discussed in this guide with detail.

Conclusion

To fix the client/server connection refused using netcat, the port on the server must be opened to listen to the client. To do this, the command “nc -l -p <Port-to-Use>” is executed, and from the client side, the command “nc <Server-IP-Address> <Port-to-Use>” is used. This fixes the error “Connection Refused” while the netcat command is being used.

This guide explained the reason for the client/server connection refused error using netcat and fixed it.