You must have used SSH commands via the command-line terminal as a system administrator. It is a commonly used command for securely transferring data between connected machines over the network.
That being said, have you ever come across the “ssh connection refused” error? If yes, you need to make some changes in the SSH settings or check the setup.
Don’t worry; we have the answers right here. Here, we have mentioned some of the possible reasons causing this error and their solutions. Let’s get started.
Why do we get a “Connection Refused” error?
There can be various reasons for this error to occur whenever you try to establish a connection to the remote server using the SSH command. To solve or get to the root cause of this error, you need to identify why the system refuses the connection via SSH. Thus, we have mentioned some of the reasons you can use to debug the situation accordingly.
1. SSH Client is Not Installed properly
Before getting into the details, it is better to check if the SSH has been installed accurately. The machine you are trying to access the remote server should have the SSH client installed on the device. If you do not have a proper client setup, you will not have access to the desired server.
To check the SSH client's status on your local machine, you can run the ssh command in the terminal, as shown below. We are using an Ubuntu OS (SSH Ubuntu) in this case.
ssh
If you see a list of the SSH command options in the output, then the SSH client is correctly installed on the system. But, if you get the result showing the command not found, you need to install the OpenSSH client again on the system.
For installing the SSH client on the system, you can follow the below steps-
- First, open the command-line terminal.
- Run the below-mentioned commands
For Ubuntu/Debian systems:
sudo apt install openssh-client
For CentOS/RHEL systems:
sudo yum install openssh-client
2. SSH Daemon is Not correctly Installed on the Server
Similar to the previously mentioned method, we should check for the installation of the SSH daemon on the machine. You need a server version for listening and accepting the establishing connections. Thus, it might be possible that the remote server may refuse the incoming connection due to the missing setup.
To check whether SSH is available on the remote server, you can run the following command.
ssh localhost
You can start installing the SSH on the server if the output shows the connection refused error.
For installing the SSH on the remote server, you can use the steps mentioned above in problem 1.
3. If the Credentials are Wrong
There might be a human error where the user entered the wrong password for establishing the connection to the remote server, resulting in a refused connection from the server.
For this, you need to check if you are using the correct IP address of the server or not. You can verify the correct SSH open port by running the below-mentioned command on the terminal.
grep Port /etc/ssh/sshd_config
You will get the below output for the above command, which will display the port number you are using.
4. If the SSH Service is Down
Ensure you enable the required services, which must run in the background. If the service is down, the SSH daemon will not accept the incoming connection, and you will get the connection refused error on the screen.
To check the running service's status, you can run the below-mentioned command from the terminal.
sudo service ssh status
After running the command, you will get the status of the service. If it is running, there is no issue, but if the service is not enabled, you need to enable it.
You can follow the steps below to enable the required service to resolve the error.
systemctl start sshd
You can run the following command to enable the service to run at boot.
sudo systemctl enable sshd
5. A firewall is Preventing the SSH Connection
There might be a possibility that the connection is getting refused due to the firewall restrictions. The firewall will protect the server from potential attacks. But if you have SSH set up on your machine, you need to configure the firewall settings to allow the specific SSH connections.
Ensure that the firewall does not restrict the SSH connections to avoid the connection refused error. You can follow the below simple steps for allowing SSH concessions through the firewall.
To fix this issue, you need to use the ufw (uncomplicated firewall) from the command line to manage the firewall settings.
sudo ufw allow ssh
6. The SSH Port is Closed
The SSH will send the request to a specific port whenever the connection is made to the remote server via SSH. The server should have the SSH port open; else, you will get the “connection refused” error.
By default, the SSH uses port 22, and if there are no configuration changes, you need to ensure that the server listens for the incoming requests.
To list all the ports that are currently listening, you can run the below-mentioned command.
sudo lsof -i -n -P | grep LISTEN
Check for port 22 in the output, check for its state, and then set it to LISTEN. Also, you can try to check if the specific port is open, as shown below.
sudo lsof -i:22
For enabling port 22 for listening to the requests, you can use the iptables command as shown below.
sudo iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
7. SSH Debugging and Logging
You can enable the verbose or debugging mode to analyse the problem related to SSH in Linux systems. SSH will print the debugging message whenever you enable this mode to help the user troubleshoot the error and find the root cause.
In Linux, there are three levels of verbosity:
- level 1 (-v)
- level 2 (-vv)
- level 3 (-vvv)
You can run the following command with the -v option.
ssh -v [server_ip]
ssh -vv [server_ip]
or
ssh -vvv [server_ip]
Conclusion
Whenever you use the SSH connections for transferring the data securely over the network, you might face some problems while establishing the connection. One of the commonly occurred errors is “connection refused”. Here, we mentioned some of the reasons why the SSH connection refused error happened and how to fix it.
You can go through this article to understand the possible reasons for the error and how you can resolve them.
People also read: