Friday, April 20, 2012

Configuring Emacs: making TRAMP use ssh-agent

TRAMP provides Emacs with remote editing over ssh and sftp sessions.  We should be able to leverage ssh-agent to eliminate the need for passwords when using TRAMP.  Sometimes it doesn't automatically use the ssh-agent.  It may be because of a custom configuration, old version, or a platform that isn't full supported.  This is one technique that has worked for me.
  • In your emacs config set SSH_AUTH_SOCK.
cat >> ~/.emacs <<EOF
(setenv "SSH_AUTH_SOCK" (concat (getenv "HOME") "/.ssh-auth-sock"))
  • Start the ssh-agent
  • Add and authenticate your key to the ssh-agent
  • Create a soft link to the ssh auth sock file
cat >> ~/.profile <<EOF
test -r ~/.agent && . ~/.agent

ssh-add -l > /dev/null 2>&1
test ${?} = 2 && ssh-agent -s > ~/.agent

ln -sf $SSH_AUTH_SOCK ~/.ssh-auth-sock

ssh-add -l > /dev/null 2>&1
test $? = 1 && ssh-add

Now you should be able to test TRAMP.  Access a file on a server with authorized_keys set up and verify you don't get prompted for a password.