34

I accidentally killed my ssh-agent, how do I restart it without having to reconnect ?

I tried this but it does not work :

$ eval $(ssh-agent -s)
Agent pid 8055

Then, I open a new Gnome terminal with CTRL+SHIFT+N from the previous terminal window and type :

$ ssh-add
Could not open a connection to your authentication agent.

But if I open a new Gnome terminal from my first Gnome terminal by typing :

$ gnome-terminal &

then this new window is able to connect to the ssh-agent.

Is it not possible for all my Gnome terminals to "see" the ssh-agent without having to reconnect to the PC/server ?

4 Answers 4

36

This doesn't work as you supposed. ssh-agent overwrites the configuration.

TO FIX THIS---

Find agent:

eval "$(ssh-agent -s)"

Agent pid 9546

Kill PID:

kill -9 9546

THEN YOU CHECK

ssh [email protected]

ssh [email protected]

It should work now.

3
  • This starts a new agent and then immediately kills it. How does that achieve anything?
    – jlh
    Commented Jul 28 at 14:08
  • as you see, this just kills the ssh-agent and restarts a new agent - this basically refreshes the config. What do you want to achieve?
    – Rajat jain
    Commented Jul 30 at 5:10
  • Solving OP's problem is what should be achieved here: Restarting a new ssh-agent in such a way that when a new terminal is opened, it just works.
    – jlh
    Commented Jul 31 at 6:35
5
killall ssh-agent; eval "$(ssh-agent)"
3
  • 2
    Welcome to the site, and thank you for your contribution. Would you mind adding some explanation on how that solves the OPs problem? Also, please note that the "backtick" notation for command substitutions is deprecated, and the $( ... ) syntax is recommended instead.
    – AdminBee
    Commented Aug 19, 2020 at 12:59
  • 1
    I have my doubts that this would "restart (ssh-agent) without having to reconnect"; perhaps you could elaborate?
    – Jeff Schaller
    Commented Aug 19, 2020 at 13:32
  • 2
    I just tested this on MacOS. It works the same way the accepted answer works, but is simpler. I'd suggest ssh-agent -k rather than killall ssh-agent though — no need to potentially kill more processes than we need to. Commented Sep 11, 2023 at 18:47
2

The problem is, your original ssh-agent was probably started by your GUI session start-up scripts, and the SSH_AUTH_SOCK environment variable created at that point was inherited by the processes of your desktop environment.

Any new processes started through the GUI will keep inheriting the old value, and any running Gnome terminal processes (and the shells inside them) have already inherited the old value. You would need to push the new SSH_AUTH_SOCK value into every process that has the old value, and also trigger every running ssh client process to reconfigure themselves to use the new value. There is simply no mechanism for this.

What you could do instead, is note the old and new SSH_AUTH_SOCK values, and then create a symbolic link in place of the old agent socket, pointing to the new socket.

In other words:

ln -sf <new SSH_AUTH_SOCK value> <old SSH_AUTH_SOCK value>

Just remember that the new agent is still a child process of the shell it was started from, and may die if that shell exits.

2
  • 1
    This is genius! Unlike the other answers, this also works for any shells/terminals started later on. It's no problem to exit the shell where ssh-agent was started from, the agent process continues running.
    – jlh
    Commented Jul 28 at 14:14
  • This was a very interesting idea and it worked for me too. I hope the OP tries it out and accepts this answer. Commented Sep 9 at 1:42
-2

Try restart using the following command:

sudo service ssh restart

The private/public RSA SSH keys are located in ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub, respectively. You can transfer the public key to another machine to connect to it through public key authentication. This can be done via ssh-copy-id like so:

ssh-copy-id username@host

Or you can append your public key (id_rsa.pub) to the server's /home/username/.ssh/authorized_keys file, which is in essence what ssh-copy-id does.

1
  • 12
    This command does not restart the ssh-agent, it only restarts the ssh service
    – SebMa
    Commented May 12, 2019 at 22:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .