SSH and Workbench connection failure from incorrect port

Updated at:
Copy as MD

Problem description

You cannot connect to an ECS instance using SSH or Workbench. An instance health diagnostic report indicates that the sshd process is listening on an address and port that do not match those specified in the configuration file.

Cause

You cannot connect to the ECS instance because the sshd process is listening on an address and port that are inconsistent with its configuration file, /etc/ssh/sshd_config.

Solution

To resolve this issue, modify the listening address and port in the SSH service configuration.

  1. Connect to the ECS instance using VNC.
    For more information, see Choose a connection method.
    Note If you cannot log on to the ECS instance using SSH, you must connect with VNC.
  2. Check whether the port number in the sshd_config configuration file matches the port the sshd process is listening on.

    If they do not match, proceed to Step 3 to modify the sshd_config configuration file.

    • Check the port number configured in sshd_config:
      sshd -T
      The following output shows that the configured port is 23.
      [xxx@iZbpxxx ~]# sshd -T
      port 23
      addressfamily inet
      listenaddress 0.0.0.0:23
      usepam yes
      logingracetime 120
      x11displayoffset 10
      x11maxdisplays 1000
      maxauthtries 6
      maxsessions 10
      clientaliveinterval 0
      clientalivecountmax 3
      streamlocalbindmask 0177
      permitrootlogin yes
      ignorerhosts yes
      ignoreuserknownhosts no
    • Check the port on which the sshd process is listening:
      netstat -ntlp |grep sshd
      Note If the preceding command fails, run ss -tlnp | grep sshd.
      The following output shows the sshd process is listening on port 22. Because this differs from the configured port (23), proceed to Step 3 to modify the sshd_config configuration file.
      [root@rhel8xxx        ]# netstat -ntlp |grep  sshd
      tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      5091/sshd
      tcp6       0      0 :::22                   :::*                    LISTEN      5091/sshd
  3. Modify the sshd_config configuration file.
    1. Open the sshd_config configuration file.
      vim /etc/ssh/sshd_config
    2. Press i to enter Insert mode.
    3. Modify the port number in the sshd_config configuration file to match the port the sshd process is listening on.
      The port number in the sshd_config configuration file must match the port the sshd process is listening on. In this example, change the line Port 23 to Port 22.
      #Port 22
      Port 23
      #AddressFamily any
      #ListenAddress 0.0.0.0
      #ListenAddress ::
      HostKey /etc/ssh/ssh_host_rsa_key
      HostKey /etc/ssh/ssh_host_ecdsa_key
      HostKey /etc/ssh/ssh_host_ed25519_key
    4. Press Esc, enter :wq, and then press Enter to save the changes and exit.
  4. Restart the sshd service.
    systemctl restart sshd