Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have set up automatic (password less) ssh login to some servers using ssh-copy-id. ssh-agent works only from the terminal where it was run. How do I get ssh-add to work in all my terminals?

Naturally, I would not prefer SSH key without a passphrase.

share|improve this question
up vote 3 down vote accepted

If you're logging into a graphical session, arrange to start ssh-agent during your session startup. Some distributions already do that for you. If yours doesn't, arrange to run ssh-agent from your session startup script or from your window manager. How do do that depends on your desktop environment and your window manager. For example, if you start your window manager manually, simply replace the call to my_favorite_wm by ssh-agent my_favorite_wm.

Do not start ssh-agent from .bashrc or .zshrc, since these files are executed by each new interactive shell. The place to start ssh-agent is in a session startup file such as .profile or .xsession.

If you want to use the same SSH agent on all processes no matter where you logged in from, you can make it always use the same socket name, instead of using a randomly-named socket. For example, you might put this in your ~/.profile:

export SSH_AUTH_SOCK=~/.ssh/ssh-agent.$HOSTNAME.sock
ssh-add -l 2>/dev/null >/dev/null
if [ $? -ge 2 ]; then
  ssh-agent -a "$SSH_AUTH_SOCK" >/dev/null
share|improve this answer

Apply it to your desktop environment or window manager. When I've done this manually in the past with a custom ~/.Xclients, I just used this as the last line:

ssh-agent mywindowmanger

There might be some DE's that have their own setup options for this, although it appears to me that (e.g.) KDE does not. Currently, it seems that mine was run via code from /etc/X11/xinit/xinitrc-common (presumably something done by fedora), since it is active for all users regardless of DE/WM and the parent process command is $HOME/.Xclients, but that file does not reference ssh-agent (whereas /etc/X11/xinit/xinitrc-common does).

If you don't have a ~/.Xclients, you could create one with just that one line, but you will need to know the command that starts your DE/WM.

share|improve this answer

You probably want a program such as Keychain, which was designed for this exact purpose. From the man page:

   keychain is a manager for ssh-agent, typically run from ~/.bash_profile.
   It allows your shells and cron jobs to share a single ssh-agent process.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.