SSH login with authentication key (without using password)

If you have a remote server accessible via SSH and you often want to connect to this server, then typing password each time is very inefficient. Especially if you have multiple servers with different passwords (I hope they are different). To solve this problem you can use another authentication method called authentication by key.

To enable this first you have to create a public/private key pair on your local host. (I assume you are on a *nix system or use at least an emulator like cygwin and installed openssh already) This step is only necessary for one time and the public key can be used for multiple servers.

Creating these keys is very easy type following command on command line.

ssh-keygen

You will be asked for path where you want to store your key. You can let the defaults and press enter to continue.

And then you can type a passphrase for extra protection. Even if someone gain access to your private key without your permission (actually no one else except you should access your private keys) it is not enough to access your servers with this private key unless knowing your passphrase. If you don’t want extra protection, you can let passphrase emtpy and press enter.

Your public and private keys are generated already. (You can see them on the path you selected, by default  under ~/.ssh/ .File named id_rsa is your private key and id_rsa.pub is the public key)

Now you have to register your public key to the remote server.

ssh-copy-id username@servername

Change the username and servername (hostname or IP adress of the target server) and execute the above command. It will ask you your password and then copy your public key to target server. If it successful you can now login your remote server without password. Just try to connect in the usual way.

ssh username@servername

And that’s it.