Problem: couldn’t connect to Ubuntu 12.04 SSH Server. Everytime client wants to connect through ssh, it gives error:
Read from socket failed: Connection reset by peer
Then on the server side, checking the log using tail -f /var/log/auth.log
while trying to connect from client gives:
ubuntu sshd[3207]: Server listening on 0.0.0.0 port 22. ubuntu sshd[3207]: Server listening on :: port 22. ubuntu sshd[3208]: error: Could not load host key: /etc/ssh/ssh_host_rsa_key ubuntu sshd[3208]: error: Could not load host key: /etc/ssh/ssh_host_dsa_key ubuntu sshd[3208]: error: Could not load host key: /etc/ssh/ssh_host_ecdsa_key
According to many sources, this is possibly because the host key files are not exist. To generate the host key files they do this:
sudo ssh-keygen -t rsa1 -f /etc/ssh/ssh_host_rsa_key sudo ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key sudo ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
Then restart SSH server:
sudo service ssh restart
But this doesn’t solve the problem.
To solve this, we need to reinstall the openssh server and wipe all the files:
cd /etc sudo mv ssh/ ssh.old/ sudo apt-get remove openssh-server openssh-client ssh-import-id
Watch the output and make sure it is uninstalling. Then install it again:
sudo apt-get install openssh-server openssh-client ssh-import-id
The installation process should generate new host key files, if not, check if you have removed/renamed the /etc/ssh/
folder properly.
This is the sign when your problem is about to be solved:
Unpacking openssh-server (from .../openssh-server_1%3a5.9p1-5ubuntu1_amd64.deb) ... Selecting previously unselected package ssh-import-id. Unpacking ssh-import-id (from .../ssh-import-id_2.10-0ubuntu1_all.deb) ... Processing triggers for ureadahead ... ureadahead will be reprofiled on next reboot Processing triggers for ufw ... Processing triggers for man-db ... Setting up openssh-server (1:5.9p1-5ubuntu1) ... Creating SSH2 RSA key; this may take some time ... Creating SSH2 DSA key; this may take some time ... Creating SSH2 ECDSA key; this may take some time ... ssh start/running, process 21733 Setting up ssh-import-id (2.10-0ubuntu1) ...
Notice the part where the installation process generating the host keys. Now the problem is solved.
Don’t forget to secure your server by disabling root login in /etc/ssh/sshd_config
:
#PermitRootLogin yes PermitRootLogin no