TransWikia.com

set up custom port for default ssh connection in tramp

Emacs Asked by CD86 on November 28, 2020

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

One Answer

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

Answered by nega on November 28, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP