Sometimes you may get SSH connection refused error while trying to connect to SSH server. In this article, we will look at how to fix SSH connection refused error.
How to Fix SSH Connection Refused Error
Here are the steps to fix SSH connection refused error.
1. Install SSH client
You may get this error because the SSH client is not installed on your local system. You need a local ssh client on your laptop/workstation to initiate SSH connection. You can check if it is installed or not with the following command.
If you get an error message saying “command not found” then it means ssh command is not installed on your system. You can install it with the following command.
$ sudo apt install openssh-client #debian/ubuntu $ sudo yum install openssh-client #redhat/fedora/centos
2. Install SSH Daemon
You may not be able to connect to SSH because there is no ssh daemon running on remote server. Just like you need a local SSH client to initiate SSH connection, you also need a SSH daemon on your remote server to accept these connections. You can verify this with the following command.
$ sudo ssh localhost
In such cases, you need to install SSH server on remote server. You can install it with the following command.
$ sudo apt-get install openssh-server
3. Incorrect Credentials
You may also get this error, if you supply incorrect credentials. Please make sure you are using the right username, password, and remote IP address. You may check if you are connecting to the right port (22) by running the following command.
$ sudo grep Port /etc/ssh/sshd_config
If the output displays port 22 then you are connecting to the right port. Otherwise, you need to edit this file to point it to Port 22.
4. SSH Service is Down
If your remote SSH service is down, then also you may be refused connection. You can run the following command on your remote server, to check if SSH service is running.
$ sudo service ssh status
In the output, you must see Active : active (running).
... Loaded : ... Active : active (running) Docs : ... ...
If you don’t see it, then run the following command to start SSH service.
$ systemctl start sshd
To enable SSH service to run at boot, run the following command.
$ sudo systemctl enable sshd
5. Firewall is blocking port 22
Sometimes, it may also be that your remote server’s firewall is blocking port 22, required for SSH connection. If your Linux system, uses ufw firewall then run the following command to open port 22.
$ sudo ufw allow ssh
If it uses iptables, then use the following command for the same.
$ sudo iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
In this article, we have looked at different reasons for getting “SSH connection refused” error and ways to fix this problem. If you get this error, please ensure that
1. Local ssh client is installed and running on your local laptop/workstation
2. Remote ssh server is installed and running on your remote server
3. Port 22 is open on your remote server
These checks should solve your problem in most cases.
Also read :