What to do if a remote connection to an ECS instance fails due to an sshd configuration error

更新时间:
复制 MD 格式

This topic describes the cause of and solution for a remote connection failure to an Elastic Compute Service (ECS) instance due to an sshd configuration error.

Symptoms

After you modify the sshd configuration file and restart the instance, you cannot use Workbench or a third-party remote connection tool, such as PuTTY, XShell, or SecureCRT, to connect to the instance.

Cause

The /etc/ssh/sshd_config file contains configuration errors. These errors prevent the sshd service from starting. As a result, you cannot connect to the ECS instance over the Secure Shell (SSH) protocol.

Solution

Connect to the ECS instance using Virtual Network Computing (VNC). Find and correct the errors in the sshd configuration, and then restart the sshd service.

  1. Connect to the ECS instance using VNC.

    For more information, see Connect to an instance using VNC.

  2. Run the following command to find the path of the sshd executable.

    which sshd

    The following output indicates that the sshd executable is located at /usr/sbin/sshd.

    image

  3. Run the following command to start sshd in test mode and check the configuration file for errors.

    Important

    After you modify the sshd configuration file, run the following command to check the file for errors before you restart the instance. This helps prevent the issue described in this topic.

    sudo /usr/sbin/sshd -t
    • If no information is returned, the sshd configuration is correct.

    • If information is returned, the sshd configuration is incorrect. For example, the following output indicates that the parameter on line 144 of the /etc/ssh/sshd_config file is set to "no1". This is an unsupported value that you must change.

      image

  4. Modify the /etc/ssh/sshd_config configuration file.

    1. Open the /etc/ssh/sshd_config configuration file.

      sudo vim /etc/ssh/sshd_config
    2. Press the i key to enter Insert mode. Based on the error message, find the incorrect line and fix it. For example, on line 144, change the value of the UseDNS parameter to no.

      UseDNS no
    3. Press the Esc key, enter :wq!, and then press Enter to save the changes and exit the editor.

  5. Rerun the sudo /usr/sbin/sshd -t command to confirm that the configuration is correct. If no information is returned, the error is fixed, as shown in the following figure.

    image

  6. Restart the sshd service for the changes to take effect.

    sudo systemctl restart sshd.service
    Note

    If an error occurs when you run the systemctl command, see What do I do if an error occurs when I run the systemctl command on a Linux instance?