Fix sshd configuration errors that prevent SSH connections to an ECS instance by validating and correcting the sshd_config file.
Problem description
After you modify the sshd configuration file and restart the instance, you cannot connect to it with Workbench or third-party tools such as PuTTY, Xshell, and SecureCRT.
Cause
The /etc/ssh/sshd_config file contains errors that prevent sshd from starting, which blocks SSH connections to the instance.
Solution
Connect to the instance through VNC, fix errors in the sshd configuration file, and restart the sshd service.
-
Connect to the instance through VNC.
-
Identify the sshd binary path:
which sshdThe output shows sshd is located at
/usr/sbin/sshd.
-
Run sshd in test mode to check for configuration errors.
ImportantRun this command after every sshd_config change and before restarting the instance to catch errors early.
sudo /usr/sbin/sshd -t-
If the configuration is correct, no output is returned.
-
If errors exist, the output shows the error location. For example, the following output indicates that the value on line 144 of
/etc/ssh/sshd_configis set to an unsupported valueno1.
-
-
Fix the /etc/ssh/sshd_config file.
-
Open the
/etc/ssh/sshd_configfile:sudo vim /etc/ssh/sshd_config -
Press
Ito enter Insert mode. Locate the error line from the previous output and fix it. For example, set UseDNS on line 144 to no.UseDNS no -
Press
Esc, enter:wq, and press Enter to save and close the file.
-
-
Rerun
sudo /usr/sbin/sshd -tto verify the fix. If no output is returned, the configuration is correct.
-
Restart the sshd service to apply the changes.
sudo systemctl restart sshd.serviceNoteIf the systemctl command fails, see What do I do if an error occurs when I run the systemctl command on a Linux instance?.