ssh: connect to host xxx.xxx.xxx.xxx port 22: Connection refused

Problem Description:

I'm having trouble trying to SSH into a target server. Specifically, when I try to connect to the server's IP address using an SSH client, I get an "ssh: connect to host [IP address] port 22: Connection refused" error message. After investigation, it was found that the problem was caused by the firewall configuration of the target server. The target server uses the Ubuntu system, and its firewall is of iptablesthe type, specifically using ufw(Uncomplicated Firewall) as iptablesthe front-end tool for . Finally, I solved the problem by checking the firewall settings and making sure the port used by SSH was open in the firewall.

When you encounter the "ssh: connect to host [IP address] port 22: Connection refused" error message when using SSH connection, it usually means that the SSH server rejected the connection request. This can be caused by several conditions:

  1. SSH server not started: The SSH server may not be properly started or running. You can make sure the SSH server is running and check the server's SSH configuration.

  2. Firewall blocking the connection: Firewall settings may be blocking SSH connections to the target server's port. You need to confirm the firewall configuration on the server and ensure that the port used by the SSH service (22 by default) is open.

  3. Unable to reach target server: There may be a network connectivity issue preventing you from reaching the target server. This could be due to a network failure, a network misconfiguration, or the destination server being unreachable.

  4. SSH misconfiguration: If you have a custom configuration of your SSH server, there may be a misconfiguration causing the connection to be refused. You can check the SSH server configuration files, such as sshd_config, and make sure it is configured correctly.

You can follow the steps to troubleshoot:

  1. Confirm that the SSH server on the target server is running and check that it is configured correctly.
  2. Check the firewall settings of the target server to make sure the port used by SSH (22 by default) is open.
  3. Make sure that the network connection between your computer and the target server is working without any network failures.
  4. If you have a custom configuration of your SSH server, check the configuration file and make sure it is configured correctly.

Check step by step:

First confirm whether the SSH server is started

  1. Log in to the target server: Log in to the target server by other means, such as a physical terminal, remote desktop, etc., using a known username and password.

  2. Check the SSH process: Execute the following command on the target server to check if the SSH process is running:

    ps -ef | grep sshd

    If you see process information containing "sshd" in the output, it means that the SSH server is running.

  3. Check the SSH service status: On some systems, you can check the running status of the SSH service with the following command:

    service ssh status

    If the output shows that the SSH service is running, then the SSH server is running.

  4. Port Listening: Use the following command to check if the server is listening on the SSH port (22 by default):

    netstat -tuln | grep 22

    If you see a line about the SSH service in the output, it means that the SSH server is listening on the port.

There may be output similar to:

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN

Confirm the target server's firewall settings

  1. Check firewall status:

    sudo iptables -L
  2. Confirm whether the SSH port is open: Find the port used by SSH in the firewall rule list, which is 22 by default. If port 22 is not listed or is disabled, then the SSH port is not open.

  3. If the SSH port is not open, you can take corresponding actions according to the firewall tool used by the server to open the SSH port:

    sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Precautions

My problem is solved here, if it is not resolved, please try the next steps.

It should be noted that I am using the Ubuntu system, and different systems may be different. For example, different firewalls require different processing methods.

Guess you like

Origin blog.csdn.net/Orlando_Ari/article/details/131085821