Generating a new SSH key and adding it to the ssh-agent

by

After you have checked for existing SSH keys, you can generate a new SSH key to use for authentication, then add it to the ssh-agent. Thats more comfortable to use.

If you don’t already have an SSH key, you must generate a new SSH key. If you’re unsure whether you already have an SSH key, check for existing keys.

If you don’t want to re-enter your passphrase every time you use your SSH key, you can add your key to the SSH agent, which manages your SSH keys and remembers your passphrase, or yu generate a key whitch don’t need an Password (but not so much secure)

 

 

Generating a new SSH key

  1. Open Terminal.
  2. Paste the text below, substituting in your GitHub email address.

    This creates a new ssh key, using the provided email as a label.
  3. When you are prompted to „Enter a file in which to save the key,“ press Enter. This accepts the default file location.
  4. At the prompt, type a secure passphrase. For more information, see: „Working wiht ssh key passphrases“.

 

Adding your SSH key to the ssh-agent (Linux)

Before adding a new SSH key to the ssh-agent to manage your keys, you should have checked for existing ssh keys and generated a new ssh key.

  1. Start the ssh-agent in the background.
  2. Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.

How to establish passwordless ssh between two servers

Step by step guide to establish passwordless ssh in two Unix or Linux servers. Authenticate securely using public and private keys.


If you are working in an infra where there are hundreds of Linux or Unix servers running, then you must be having big time while managing them. To deal with such large number of servers, passwordless ssh becomes an must do practice. Once can achieve remote execution of scripts, commands, sync files via scp etc tasks with passwordless ssh very easily.

Password less ssh is not compromising on security. You will be using pair of user generated keys for authentication so your security is not compromised. Its totally secured, only thing is you are being authenticated already saved keys rather than human entered password. This removes dependency of entering password and hence automatize whole process non-interactively.

Also read : Run commands on multiple linux servers from Windows machine in one go


Lets see how to setup password less ssh between two servers:

Step 1:

Create your SSH key pair on source machine. This is machine from which you will be doing passwordless SSH to destination machine.

Use below command :

Note that your key pair is id_rsa and id_rsa.pub files in shown directories. Your id_rsa is private key which will reside on source machine. id_rsa.pub is public key which reside on destination machine. When SSH attempt is made from source to destination, protocol checks these both keys from source and destination. If they match then connection will be established without asking password.


Step 2:

Now, we need to copy id_rsa.pub key on destination machine. It should be copied to home directory of intended user in destination server. It should reside under ~/.ssh/ (i.e. home directory/.ssh/) and with name authorized_keys. You can copy file using shell or any other file transfer program.

If you are trying from source machine using ssh then use below commands:

 

Here, first command create .ssh directory on destination machine. Second command copies id_rs.pub file’s content to destination machine under file ~/.ssh/authorized_keys and last command sets proper permissions.


Step 3:

You are done! Try SSH from source to destination and it will be through without password!


This method works for all Linux and Unix variants for SSH protocol. You can also configure it for different users on source and destination. One machine can have more than one authorized key (one key for one source machine), thats why we have concatenated id_rsa.pub content to authorized_keys file (not overwrite).