I'm using Magit over Tramp and my git push commands fail because tramp doesn't forward my ssh-agent to the remote machine.

In pseudo code, it looks like I need to do the following:

tramp-methods["ssh"][tramp-login-args].prepend( ("-A") )

How do I do this without copy/pasting the entire tramp-methods variable into my emacs init file? Or is there a better way to do this?

  • 1
    Check the Association-Lists section of the elisp manual.
    – npostavs
    Commented Nov 20, 2015 at 22:57
  • @npostavs: Thanks for the hint. Is there a better way to code "put something in list if not already there"?
    – Harvey
    Commented Nov 21, 2015 at 13:44
  • 5
    You could also use ~/.ssh/config to deal to this (from specific hosts) regardless of whether or not you're using Emacs to connect, by setting ForwardAgent yes for the host(s) in question.
    – phils
    Commented Nov 21, 2015 at 16:06
  • @phils: I probably should taken this approach form the start. I don't really like maintaining ssh_config files.
    – Harvey
    Commented Nov 22, 2015 at 17:19

2 Answers 2


Starting with Tramp 2.2.13, you will be able to overwrite entries of tramp-methods. In your case, you would do something like

(add-to-list 'tramp-connection-properties
             (list (regexp-quote "/ssh:[email protected]:")
                   '(("-A") ("-l" "%u") ("-p" "%p") ("%c")
                     ("-e" "none") ("%h"))))

Tramp 2.2.13 is integrated in the upcoming Emacs 25.1. Tramp 2.2.13 will be released itself at the end of this year. For the time being, you could use the development versions of Emacs or Tramp, of course.

  • Will you be able to modify the existing args or simply replace? One of the reasons I took the modify approach was so I wouldn't have to worry about stepping on future changes to tramp.
    – Harvey
    Commented Nov 22, 2015 at 17:34
  • 1
    It's not a modification or a replace, it is just a kind of overlay. tramp-methods keeps untouched. Settings in tramp-connection-properties are prioritized over tramp-methods. But you are right, likely there shall be a convenience function which returns the recent value of a given argument in tramp-methods; this would ease it to change. Will think about. Commented Nov 22, 2015 at 18:51
  • In what file should this be added? Commented May 21, 2023 at 20:15
  • In your init file, after loading Tramp. Commented May 22, 2023 at 7:56

After suggestions...

(defun add-ssh-agent-to-tramp ()
  (cl-pushnew '("-A")
              (cadr (assoc 'tramp-login-args
                           ; if on Windows using Putty with Pageant,
                           ; replace "ssh" with "plink"
                           (assoc "ssh" tramp-methods)))
              :test #'equal))
  • This did it for me. can now multi hop into ec2 instance in private subnet. Thankyou!
    – Baggers
    Commented Jan 8, 2019 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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