Skip to content
Advertisement

SSH – Entering a Passphrase Only Once

On $ ssh localhost, I was being asked to provide password. It was tedious and kind of annoying to provide password for every login.

So, I have created ssh keys with passphrase..

JavaScript

I have copied the content of the ~/.ssh/id_rsa.pub to ~/.ssh/authorized_keys

JavaScript

Now, on $ ssh localhost I was being asked to provide passphrase. No improvement, but instead of providing password I was providing passphrase.

I came across Adding public key to ~/.ssh/authorized_keys does not log me in automatically, but it did not address my problem. As the comment states, that if the the keys were created with a passphrase, it will always ask for passphrase.

However, if the the ssh keys would have been created without passphrase, passphrase would not be required for any ssh operation.

The key take away from this post was the verbose flag -v and

JavaScript

On searching about PEM_read_PrivateKey failed I came across quite a number of posts, couple of them are from stackoverflow itself.

But could not find a proper solution.

Then I came across Setting up passwordless, passphraseless ssh which states about using a ssh agent to enter the passphrase automatically.

JavaScript

or

JavaScript

but again, it solved only for that specific session. Every time I start a new session I have to execute the

JavaScript

I could add it to my .bashrc or .zshrc but it’s annoying to enter the passphrase for every session.

Is there any way to use the same ssh-agent across session?

Advertisement

Answer

In order to solve the cross session ssh-agent, one has to comprehend

JavaScript

On executing, ssh-agent, it will output few unix command to the STDOUT

JavaScript

evaluating or eval-ing a command will execute the command’s output.

So, eval`ssh-agent` would translate to

JavaScript

The above statements sets the environment variables SSH_AUTH_SOCK and SSH_AGENT_PID and also prints the agent’s process id to the STDOUT

The environment variable SSH_AUTH_SOCK points to a UNIX socket.

JavaScript

For any other ssh operation, it looks for this socket, if present it uses it or ask for the passphrase. But this socket needs to be authenticated, prior it can be used. And that is done by adding the identity

JavaScript

Since the environment variable SSH_AUTH_SOCK is exported, it is available for any shell originating from this shell, but not any new login.

Then I came across Understanding ssh-agent and ssh-add which guided to ssh-find-agent.bash

ssh-find-agent.bash looks for all live agent socket and exports them as environment variable.

As suggested by Jon Cairns, in the post.

Download it somewhere accessible (e.g. ~/.ssh-find-agent) and add the following to your shell’s configuration file (e.g. ~/.bashrc, ~/.zshrc):***

source ~/.ssh-find-agent/ssh-find-agent.bash

set_ssh_agent_socket

AND THIS IS THE SOLUTION I HAVE BEEN LOOKING FOR

I have modified the function names, to suit my preference, same can be found at ssh-auth-sock The functions related to GPG, GNOME & OSX are not required for my purpose. The script needs to be refactored.

Moreover, OS X has its own keychain. Thus I execute these functions only for Linux

I have also created an alias for eval ssh-agent && ssh-add ~/.ssh/id_rsa , which is quicker to type than the whole command.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement