ssh: connect to host 192.168.1.1 port 22: Connection refused

ssh: connect to host 192.168.1.1 port 22: Connection refused

 

I tried to SSH to my CentOS Linux server via putty and got the above error. It might be due to any of the below reasons you are unable to SSH to the linux server.
 


 
Reasons :

1. SSH port 22 is not open/blocked by the server firewall

2. SSH service might not be running on your server

3. The server might be having a custom SSH port number

4. PermitRootLogin might be disabled on your server

5. Your IP address might be blocked by the server firewall.

6. Your server might be having sudo, su access OR key based authentication
 


 
FIX :

 
1. Log into your linux server through console access and disable the server firewall

service iptables stop

Now check whether you’re able to SSH to the server. If you’re able to access SSH then the issue is related to the firewall.

Command to check whether you’re IP address is blocked by the server firewall : iptables -nL | grep

Example : iptables -nL | grep 192.168.2.5

Remove the iptables rule if your IP address is blocked.

Type the command “iptables -nL” and make sure there is no rule blocking the port 22.

Open 22 port in server firewall and restart the firewall.

Command to Open 22 Port : iptables -A INPUT -p tcp –dport 22 -j ACCEPT

If you are using CSF firewall you must open the SSH port in CSF configuration file /etc/csf/csf.conf

If you are having Amazon cloud server, the port number should be opened in the security group.

 
2. Check whether you have set custom ssh port number in SSH configuration /etc/ssh/sshd_config

root@server [~]# grep Port /etc/ssh/ssh_config
Port 2222

My server had a custom SSH port number 2222 and that is the reason why port 22 was rejected with error message. Try the below command to ssh

ssh root@192.168.2.5 -p 2222

 
3. PermitRootLogin should be set to yes in SSH configuration file.

root@server [~]# grep -i permit /etc/ssh/sshd_config
PermitRootLogin yes

 
4. Type the command ‘netstat -tupln | grep ssh’ to check whether SSH service is running.

root@server [~]# netstat -tupln | grep -i ssh
root@server [~]#

Check the status of SSH service: service ssh status OR service sshd status

ssh service is not running on the above server and that is the reason why you’re unable to SSH to the server.

Restart SSH service using command : service sshd restart

[root@server ~]# service sshd restart
Stopping sshd: [ OK ]
Starting sshd: [ OK ]

Run the command “service sshd status” OR “service ssh status” to make sure the service is running fine

root@server [~]# netstat -tupln | grep -i ssh
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 28013/sshd
tcp 0 0 :::22 :::* LISTEN 28013/sshd