1

I am wondering what is the best way to set up a default port that is not the standard 22 port. I regularly log in with tramp to remote systems and would like to avoid having to put in #1234 every time I connect.

My tramp config (org-mode) looks as follows

* Tramp
For TRAMP to work with ssh-key, install the programm ssh-askpass
 (https://github.com/markcarver/mac-ssh-askpass)
 C-x C-f /ssh:you@remotehost|sudo:remotehost:/path/to/file

#+BEGIN_SRC emacs-lisp
(defun url-clear-cookies ()
  "Clear all cookies."
  (interactive)
  (setq url-cookie-storage nil)
  (setq url-cookie-secure-storage nil))

(use-package password-cache
  :ensure nil
  :custom
  ;; Never expire passwords
  (password-cache-expiry nil))

(use-package tramp
  :ensure nil
  :custom
  (tramp-default-method "ssh"))

(defun add-ssh-agent-to-tramp ()
  (cl-pushnew '("-A")
              (cadr (assoc 'tramp-login-args
                           (assoc "ssh" tramp-methods)))
              :test #'equal))

(use-package tramp-sh
  :ensure nil
  :custom
  ;; Use out-of-band method for big files
  (tramp-copy-size-limit (* 0.5 1024 1024))
  :config
  ;; Use the PATH from the remote
  (add-to-list 'tramp-remote-path 'tramp-own-remote-path))

(use-package tramp-gvfs
  :ensure nil
  :after tramp-ftp
  :config
  ;; Prefer gvfs for FTP
  (add-to-list 'tramp-gvfs-methods "ftp"))

(defun local-file-name-as-sudo (file-name)
  "Transforms /foo/bar.ext into /sudo::/foo/bar.ext"
  (concat "/sudo::" file-name))

(defun tramp-file-name-as-sudo (file-name)
  "Transforms /scp:user@host:/foo/bar.ext into /ssh:user@host|sudo:host:/foo/bar.ext"
  (let* ((parts (tramp-dissect-file-name file-name))
         (host (tramp-file-name-host parts)))

    ;; replace "host:" by "host|sudo:host:"
    (setq file-name (replace-regexp-in-string (regexp-quote (concat host ":"))
                                              (concat host "|sudo:" host ":")
                                              file-name t t))

    ;; replace scp by ssh otherwise sudo doens't work
    (setq file-name (replace-regexp-in-string "^/scp" "/ssh" file-name))))

(defun buffer-file-name-as-sudo (&optional buffer)
  "Return BUFFER filename as sudo"
  (require 'tramp)
  (let* ((buffer (or buffer (current-buffer)))
         (file-name (expand-file-name (or (buffer-file-name buffer) dired-directory))))
    (if (tramp-tramp-file-p file-name)
        (tramp-file-name-as-sudo file-name)
      (local-file-name-as-sudo file-name))))

(defun find-alternative-file-with-sudo ()
  (interactive)
  (find-alternate-file (buffer-file-name-as-sudo)))
#+END_SRC
6
  • Don’t try this in tramp. Use your ~/.ssh/config.
    – nega
    Feb 23, 2019 at 22:22
  • thanks, and I thought that the above causes tramp to use SSH by default. But still I have to put in ssh every time. How can I change that?
    – CD86
    Feb 23, 2019 at 22:24
  • Set tramp-default-method to set tramp’s default method ;)
    – nega
    Feb 23, 2019 at 22:29
  • I did that, ah shown above, still it does not work and only shows "file not found, if I leave out the SSH. If I put it, it works though
    – CD86
    Feb 23, 2019 at 22:31
  • Try (setq tramp-default-method “ssh”) outside of your use-package statement.
    – nega
    Feb 23, 2019 at 22:41

1 Answer 1

2

In your init.el you have

(use-package tramp
  :ensure nil
  :custom
  (tramp-default-method "ssh"))

This should cause an error Error (use-package): tramp/:config: Symbol’s function definition is void: tramp-default-method. Doing:

(use-package tramp
  :ensure nil
  :custom
  (setq tramp-default-method "ssh"))

works as expected. After startup, doing Ctrl+h v tramp-default-method, gets you:

tramp-default-method is a variable defined in ‘tramp.el’.
Its value is "ssh"
Original value was "scp"

Documentation:
Default method to use for transferring files.
See ‘tramp-methods’ for possibilities.
Also see ‘tramp-default-method-alist’.

You can customize this variable.

Now, in your ~/.ssh/config you can do something like this to multi-hop to your work dekstop:

Host work-desktop
ControlMaster yes
ProxyCommand ssh -q bastion.yoyody.ne:2222 -W work-desktop.internal:22

Tramp will now "do the right thing".

M-x find-file /work-desktop:my_project.txt
4
  • thanks a lot :) I will try this. I didn't get an error though with my version
    – CD86
    Feb 24, 2019 at 7:33
  • so I checked, and putting setq in front as you advised causes an error. In contrast omitting this, like I did from the start, works and I get the desired output if tested how the variable is set. However, if I want to connect to a remote server, I still have to put ssh, otherwise emacs hangs whilte trying to establlish the connection oO
    – CD86
    Feb 24, 2019 at 11:19
  • Curious. Try moving your init file aside, and creating a minimal (non-org-mode) replacement that initializes packages and use-package's tramp. You can try my minimal version
    – nega
    Feb 24, 2019 at 15:26

Your Answer

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

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