I can't figure out why everytime I ssh back into my server I always have to start my ssh agent with the command eval 'ssh-agent -s'
followed by ssh-add <my_key.pem>
in order to successfully ssh from my server. I have placed this in my .bashrc but obviously I'm missing something and don't want to always have to initialize that each time I log in.
3 Answers
As @Jakuje said, using Forward Agent and using a key on your local, secure machine is better.
However, if you need to do what you say, use a tool such as keychain
(github.com/funtoo/keychain) or ssh-find-agent
(github.com/wwalker/ssh-find-agent ( I wrote the latter, and many still use it, but I have since switched to using the former ). Both of these tools look for running gpg or ssh agents and connectyour environment up to them for you.
I believe much more secure and useful is to ForwardAgent
from your workstation, than having some keys on some server and fiddle during each login with agent reinitialization.
You can simply add to your ~/.ssh/config
for appropriate host (or use -A
switch to ssh
command):
ForwardAgent yes
Add the local key to your local agent and you don't have to care if you use it locally or remotely.
You also didn't mention if it is password protected key, but I assume no, if you put it into .bashrc
. Much easier way would be also to specify the path to key in your ~/.ssh/config
on server (if you want to have it on server) like this:
IdentityFile <my_key.pem>
again for specific host or globally.
-
I wonder what I did.... I had this issue when I set up ssh for my github account. Then I set up ssh for my digital ocean droplet and after doing ssh-copy-id, ssh-add, and then connecting to my webserver, the issue went away. I just restarted my computer and miraculously I don't have to start the agent any more. WEIRD Commented Sep 15, 2017 at 20:12
-
Not my choice cause I have hundreds of repos and do not put them to my ssh config ~/.ssh/config– Nam G VUCommented Nov 8, 2020 at 5:55
if you are encountering this with git pushes,setting git remote to match the settings in your .ssh/config should do the trick..
git remote add alice git@gitolite-as-alice:whatever.git
.ssh/config
Host gitolite-as-alice <-- matches git remote above
HostName git.company.com
User git
IdentityFile /home/whoever/.ssh/id_rsa.alice
IdentitiesOnly yes
-
Not my choice cause I have hundreds of repos and do not put them to my ssh config ~/.ssh/config– Nam G VUCommented Nov 8, 2020 at 5:54
Could not open a connection to your authentication agent.
after runningssh-add -L
after logging out.