[Server Management] ssh cannot log in to the server, and the solution to the failure of the host to restart the ssh service

background

A server host in the laboratory has the following problem: xxx, that 123node has been unable to connect to ssh since yesterday afternoon, and it can be pinged ~ Can you take a look for me before you leave?
I verified the following and found that the ping is indeed possible, but the ssh connection prompt is as follows (assuming the IP of the service is: 123.11.22.123, and the IP is written randomly):

ssh: connect to host 123.11.22.123 port 22: Connection refused

In this case, it may be caused by the firewall, or the ssh service may not start successfully.

View firewall configuration

Use the following command to view the firewall status:

sudo ufw status

It is found that the firewall normally allows port 22 access, as shown below:

Status: active
To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere                               
22/tcp                     ALLOW       Anywhere                  
22 (v6)                    ALLOW       Anywhere (v6)               
22/tcp (v6)                ALLOW       Anywhere (v6)

Therefore, it is not the cause of the firewall.

Remarks: If it is caused by firewall blocking, just allow ssh connection directly, as shown below:

sudo ufw allow ssh

Check whether the ssh service is successfully opened

sudo su
systemctl status ssh.service

Failed appears in the returned result , indicating that the ssh service has not been successfully started

Try restarting the ssh service

sudo service ssh start
/etc/init.d/ssh start
/etc/init.d/ssh restart

After trying these operations, it was found that the ssh service was still not started successfully, and the server host was restarted, but it was found that the problem could not be solved.

Uninstall the original ssh and reinstall the new ssh

1. Uninstall the current ssh:

sudo apt-get autoremove --purge openssh-server openssh-client

2. Reinstall the new ssh

sudo apt-get update
sudo apt-get install openssh-server openssh-client

3. Check the ssh process

ps -e | grep ssh

returns as follows:

4820 ?        00:00:00 sshd

It indicates that the ssh service has been started. If your ssh service is not started, you can use the following command:

sudo service ssh start

4. View ssh status

systemctl status ssh.service

The results shown below are returned, indicating that the ssh service has started normally.
insert image description here
5. Use ssh to connect to the server, and it is found that the connection has been successful, and the problem has been successfully solved!

Summarize

If suddenly ssh cannot connect to the server, first check if you can ping the server, if the ping is successful, it is not the reason of the network; then check the firewall settings of the server to see if the port for ssh connection is denied or access is restricted Secondly, check whether the ssh service is successfully started, try to restart the ssh service to see if the problem can be solved; finally, try to uninstall the current ssh software and reinstall the new ssh software. For novices, reinstalling the ssh software is far easier and more convenient than modifying the ssh configuration file!

Guess you like

Origin blog.csdn.net/m0_37201243/article/details/122657388