My default-directory is: /ssh:[email protected]#2222:/home3/lawlist

My .authinfo file contains: machine 12.34.567.89 login lawlist password abcdefg

The above-combination works just fine. However, I would like to remove #2222 from the default-directory and add port 2222 to my .authinfo file.

If I remove #2222 from the default-directory and add port 2222 to my .authinfo file, neither Eshell nor Dired permit me to login to the remote server.


Waiting for prompts from remote shell ...
Timeout reached, see buffer `*tramp/ssh [email protected]*` for details.

Q:  How can I remove #2222 from my default-directory and force Eshell and/or Dired to extract port 2222 from my .authinfo file?

EDIT: The following is rough draft to add this new feature. A maintainer of Tramp, @Michael Albinus, has suggested that this may break other aspects of Tramp and he has invited a discussion on [email protected] regarding this issue.

  1. Ensure you are using a version of Emacs containing the December 17, 2015 bug fix to auth-source-ensure-strings that was resolved by commit 9384953: https://github.com/jwiegley/emacs/commit/938495317a02b06a6c512832d0c6d9530fcd7f2b

  2. Define the following function:

(defun get-auth-info (host user &optional port)
  (let ((info (nth 0 (auth-source-search
                      :host host
                      :user user
                      :port port
                      :require '(:user :secret)
                      :create t))))
    (if info
       (let* ((port (plist-get info :port))
              (secret-maybe (plist-get info :secret))
                (if (functionp secret-maybe)
                  (funcall secret-maybe)
           (list port secret))
  1. Comment out :port tramp-current-method in tramp-read-passwd. tramp-current-method is "ssh" and this prevents auth-source-search from returning a result if .authinfo contains a specified port other than ssh; e.g., port 2222.

  2. Within tramp-maybe-open-connection, comment out and replace this section as follows:

  ;; (when (string-match tramp-host-with-port-regexp l-host)
  ;;   (setq l-port (match-string 2 l-host)
  ;;         l-host (match-string 1 l-host)))

    ((string-match tramp-host-with-port-regexp l-host)
      (setq l-port (match-string 2 l-host)
            l-host (match-string 1 l-host)))
    ((and l-host l-user)
      (let ((port (car (get-auth-info l-host l-user))))
        (when port
           (setq l-port port)))))

2 Answers 2


In auth-source.el, the port specification for Tramp is redefined. It must contain the Tramp method name, you cannot define the port number ssh is using.

Instead, I recommend to use a proper entry in ~/.ssh/config:

Host 12.34.567.89
    Port    2222

Of course, you could add other attributes like HostName and User.

  • Whenever you have some time, it would be wonderful if you could please take a look at my proposed solution (to add this new feature) in the edited question above and let me know what you think.
    – lawlist
    Mar 9, 2017 at 18:44
  • I see. However, it will break existing code, so I doubt it will go into the main branch of Emacs / Tramp. If you want to discuss your solution in detail, we'll better move to the Tramp ML [email protected]. Mar 9, 2017 at 19:02
  • Thank you for taking a look at the proposed new feature -- I had a feeling it might break something that I was unaware of, which is why I did not want to post an alternative answer. I would be happy to launch a feature request / discussion and will put this on my todo-list, but I have some work-related projects to attend to at the moment. In the meantime, I will mark your answer as correct and put a notation in the edited question cautioning that this was a draft that the Tramp maintainer believes may break other aspects of of Tramp.
    – lawlist
    Mar 9, 2017 at 19:07

TRAMP doesn't look for port numbers in the authinfo file; you can see the function tramp-parse-netrc-group for details. It looks like you could modify tramp-host-regexp, but I don't know off-hand if that would work out; later users of that information might fail if it's not just a hostname.

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.