To ensure the security of the connection when using SSH is by using public/private SSH keys, because passwords are not passed across the network and the system is resistant to attacks by “brute force”.
mkdir ~/.ssh
Using RSA:
ssh-keygen -t rsa
or using DSA (Digital Signing Algorithm):
ssh-keygen -t dsa
When asked for a pass phrase leave blank, since our purpose is to automate things.
Two files in the .ssh directory: id_dsa and id_dsa.pub. The pub file has the public key and will be placed on the remote server.
Copy the id_dsa.pub file to the remote server via SCP:
scp ~/.ssh/id_dsa.pub username@example.com:/home/username/
ssh username@example.com
mkdir ~/.ssh
cat id_dsa.pub >> ~/.ssh/authorized_keys
rm id_dsa.pub
chown -R username:username /home/username/.ssh chmod 700 /home/username/.ssh chmod 600 /home/username/.ssh/authorized_keys
sudo vi /etc/ssh/sshd_config
And check this lines:
RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile %h/.ssh/authorized_keys PasswordAuthentication no
sudo /etc/init.d/ssh reload
Done
Try to connect to the remote server with SSH:
ssh -i /path-to-private-key username@remote-host-ip-address
Or just this:
ssh username@remote-host-ip-address