How to avoid ssh from prompting key passphrase for passwordless logins

The ssh-agent program is an authentication agent that handles passwords for SSH private keys. Use ssh-add to add the keys to the list maintained by ssh-agent. After you add a private key password to ssh-agent, you do not need to enter it each time you connect to a remote host with your public key.

Generating authentication key pairs

Use the ssh-keygen command to generate authentication key pairs as described below. Provide a passphrase, for example “password”, when creating the key pairs.

# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:4lYqkqgXmhIxoyMdT+ZfGFCxeMUqTnXLjrRQKjbEC/U root@geeklab
The key's randomart image is:
+---[RSA 2048]----+
| o.  .oo.        |
|. o...ooo        |
| o .E=o+ .       |
|+ * B.+ o        |
|.* @ +.*S        |
|=.o.+.++o        |
|o=o...+.         |
|= .. o.          |
|o.               |
+----[SHA256]-----+

Copy the Public key to remote host

1. Copy the public key to ~/.ssh/authorized_keys on the remote system.

# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.12.10

2. Now try logging into the machine, with “ssh ‘root@192.168.12.10′”, and check in the .ssh/authorized_keys file to make sure we haven’t added extra keys that you weren’t expecting.

$ ssh 192.168.12.10
Enter passphrase for key '/root/.ssh/id_rsa': 
Last login: Wed Apr 06 09:03:50 2014 from 192.168.12.20

Add private key password to ssh-agent

1. To add the private key password to ssh-agent, enter the following command:

# exec ssh-agent $SHELL

2. The next step is to use the ssh-add command to add the key.

# ssh-add
Enter passphrase for /root/.ssh/id_rsa: 
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

3. The “ssh-add -l” command lists fingerprints of all identities currently represented by the agent.

# ssh-add -l
2048 SHA256:4lYqkqgXmhIxoyMdT+ZfGFCxeMUqTnXLjrRQKjbEC/U /root/.ssh/id_rsa (RSA)

4. You can try loggin in to the remote system without password now.

$ ssh 192.168.12.10
Last login: Thu Apr 06 11:13:29 2014 from 192.168.12.20

In this example, the passphrase is remembered for only the current login session and is forgotten when you log out.

Related Post