SSH is widely used with automation scripting, as it makes it possible to remotely execute commands at remote hosts and read their outputs. Usually, SSH is authenticated by using a username and password, which are prompted during the execution of SSH commands. However, providing passwords in automated scripts is impractical, so we need to automate logins. SSH has an in-built feature by which SSH can auto-login using SSH keys. This recipe describes how to create SSH keys and facilitate auto-login.
SSH uses an encryption technique called asymmetric keys
consisting of two keys: a public key and a private key for automatic authentication. We can create an authentication key pair using the ssh-keygen
command. For automating the authentication, the public key must be placed at the server (by appending the public key to the ~/.ssh/authorized_keys
file) and its private key file of the pair should be present at the ~/.ssh
directory of the user at the...