Every time I SSH into another server from our headless Ubuntu server I am asked for the password to my key file. Even if I have previously connected to the server.

Do you have any idea why this maybe? It could be something as simple as ssh-agent not currently running or something.

The same key on my Ubuntu Gnome desktop is working fine. Both server and desktop are running Ubuntu 10.10.

ps -ef | grep '[s]sh-agent'
simon     3219     1  0 12:46 ?        00:00:00 ssh-agent
  • 1
    First, as you said, check whether ssh-agent is running: ps -ef | grep '[s]sh-agent' Commented Apr 21, 2011 at 13:23
  • @mssever Yep it appears to be. See updated question.
    – Treffynnon
    Commented Apr 21, 2011 at 13:25

3 Answers 3


Even if agent is up, if certain environment variables are not set, you have no reference to agent. Furthermore, even if it is all ok, agent and variables, the identity are not automatically sent to agent: that is a task for ssh-askpass, working only in X sessions.

If you are using bash, create the file ~/.bash_profile with this content:

# File: ~/.bash_profile

# source ~/.profile, if available
if [[ -r ~/.profile ]]; then
  . ~/.profile

# start agent and set environment variables, if needed
if ! env | grep -q SSH_AGENT_PID >/dev/null; then
  echo "Starting ssh agent"
  eval $(ssh-agent -s)

# ssh become a function, adding identity to agent when needed
ssh() {
  if ! ssh-add -l >/dev/null 2>&-; then
    ssh-add ~/.ssh/id_dsa
  /usr/bin/ssh "$@"
export -f ssh

# another example: git
git() {
  if ! ssh-add -l >/dev/null 2>&-; then
    ssh-add ~/.ssh/id_dsa
  /usr/bin/git "$@"
export -f git

modify the file name ~/.ssh/id_dsa following your needs, and add this line to ~/.bash_logout

# stuff to add at end of ~/.bash_logout
if ((agent_started)); then
  echo "Killing ssh agent"
  ssh-agent -k

A last note: this do not interfere with a gnome session, because in that case only ~/.profile is sourced, and you can benefit from the ssh-askpass graphical interface that ask for a passphrase and send it to the ssh-agent.

  • Thanks @enzotib its works great for sshing into other machines. But it doesn't when using git to push or pull on a public repository. Do you have any ideas how I could solve that as well?
    – Treffynnon
    Commented Apr 26, 2011 at 9:43
  • Add a function identical to ssh(), call it git() and change last line from /usr/bin/ssh to /usr/bin/git. Also, add a final line with export -f git.
    – enzotib
    Commented Apr 26, 2011 at 10:15
  • @enzotib I tried that, but now when I ssh into the machine with the bash_profile file you created above setup it asks me for the key's password. For some reason now every time I ssh in it asks even before I have executed ssh or git. See gist.github.com/942082 for the prompt I get back when I login to the offending machine.
    – Treffynnon
    Commented Apr 26, 2011 at 10:25
  • @Treffynnon: I don't know exactly how git interact with ssh, and I don't use git so that I cannot try. I modified the ~/.bash_profile to include the git() function I mentioned above, to be sure my previous explanation was clear. Also, it seems from the output you show that you modified the server machine, but all my advices were directed to the client machine only.
    – enzotib
    Commented Apr 26, 2011 at 10:42
  • @enzotib That is correct. The server I log into becomes my client when I git push to a central forge/repository from it so the file is in the right place. I think you have just given me the hint/jogged my thoughts. I have a special bash prompt that displays the git branch of the pwd. I bet that is what is triggering the password request at bash login as it attempts to git stat before rendering the bash prompt!
    – Treffynnon
    Commented Apr 26, 2011 at 10:49

I recently started using ssh-ident:


all you have to do is add something like:

alias ssh="/path/to/ssh-ident"

in your .bashrc. Alternatively, you can ln -s /path/to/ssh-ident ~/bin/ssh or some other directory in your PATH before /bin and /usr/bin.


This question has a quite good answer over at SuperUser.

I found the hint to use Keychain pretty useful.

You must log in to answer this question.

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