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


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


Find agent:

eval "$(ssh-agent -s)"

Agent pid 9546

Kill PID:

kill -9 9546


ssh [email protected]

ssh [email protected]

It should work now.

killall ssh-agent; eval "$(ssh-agent)"
  • 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
    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
    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. Sep 11, 2023 at 18:47

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.

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

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.

You must log in to answer this question.

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