Mac OS X Lion, Terminal and ssh: how to start ssh-agent at login

As a freelance sysadmin, I use Mac OS X’s Terminal.app to connect to a lot of different Unix and Linux servers–I will frequently have a dozen or two (or sometimes three) terminals open to different machines.  This is one of the reasons I hate rebooting–I lose all my connections & pretty much have to start over from scratch.

I recently upgraded to Mac OS X Lion, and things have been mostly positive.  I really love the option to “Reopen windows when logging back in.”  In terminal, that means I get all my windows back! I do still have to reconnect to all the machines, but at least I can see where I was connected, and pretty much what I was doing.  I use screen extensively on  the servers I connect to, so I frequently don’t even lose what I was doing.

The only problem is that when Mac OS X boots back up and starts Terminal.app at login, ssh-agent is not started before Terminal.app starts, meaning I can’t log into servers using my SSH public keys.  Further, if you quit Terminal.app, and re-open it, it won’t reliably open ssh-agent (I’ve had it happen a couple times out of the many times I’ve tested).  The only surefire way to get ssh-agent to start is to close all your windows and then quit & restart Terminal.app with a completely blank slate.

That is a problem as it undoes all the good that saving all my windows did.  I wanted to find a way to get ssh-agent started before Terminal.app opened.  I added it to my Login Items without any success, and even adding a LoginHook to /etc/ttys did not make it start before Terminal loaded.

But, there is a workaround  in that if I have ssh-agent start at login, I can quit & restart Terminal.app and it always connects, so that is what I have done.  The probably more correct place to do is to add it to my Login Items.  To do that, I opened a Terminal window & created a symbolic link from ssh-agent to my home directory.

ln -s /usr/bin/ssh-agent .

Then I added it to my Login Items by pressing the + button and selecting it from my home dir.  I can then remove the symbolic link I created, as Login Items is smart enough to link to the actual ssh-agent program.

So now after bootup and login, if I quit Terminal.app and restart it, it always connects to the ssh-agent and I can go about my passwordless logging in to all my servers.

Share:
  • Twitter
  • Facebook
  • StumbleUpon
  • Google Bookmarks
This entry was posted in OSX, Secure Shell. Bookmark the permalink.

8 comments on “Mac OS X Lion, Terminal and ssh: how to start ssh-agent at login

  1. Pingback: Mac OS X Lion, Terminal and ssh: how to start ssh-agent at login … | Linux Affinity

  2. I always wondered why Lion would sometimes prompt me for my passphrase even though I have my ssh-agent set up from my Leopard days. Good to know why!

    Incidentally, you can just do Command-Shift-G in the file dialog to go to /usr/bin. It eliminates the need to create a temporary symbolic link to ssh-agent in your home directory.

    • With the 10.7.2 beta things have changed again.
      The trick with the startup item does no longer work.
      This seems to be a timing problem.
      The work-around is not nice but rather painless:
      After a reboot, quit and restart terminal.app.

  3. wrdieter on said:

    You might try adding this to your ${HOME}/.profile:

    
    # Start ssh-agent if it is not already running and set up ssh-agent variables
    if [ "x" == "x`ps -x -u ${USER} | egrep [s]sh-agent`" ] ; then 
      # no ssh-agent running"
      ssh-agent | sed -e "/^echo/d" > ${HOME}/bin/agent-env
    fi
    source ${HOME}/bin/agent-env
    

    You will still have to ssh-add your keys manually, or try you ssh-add calls to ssh-add within the if statement (not sure what it would do about the passwords).

    This code assumes you have a ${HOME}/bin directory that you can write to. You could just as easily dump the agent-env file in /tmp…

    Bill.

  4. you could also put this in your .bash_profile

    if [ "x`ps ax |grep ssh-agent |grep -v grep`" == "x" ]; then ssh-agent; fi

  5. Taylor Carpenter on said:

    port or brew install proctools then you can use pgrep

    [[ -z "`pgrep ssh-agent` ]] && ssh-agent

    • Fred on said:

      Taylor’s snippet omits a quote. It should read:

      [ -z "`pgrep ssh-agent`"]] && ssh-agent

      Alternatively:

      pgrep ssh-agent >/dev/null || ssh-agent

Leave a Reply

Your email address will not be published. Required fields are marked *

*

11,771 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>